started documentation of UcxProperties + some fixes

Mon, 05 Aug 2013 14:38:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 Aug 2013 14:38:37 +0200
changeset 130
633f15ce2ee4
parent 129
61edec666928
child 131
fc3af16818a3

started documentation of UcxProperties + some fixes

ucx/properties.c file | annotate | diff | comparison | revisions
ucx/properties.h file | annotate | diff | comparison | revisions
     1.1 --- a/ucx/properties.c	Wed Jul 24 14:26:17 2013 +0200
     1.2 +++ b/ucx/properties.c	Mon Aug 05 14:38:37 2013 +0200
     1.3 @@ -210,8 +210,8 @@
     1.4              return 1;
     1.5          }
     1.6      }
     1.7 -    if(parser->error) {
     1.8 -        return 1;
     1.9 +    if (parser->error) {
    1.10 +        return parser->error;
    1.11      } else {
    1.12          return 0;
    1.13      }
    1.14 @@ -219,17 +219,20 @@
    1.15  
    1.16  int ucx_properties_load(UcxMap *map, FILE *file) {
    1.17      UcxProperties *parser = ucx_properties_new();
    1.18 -    if(!parser || !map || !file) {
    1.19 +    if(!(parser && map && file)) {
    1.20          return 1;
    1.21      }
    1.22      
    1.23 +    // buffer size is documented - change doc, when you change bufsize!
    1.24 +    const size_t bufsize = 1024;
    1.25 +    
    1.26      int error = 0;
    1.27      size_t r;
    1.28 -    char buf[1024];
    1.29 -    while((r = fread(buf, 1, 1024, file)) != 0) {
    1.30 +    char buf[bufsize];
    1.31 +    while((r = fread(buf, 1, bufsize, file)) != 0) {
    1.32          ucx_properties_fill(parser, buf, r);
    1.33 -        if(ucx_properties2map(parser, map)) {
    1.34 -            error = 1;
    1.35 +        error = ucx_properties2map(parser, map);
    1.36 +        if (error) {
    1.37              break;
    1.38          }
    1.39      }
    1.40 @@ -253,7 +256,9 @@
    1.41          written += fwrite(value.ptr, 1, value.length, file);
    1.42          written += fwrite("\n", 1, 1, file);
    1.43  
    1.44 -        if (written != k.len + value.length + 4) return 1;
    1.45 +        if (written != k.len + value.length + 4) {
    1.46 +            return 1;
    1.47 +        }
    1.48      }
    1.49  
    1.50      return 0;
     2.1 --- a/ucx/properties.h	Wed Jul 24 14:26:17 2013 +0200
     2.2 +++ b/ucx/properties.h	Mon Aug 05 14:38:37 2013 +0200
     2.3 @@ -25,6 +25,14 @@
     2.4   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     2.5   * POSSIBILITY OF SUCH DAMAGE.
     2.6   */
     2.7 +/**
     2.8 + * @file properties.h
     2.9 + * 
    2.10 + * Load / store utilities for properties files.
    2.11 + * 
    2.12 + * @author Mike Becker
    2.13 + * @author Olaf Wintermann
    2.14 + */
    2.15  
    2.16  #ifndef UCX_PROPERTIES_H
    2.17  #define	UCX_PROPERTIES_H
    2.18 @@ -51,13 +59,53 @@
    2.19  } UcxProperties;
    2.20  
    2.21  
    2.22 +/**
    2.23 + * Constructs a new UcxProperties object.
    2.24 + * @return A pointer to the new UcxProperties object.
    2.25 + */
    2.26  UcxProperties *ucx_properties_new();
    2.27 -void ucx_properties_free(UcxProperties *parser);
    2.28 -void ucx_properties_fill(UcxProperties *parser, char *buf, size_t len);
    2.29 -int ucx_properties_next(UcxProperties *parser, sstr_t *name, sstr_t *value);
    2.30 -int ucx_properties2map(UcxProperties *parser, UcxMap *map);
    2.31 +/**
    2.32 + * Destroys an UcxProperties object.
    2.33 + * @param prop The UcxProperties object to destroy.
    2.34 + */
    2.35 +void ucx_properties_free(UcxProperties *prop);
    2.36 +/**
    2.37 + * Refills the input buffer.
    2.38 + * @param prop The UcxProperties object.
    2.39 + * @param buf A pointer to the new buffer.
    2.40 + * @param len The payload length of the buffer.
    2.41 + */
    2.42 +void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len);
    2.43 +int ucx_properties_next(UcxProperties *prop, sstr_t *name, sstr_t *value);
    2.44 +int ucx_properties2map(UcxProperties *prop, UcxMap *map);
    2.45  
    2.46 +/**
    2.47 + * Loads a properties file to an UcxMap.
    2.48 + * 
    2.49 + * This is a convenience function that reads chunks of 1 KB from an input
    2.50 + * stream until the end of the stream is reached.
    2.51 + * 
    2.52 + * An UcxProperties object is implicitly created and destroyed.
    2.53 + * 
    2.54 + * @param map The map object to write the key/value-pairs to.
    2.55 + * @param file The <code>FILE*</code> stream to read from.
    2.56 + * @return 0 on success, or a non-zero value on error
    2.57 + * 
    2.58 + * @see ucx_properties_fill()
    2.59 + * @see ucx_properties2map()
    2.60 + */
    2.61  int ucx_properties_load(UcxMap *map, FILE *file);
    2.62 +/**
    2.63 + * Stores an UcxMap to a file.
    2.64 + * 
    2.65 + * The key/value-pairs are written by using the following format:
    2.66 + * 
    2.67 + * <code>[key] = [value]\\n</code>
    2.68 + * 
    2.69 + * @param map The map to store.
    2.70 + * @param file The <code>FILE*</code> stream to write to.
    2.71 + * @return 0 on success, or a non-zero value on error
    2.72 + */
    2.73  int ucx_properties_store(UcxMap *map, FILE *file);
    2.74  
    2.75  #ifdef	__cplusplus

mercurial