/*
 *  Copyright (C)  2000-2001 Marc Wandschneider <mw@kiltdown.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation.
 *
 *  For more information look at the file COPYRIGHT in this package
 *
 */

#ifndef _PROPERTIES_H_
#define _PROPERTIES_H_

#include <sanity.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <llist.h>

/**
 * This class defines a simple little properties file.  Entries in the file
 * are of the format:
 *
 * Name=value
 *
 * Comment lines starting with # are also permitted.
 *
 * Names and Values are each limited to 1024 characters.
 *
 * CONSIDER: marcw, 10.11.2000 -- this class doesn't do DBCS or wide chars.
 * is this a huge problem??
 */
const int MAX_NAMEVALUE_LEN = 1024;


struct NameValuePair {
    const char *name;
    const char *value;
    const char *comment;
};


class PropertiesFile {

  public:
    /**
     * Create a new instance of this class using the given filename.  returns
     * S_CREATED if we're creating a new one (the file doesn't exist on disk
     * yet)
     */
    static ERRCODE openPropertiesFile(const char *, PropertiesFile **,
                                      bool createIfNecessary = false);

    /**
     * These routines add properties to the file.  If the nmae already
     * exists, S_WARNING is returned instead of S_OK on success.
     */
    ERRCODE writeStringProperty(const char *name, const char *value);
    ERRCODE writeIntProperty(const char *, int);
    ERRCODE writeDoubleProperty(const char *, double);
    ERRCODE writeBooleanProperty(const char *, bool);

    /**
     * Read in the properties.  CALLER is reponsible for freeing value
     * in string case.  S_NODATA is returned if the name doesn't exist
     * in the file.
     */
    ERRCODE getStringProperty(const char *name, const char **value);
    ERRCODE getIntProperty(const char *, int *);
    ERRCODE getDoubleProperty(const char *, double *);
    ERRCODE getBooleanProperty(const char *, bool *);

    /**
     * Makes sure our output is up to date.
     */
    ERRCODE sync();

    /**
     * Closes down the file.
     */
    ERRCODE close();

    ~PropertiesFile();

  private:
    /**
     * Don't call me -- call openPropertiesFile instead.
     */
    PropertiesFile();

    /**
     * Find the given name.
     */
    NameValuePair *findProperty(const char *name);

    /**
     * Here is where we'll store our values.  Comment lines are preserved on
     * input, but new keys are written out at the end.
     */
    LinkList<NameValuePair *> keyList;

    /**
     * the filename of the properties file.
     */
    const char *fileName;

    mode_t umask;
};

#endif // _PROPERTIES_H_




syntax highlighted by Code2HTML, v. 0.9.1