#ifndef PREFS_H
#define PREFS_H


#define PREF_NAME_NOT_FOUND (-999)


typedef struct values_struct {
    char *value;	/* The value */
    int mapping;	/* The enum this value maps to, if any */
} Values;
typedef struct prefs_struct {
    char *name;		/* The name of the pref */
    Values *values;	/* The valid values */
} Prefs;


/*
 * Returns the number of valid prefs successfully read from the file.
 * Takes the filename of the prefs file to read.
 */
int readPrefs(char *fname);

/*
 * Initializes the internal globals and obtains a pointer to the
 *   user's prefs structure.
 */
void initPrefs(Prefs *prefs);

/*
 * Returns the current number of valid preferences read from the file.
 */
int getInternalPrefCount();

/*
 * If newval is NULL, it is assumed you want to match name with
 * the value of the corresponding mapping enumeration.
 *
 * Returns 1 if everything is valid and a match is found. Else 0
 * and sets the error string.
 */
int setPref(char *name, char *newval, int mapping);

/*
 * Called with the index into the user defined prefs. If it doesn't already
 * exist, it adds the default.
 *
 * Returns 1 if successfully added, 2 if it already exists, 0 if failure.
 */
int defaultPref(int index);

/*
 * Takes the name to get the mapping for, returns -1 on error, otherwise
 * the correct mapping value.
 */
int getPrefMapping(char *name);

/*
 * Takes the name to get the value string for, returns NULL on error,
 * otherwise the correct value string duplicated (you can NOT free the
 * returned string and each time you call getPrefValue it will overwrite
 * what was previously there.
 */
char *getPrefValue(char *name);

/*
 * Takes a string and prints out that string and additionally any
 * error message available.
 */
void prefError(char *start);

/*
 * Takes a string and sprintfs that string and additionally any
 * error message available. Returns an allocated string you must
 * free up.
 */
char *strPrefError(char *start);

/*
 * Writes out the current preferences. This does NOT mean it writes out
 * your user preferences structure, it only writes out the preferences that
 * it read in. If you have made any changes to the preferences which were
 * read in (via setPref) then they will be written out with the new values.
 */
int writePrefs();

/*
 * Prints out the preferences...primarily for debugging.
 */
void printPrefs();

/*
 * Takes the pref name and the mapping to compare. Return value is just
 * like strcmp(cmpmap,internal_map) only with integers. If cmpmap is
 * less than internal_map, it will return -1, == will return 0, > will
 * return +1. If it is unable to find the preference name, it will return
 * PREF_NAME_NOT_FOUND.
 */
int prefCmpMapping(char *name, int cmpmap);

/*
 * Takes the pref name and the value to compare. Return value is just
 * like strcmp(cmpval,internal_value). If it is unable to find the
 * preference name, it will return PREF_NAME_NOT_FOUND.
 */
int prefCmpValue(char *name, char *cmpval);

#endif /* PREFS_H */
