[cig-commits] r7512 - in cs/cigma/branches/cigma-0.9: build sandbox
sandbox/python/cigma sandbox/sqlite src
luis at geodynamics.org
luis at geodynamics.org
Tue Jun 26 10:07:05 PDT 2007
Author: luis
Date: 2007-06-26 10:07:05 -0700 (Tue, 26 Jun 2007)
New Revision: 7512
Added:
cs/cigma/branches/cigma-0.9/sandbox/python/cigma/converters.py
cs/cigma/branches/cigma-0.9/sandbox/python/cigma/writers.py
cs/cigma/branches/cigma-0.9/sandbox/sqlite/
cs/cigma/branches/cigma-0.9/sandbox/sqlite/Makefile
cs/cigma/branches/cigma-0.9/sandbox/sqlite/ex.c
cs/cigma/branches/cigma-0.9/src/iniparser.c
cs/cigma/branches/cigma-0.9/src/iniparser.h
Modified:
cs/cigma/branches/cigma-0.9/build/Makefile
cs/cigma/branches/cigma-0.9/sandbox/python/cigma/__init__.py
cs/cigma/branches/cigma-0.9/src/README
Log:
darcs patches:
* Now compiling fortran code with -Wall
* Added vtk function to the cigma python module
This function creates an ascii vtk file from a cell-based scalar field
defined on a tetrahedral mesh.
* Added converters to the cigma python module
* Added version number to __init__.py
* Added sqlite example
* Updated makefile for sqlite example in sandbox
* Added -ldl flag to list of libraries to link
* Added -static flag to the makefile rule for compiling the cigma executable
* Removing sqlite3.o from list of dependencies until it is truly needed
* Added parser for .ini files (iniparser.c and iniparser.h)
* Updated acknowledgements section in src/README
* Updated makefile for explicit build
- Fixed circular dependency in rule for argtable2.o
- Added build rule for iniparser.o
Modified: cs/cigma/branches/cigma-0.9/build/Makefile
===================================================================
--- cs/cigma/branches/cigma-0.9/build/Makefile 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/build/Makefile 2007-06-26 17:07:05 UTC (rev 7512)
@@ -14,7 +14,7 @@
CFLAGS = -Wall $(OPTIMIZE)
-LIBS = -lm -lhdf5 -lpthread
+LIBS = -lm -lhdf5 -lpthread -ldl
DC3DLIBS = -lgfortran
TARGETS = libcigma.a libcigma.so cigma
@@ -53,8 +53,13 @@
$(CIGMA)/src/tet4.o \
$(CIGMA)/src/hex8.o
+EXTOBJS = \
+ $(CIGMA)/src/argtable2.o \
+ $(CIGMA)/src/hash.o \
+ $(CIGMA)/src/iniparser.o
+# $(CIGMA)/src/sqlite3.o
+
ARGOBJS = \
- $(CIGMA)/src/argtable2.o \
$(CIGMA)/src/arg_end.o \
$(CIGMA)/src/arg_rem.o \
$(CIGMA)/src/arg_lit.o \
@@ -63,11 +68,6 @@
$(CIGMA)/src/arg_dbl.o \
$(CIGMA)/src/arg_file.o
-EXTOBJS = \
- $(CIGMA)/src/hash.o \
- $(CIGMA)/src/sqlite3.o
-
-
ALLOBJS = $(OBJS) $(ELTOBJS) $(ARGOBJS) $(EXTOBJS)
###############################################################################
@@ -75,7 +75,7 @@
all: $(TARGETS)
cigma: $(CIGMA)/bin/cigma.c libcigma.a libcigma.so
- $(CC) $(LDFLAGS) $(LIBS) $< -o $@
+ $(CC) -static $(CFLAGS) $(LIBS) $< -o $@
libcigma.a: $(ALLOBJS)
ar rcs $@ $^
@@ -93,8 +93,11 @@
$(CIGMA)/src/argtable2.o: $(CIGMA)/src/argtable2.c $(ARGOBJS)
$(CC) $(CFLAGS) -c $< -o $@
+$(CIGMA)/src/iniparser.o: $(CIGMA)/src/iniparser.c
+ $(CC) -c $< -o $@
+
$(CIGMA)/benchmarks/disloc3d/dc3d.o: $(CIGMA)/fields/disloc3d/dc3d.f
- $(FC) -c $< -o $@
+ $(FC) -Wall -c $< -o $@
.c.o:
$(CC) $(CFLAGS) -c $< -o $@
Modified: cs/cigma/branches/cigma-0.9/sandbox/python/cigma/__init__.py
===================================================================
--- cs/cigma/branches/cigma-0.9/sandbox/python/cigma/__init__.py 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/sandbox/python/cigma/__init__.py 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1 @@
+__version__ = '0.9.0'
Added: cs/cigma/branches/cigma-0.9/sandbox/python/cigma/converters.py
===================================================================
--- cs/cigma/branches/cigma-0.9/sandbox/python/cigma/converters.py 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/sandbox/python/cigma/converters.py 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1,190 @@
+import numpy
+
+
+def getchildgroup(file, parent, childname):
+ if childname not in parent:
+ group = file.createGroup(parent, childname)
+ else:
+ group = getattr(parent, childname)
+ return group
+
+def write_h5(filename, modelname, coords, connect, variables):
+ """This method generates files that can be compared with Cigma."""
+ import tables
+
+ h5 = tables.openFile(filename, "a")
+ model = getchildgroup(h5, h5.root, modelname)
+ mesh = getchildgroup(h5, model, "mesh")
+ vars = getchildgroup(h5, model, "variables")
+
+ #
+ # variables example:
+ # - two variables at three different timesteps
+ # - total of six field arrays
+ # - example data
+ # [('displacement', [('0', disp0),
+ # ('5', disp5),
+ # ('10', disp10)]),
+ # ('velocity', [('0', velo0),
+ # ('5', velo5),
+ # ('10', velo10)])]
+ #
+ for (varname, fields) in variables:
+ var = getchildgroup(h5, vars, varname)
+ for fieldname,field in fields:
+ h5.createArray(var, fieldname, field)
+
+ h5.close()
+ return
+
+
+def pylith_h5(filename, meshfile, stepfile, steps, **kwargs):
+ """This method generates files that can be compared with Cigma."""
+
+ # read mesh
+ fp = open(meshfile, 'r')
+ print "Reading", fp.name
+ line = fp.readline()
+ header = map(int, line.split())
+ (nno, nel, a, b, c) = header
+
+ coords = numpy.zeros((nno,3), numpy.float32)
+ connect = numpy.zeros((nno,4), numpy.float32)
+ disp = numpy.zeros((nno,3), numpy.float32)
+
+ for n in xrange(nno):
+ line = fp.readline()
+ cols = line.split()
+ coords[n] = float(cols[1]), float(cols[2]), float(cols[3])
+
+ for e in xrange(nel):
+ line = fp.readline()
+ cols = line.split()
+ connect[e] = int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6])
+
+ fp.close()
+
+ # open file for writing
+ import tables
+ h5 = tables.openFile(filename, "a")
+ modelname = kwargs.get('modelname', "model")
+ model = getchildgroup(h5, h5.root, modelname)
+ mesh = getchildgroup(h5, model, "mesh")
+ vars = getchildgroup(h5, model, "variables")
+ displacement = getchildgroup(h5, vars, "displacement")
+ h5.createArray(mesh, 'coordinates', coords)
+ h5.createArray(mesh, 'connectivity', connect)
+
+ # iterate over timesteps
+ for step in steps:
+
+ # read entire field into disp[] array
+ fp = open(stepfile % step, 'r')
+ print "Reading", fp.name
+ for skip in range(7):
+ fp.readline()
+ for n in xrange(nno):
+ line = fp.readline()
+ cols = line.split()
+ disp[n] = float(cols[1]), float(cols[2]), float(cols[3])
+ fp.close()
+
+ # write out to HDF5 file
+ h5.createArray(displacement, str(step), disp)
+
+ h5.close()
+ return
+
+
+###############################################################################
+# Functions for
+def write_off(filename, coords, connect, scalar_field):
+ """This function creates files that can be compared with TMC.
+ Note that the format is implicitly for scalar fields
+ defined on a tetrahedral mesh.
+ """
+
+ (nno, nsd) = coords.shape
+ (nel, ndim) = connect.shape
+
+ fp = open(filename, 'w')
+ print "Writing", fp.name
+ for n in xrange(nno):
+ fp.write("%g %g %g %g\n" % (coords[n,0],
+ coords[n,1],
+ coords[n,2],
+ scalar_field[n])
+ for e in xrange(nel):
+ fp.write("%d %d %d %d\n" % (connect[e,0],
+ connect[e,1],
+ connect[e,2],
+ connect[e,3])
+ fp.close()
+ return
+
+def pylith_reader(meshfile, stepfile, steps)
+ """This function generates three files for each timestep
+ that can be compared using TMC because it can only compare
+ scalar fields. A better option would be to output a single file
+ per time step, and then modify tmc.cpp to handle vector fields
+ properly."""
+
+ off_file = 'bench.%s.%d.off'
+
+ # read mesh
+ fp = open(meshfile, 'r')
+ print "Reading", fp.name
+ line = fp.readline()
+ header = map(int, line.split())
+ (nno, nel, a, b, c) = header
+
+ coords = numpy.zeros((nno,3), numpy.float32)
+ connect = numpy.zeros((nno,4), numpy.float32)
+ disp = numpy.zeros((nno,3), numpy.float32)
+
+ for n in xrange(nno):
+ line = fp.readline()
+ cols = line.split()
+ coords[n] = float(cols[1]), float(cols[2]), float(cols[3])
+
+ for e in xrange(nel):
+ line = fp.readline()
+ cols = line.split()
+ connect[e] = int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6])
+
+ fp.close()
+
+ # iterate over timesteps
+ for step in steps:
+
+ # read entire field
+ fp = open(stepfile % step, 'r')
+ print "Reading", fp.name
+ for skip in range(7):
+ fp.readline()
+
+ for n in xrange(nno):
+ line = fp.readline()
+ cols = line.split()
+ disp[n] = float(cols[1]), float(cols[2]), float(cols[3])
+ fp.close()
+
+ # write out an off file for each component
+ for i in xrange(3):
+ fp = open(off_file % (step,i), 'w')
+ fp.write("%d %d\n" % (nno,nel))
+ print "Writing", fp.name
+ for n in xrange(nno):
+ fp.write("%g %g %g %g\n" % (coords[n,0],
+ coords[n,1],
+ coords[n,2],
+ disp[n,i])
+ for e in xrange(nel):
+ fp.write("%d %d %d %d\n" % (connect[e,0],
+ connect[e,1],
+ connect[e,2],
+ connect[e,3])
+ fp.close()
+ return
+
+
Added: cs/cigma/branches/cigma-0.9/sandbox/python/cigma/writers.py
===================================================================
--- cs/cigma/branches/cigma-0.9/sandbox/python/cigma/writers.py 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/sandbox/python/cigma/writers.py 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1,37 @@
+
+def tet4_vtk(filename, coords, connect, errors):
+ """Write out cell-based local errors on given tetrahedral mesh"""
+
+ (nno, nsd) = coords.shape
+ (nel, ndof) = connect.shape
+ assert errors.shape[0] == nel
+
+ fp = open(filename, "w")
+ print "Creating", fp.name
+
+ fp.write("# vtk DataFile Version 3.0\n")
+ fp.write("This line is a comment\n")
+ fp.write("ASCII\n")
+
+ fp.write("DATASET UNSTRUCTURED GRID\n")
+
+ fp.write("POINTS %d float\n" % nno)
+ for n in xrange(nno):
+ fp.write("%g %g %g\n" % tuple(coords[n]))
+
+ cellstr = str(ndof) + (" %d" * ndof) + "\n"
+ fp.write("CELLS %d %d\n" % (nel, nel + nel*ndof))
+ for e in xrange(nel):
+ fp.write(cellstr % tuple(connect[e]))
+
+ fp.write("CELL_TYPES %d\n" % nel)
+ for e in xrange(nel):
+ fp.write("10\n")
+
+ fp.write("CELL_DATA %d\n" % nel)
+ fp.write("SCALARS LocalErrors float\n")
+ fp.write("LOOKUP_TABLE default\n")
+ for e in xrange(nel):
+ fp.write("%g\n" % errors[e])
+
+ fp.close()
Added: cs/cigma/branches/cigma-0.9/sandbox/sqlite/Makefile
===================================================================
--- cs/cigma/branches/cigma-0.9/sandbox/sqlite/Makefile 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/sandbox/sqlite/Makefile 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1,31 @@
+CIGMA = ../..
+HDF5_HOME = /usr
+
+CC = gcc
+INCLUDES = -I$(CIGMA)/src
+INCLUDES += -I$(HDF5_HOME)/include
+LIBRARIES = -L$(CIGMA)/build
+LIBRARIES += -L$(HDF5_HOME)/lib
+LDFLAGS += -Wl,--rpath -Wl,$(CIGMA)/build
+LDFLAGS += -Wl,--rpath -Wl,$(HDF5_HOME)/lib
+LIBS = -lcigma -lhdf5 -lpthread -ldl
+
+
+CFLAGS = -Wall $(INCLUDES) $(LIBRARIES) $(LDFLAGS) $(LIBS)
+
+TARGETS = ex
+
+#############################################################################
+
+all: $(TARGETS)
+
+ex: ex.c
+ $(CC) $(CFLAGS) $^ -o $@
+
+ex-static: ex.c
+ $(CC) -static $(CFLAGS) $^ -o $@
+
+clean:
+ rm -f $(TARGETS)
+
+.PHONY: clean
Added: cs/cigma/branches/cigma-0.9/sandbox/sqlite/ex.c
===================================================================
--- cs/cigma/branches/cigma-0.9/sandbox/sqlite/ex.c 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/sandbox/sqlite/ex.c 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1,45 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "sqlite3.h"
+
+static int callback(void *NotUsed, int argc, char **argv, char **azColName)
+{
+ int i;
+ for (i = 0; i < argc; i++)
+ {
+ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+ }
+ printf("\n");
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ sqlite3 *db;
+ char *zErrMsg = 0;
+ int rc;
+
+ if (argc != 3)
+ {
+ fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
+ exit(1);
+ }
+
+ rc = sqlite3_open(argv[1], &db);
+ if (rc)
+ {
+ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
+ sqlite3_close(db);
+ exit(1);
+ }
+
+ rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
+ if (rc != SQLITE_OK)
+ {
+ fprintf(stderr, "SQL error: %s\n", zErrMsg);
+ sqlite3_free(zErrMsg);
+ }
+
+ sqlite3_close(db);
+ return 0;
+}
Modified: cs/cigma/branches/cigma-0.9/src/README
===================================================================
--- cs/cigma/branches/cigma-0.9/src/README 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/src/README 2007-06-26 17:07:05 UTC (rev 7512)
@@ -5,4 +5,5 @@
- attr.h is from PyTable's H5ATTR.c
- hash.c can be obtained from http://www.zentus.com/c/hash.html
- sqlite3.c and sqlite3.h from http://www.sqlite.org/
- - argtable2.c, argtable2.h, and arg_*.c from http://argtable.sf.net/
+ - argtable2.[ch], and arg_*.c from http://argtable.sf.net/
+ - iniparser.[ch] from http://ndevilla.free.fr/iniparser/
Added: cs/cigma/branches/cigma-0.9/src/iniparser.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/iniparser.c 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/src/iniparser.c 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1,989 @@
+/*
+ Based upon libiniparser, by Nicolas Devillard
+ Hacked into 1 file (m-iniparser) by Freek/2005
+ Original terms following:
+
+ -- -
+
+ Copyright (c) 2000 by Nicolas Devillard (ndevilla AT free DOT fr).
+
+ Written by Nicolas Devillard. Not derived from licensed software.
+
+ Permission is granted to anyone to use this software for any
+ purpose on any computer system, and to redistribute it freely,
+ subject to the following restrictions:
+
+ 1. The author is not responsible for the consequences of use of
+ this software, no matter how awful, even if they arise
+ from defects in it.
+
+ 2. The origin of this software must not be misrepresented, either
+ by explicit claim or by omission.
+
+ 3. Altered versions must be plainly marked as such, and must not
+ be misrepresented as being the original software.
+
+ 4. This notice may not be removed or altered.
+
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "iniparser.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* strlib.c following */
+
+#define ASCIILINESZ 1024
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Convert a string to lowercase.
+ @param s String to convert.
+ @return ptr to statically allocated string.
+
+ This function returns a pointer to a statically allocated string
+ containing a lowercased version of the input string. Do not free
+ or modify the returned string! Since the returned string is statically
+ allocated, it will be modified at each function call (not re-entrant).
+ */
+/*--------------------------------------------------------------------------*/
+
+static char * strlwc(char * s)
+{
+ static char l[ASCIILINESZ+1];
+ int i ;
+
+ if (s==NULL) return NULL ;
+ memset(l, 0, ASCIILINESZ+1);
+ i=0 ;
+ while (s[i] && i<ASCIILINESZ) {
+ l[i] = (char)tolower((int)s[i]);
+ i++ ;
+ }
+ l[ASCIILINESZ]=(char)0;
+ return l ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Convert a string to uppercase.
+ @param s String to convert.
+ @return ptr to statically allocated string.
+
+ This function returns a pointer to a statically allocated string
+ containing an uppercased version of the input string. Do not free
+ or modify the returned string! Since the returned string is statically
+ allocated, it will be modified at each function call (not re-entrant).
+ */
+/*--------------------------------------------------------------------------*/
+
+static char * strupc(char * s)
+{
+ static char l[ASCIILINESZ+1];
+ int i ;
+
+ if (s==NULL) return NULL ;
+ memset(l, 0, ASCIILINESZ+1);
+ i=0 ;
+ while (s[i] && i<ASCIILINESZ) {
+ l[i] = (char)toupper((int)s[i]);
+ i++ ;
+ }
+ l[ASCIILINESZ]=(char)0;
+ return l ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Skip blanks until the first non-blank character.
+ @param s String to parse.
+ @return Pointer to char inside given string.
+
+ This function returns a pointer to the first non-blank character in the
+ given string.
+ */
+/*--------------------------------------------------------------------------*/
+
+static char * strskp(char * s)
+{
+ char * skip = s;
+ if (s==NULL) return NULL ;
+ while (isspace((int)*skip) && *skip) skip++;
+ return skip ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Remove blanks at the end of a string.
+ @param s String to parse.
+ @return ptr to statically allocated string.
+
+ This function returns a pointer to a statically allocated string,
+ which is identical to the input string, except that all blank
+ characters at the end of the string have been removed.
+ Do not free or modify the returned string! Since the returned string
+ is statically allocated, it will be modified at each function call
+ (not re-entrant).
+ */
+/*--------------------------------------------------------------------------*/
+
+static char * strcrop(char * s)
+{
+ static char l[ASCIILINESZ+1];
+ char * last ;
+
+ if (s==NULL) return NULL ;
+ memset(l, 0, ASCIILINESZ+1);
+ strcpy(l, s);
+ last = l + strlen(l);
+ while (last > l) {
+ if (!isspace((int)*(last-1)))
+ break ;
+ last -- ;
+ }
+ *last = (char)0;
+ return l ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Remove blanks at the beginning and the end of a string.
+ @param s String to parse.
+ @return ptr to statically allocated string.
+
+ This function returns a pointer to a statically allocated string,
+ which is identical to the input string, except that all blank
+ characters at the end and the beg. of the string have been removed.
+ Do not free or modify the returned string! Since the returned string
+ is statically allocated, it will be modified at each function call
+ (not re-entrant).
+ */
+/*--------------------------------------------------------------------------*/
+static char * strstrip(char * s)
+{
+ static char l[ASCIILINESZ+1];
+ char * last ;
+
+ if (s==NULL) return NULL ;
+
+ while (isspace((int)*s) && *s) s++;
+
+ memset(l, 0, ASCIILINESZ+1);
+ strcpy(l, s);
+ last = l + strlen(l);
+ while (last > l) {
+ if (!isspace((int)*(last-1)))
+ break ;
+ last -- ;
+ }
+ *last = (char)0;
+
+ return (char*)l ;
+}
+
+
+/* dictionary.c.c following */
+/** Maximum value size for integers and doubles. */
+#define MAXVALSZ 1024
+
+/** Minimal allocated number of entries in a dictionary */
+#define DICTMINSZ 128
+
+/** Invalid key token */
+#define DICT_INVALID_KEY ((char*)-1)
+
+/*
+ Doubles the allocated size associated to a pointer
+ 'size' is the current allocated size.
+ */
+static void * mem_double(void * ptr, int size)
+{
+ void *newptr;
+
+ newptr = calloc(2*size, 1);
+ memcpy(newptr, ptr, size);
+ free(ptr);
+ return newptr ;
+}
+
+
+/*---------------------------------------------------------------------------
+ Function codes
+ ---------------------------------------------------------------------------*/
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Compute the hash key for a string.
+ @param key Character string to use for key.
+ @return 1 unsigned int on at least 32 bits.
+
+ This hash function has been taken from an Article in Dr Dobbs Journal.
+ This is normally a collision-free function, distributing keys evenly.
+ The key is stored anyway in the struct so that collision can be avoided
+ by comparing the key itself in last resort.
+ */
+/*--------------------------------------------------------------------------*/
+
+static unsigned dictionary_hash(char * key)
+{
+ int len ;
+ unsigned hash ;
+ int i ;
+
+ len = strlen(key);
+ for (hash=0, i=0 ; i<len ; i++) {
+ hash += (unsigned)key[i] ;
+ hash += (hash<<10);
+ hash ^= (hash>>6) ;
+ }
+ hash += (hash <<3);
+ hash ^= (hash >>11);
+ hash += (hash <<15);
+ return hash ;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Create a new dictionary object.
+ @param size Optional initial size of the dictionary.
+ @return 1 newly allocated dictionary objet.
+
+ This function allocates a new dictionary object of given size and returns
+ it. If you do not know in advance (roughly) the number of entries in the
+ dictionary, give size=0.
+ */
+/*--------------------------------------------------------------------------*/
+
+static dictionary * dictionary_new(int size)
+{
+ dictionary *d ;
+
+ /* If no size was specified, allocate space for DICTMINSZ */
+ if (size<DICTMINSZ) size=DICTMINSZ ;
+
+ d = (dictionary *)calloc(1, sizeof(dictionary));
+ d->size = size ;
+ d->val = (char **)calloc(size, sizeof(char*));
+ d->key = (char **)calloc(size, sizeof(char*));
+ d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
+
+ return d;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Delete a dictionary object
+ @param d dictionary object to deallocate.
+ @return void
+
+ Deallocate a dictionary object and all memory associated to it.
+ */
+/*--------------------------------------------------------------------------*/
+
+static void dictionary_del(dictionary * d)
+{
+ int i ;
+
+ if (d==NULL) return ;
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]!=NULL)
+ free(d->key[i]);
+ if (d->val[i]!=NULL)
+ free(d->val[i]);
+ }
+ free(d->val);
+ free(d->key);
+ free(d->hash);
+ free(d);
+
+ return;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get a value from a dictionary.
+ @param d dictionary object to search.
+ @param key Key to look for in the dictionary.
+ @param def Default value to return if key not found.
+ @return 1 pointer to internally allocated character string.
+
+ This function locates a key in a dictionary and returns a pointer to its
+ value, or the passed 'def' pointer if no such key can be found in
+ dictionary. The returned character pointer points to data internal to the
+ dictionary object, you should not try to free it or modify it.
+ */
+/*--------------------------------------------------------------------------*/
+static char * dictionary_get(dictionary * d, char * key, char * def)
+{
+ unsigned hash ;
+ int i ;
+
+ hash = dictionary_hash(key);
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key==NULL)
+ continue ;
+ /* Compare hash */
+ if (hash==d->hash[i]) {
+ /* Compare string, to avoid hash collisions */
+ if (!strcmp(key, d->key[i])) {
+ return d->val[i] ;
+ }
+ }
+ }
+ return def ;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Set a value in a dictionary.
+ @param d dictionary object to modify.
+ @param key Key to modify or add.
+ @param val Value to add.
+ @return void
+
+ If the given key is found in the dictionary, the associated value is
+ replaced by the provided one. If the key cannot be found in the
+ dictionary, it is added to it.
+
+ It is Ok to provide a NULL value for val, but NULL values for the dictionary
+ or the key are considered as errors: the function will return immediately
+ in such a case.
+
+ Notice that if you dictionary_set a variable to NULL, a call to
+ dictionary_get will return a NULL value: the variable will be found, and
+ its value (NULL) is returned. In other words, setting the variable
+ content to NULL is equivalent to deleting the variable from the
+ dictionary. It is not possible (in this implementation) to have a key in
+ the dictionary without value.
+ */
+/*--------------------------------------------------------------------------*/
+
+static void dictionary_set(dictionary * d, char * key, char * val)
+{
+ int i ;
+ unsigned hash ;
+
+ if (d==NULL || key==NULL) return ;
+
+ /* Compute hash for this key */
+ hash = dictionary_hash(key) ;
+ /* Find if value is already in blackboard */
+ if (d->n>0) {
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]==NULL)
+ continue ;
+ if (hash==d->hash[i]) { /* Same hash value */
+ if (!strcmp(key, d->key[i])) { /* Same key */
+ /* Found a value: modify and return */
+ if (d->val[i]!=NULL)
+ free(d->val[i]);
+ d->val[i] = val ? strdup(val) : NULL ;
+ /* Value has been modified: return */
+ return ;
+ }
+ }
+ }
+ }
+ /* Add a new value */
+ /* See if dictionary needs to grow */
+ if (d->n==d->size) {
+
+ /* Reached maximum size: reallocate blackboard */
+ d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
+ d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
+ d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
+
+ /* Double size */
+ d->size *= 2 ;
+ }
+
+ /* Insert key in the first empty slot */
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]==NULL) {
+ /* Add key here */
+ break ;
+ }
+ }
+ /* Copy key */
+ d->key[i] = strdup(key);
+ d->val[i] = val ? strdup(val) : NULL ;
+ d->hash[i] = hash;
+ d->n ++ ;
+ return ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Delete a key in a dictionary
+ @param d dictionary object to modify.
+ @param key Key to remove.
+ @return void
+
+ This function deletes a key in a dictionary. Nothing is done if the
+ key cannot be found.
+ */
+/*--------------------------------------------------------------------------*/
+static void dictionary_unset(dictionary * d, char * key)
+{
+ unsigned hash ;
+ int i ;
+
+ hash = dictionary_hash(key);
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]==NULL)
+ continue ;
+ /* Compare hash */
+ if (hash==d->hash[i]) {
+ /* Compare string, to avoid hash collisions */
+ if (!strcmp(key, d->key[i])) {
+ /* Found key */
+ break ;
+ }
+ }
+ }
+ if (i>=d->size)
+ /* Key not found */
+ return ;
+
+ free(d->key[i]);
+ d->key[i] = NULL ;
+ if (d->val[i]!=NULL) {
+ free(d->val[i]);
+ d->val[i] = NULL ;
+ }
+ d->hash[i] = 0 ;
+ d->n -- ;
+ return ;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Dump a dictionary to an opened file pointer.
+ @param d Dictionary to dump
+ @param f Opened file pointer.
+ @return void
+
+ Dumps a dictionary onto an opened file pointer. Key pairs are printed out
+ as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
+ output file pointers.
+ */
+/*--------------------------------------------------------------------------*/
+
+static void dictionary_dump(dictionary *d, FILE *f)
+{
+ int i;
+
+ if (d==NULL || f==NULL) return;
+
+ for (i=0; i<d->size; i++) {
+ if (d->key[i] == NULL)
+ continue ;
+ if (d->val[i] != NULL) {
+ fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
+ } else {
+ fprintf(f, "[%s]=UNDEF\n", d->key[i]);
+ }
+ }
+
+ return;
+}
+
+
+/* iniparser.c.c following */
+#define ASCIILINESZ 1024
+#define INI_INVALID_KEY ((char*)-1)
+
+/* Private: add an entry to the dictionary */
+static void iniparser_add_entry(
+ dictionary * d,
+ char * sec,
+ char * key,
+ char * val)
+{
+ char longkey[2*ASCIILINESZ+1];
+
+ /* Make a key as section:keyword */
+ if (key!=NULL) {
+ sprintf(longkey, "%s:%s", sec, key);
+ } else {
+ strcpy(longkey, sec);
+ }
+
+ /* Add (key,val) to dictionary */
+ dictionary_set(d, longkey, val);
+ return ;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get number of sections in a dictionary
+ @param d Dictionary to examine
+ @return int Number of sections found in dictionary
+
+ This function returns the number of sections found in a dictionary.
+ The test to recognize sections is done on the string stored in the
+ dictionary: a section name is given as "section" whereas a key is
+ stored as "section:key", thus the test looks for entries that do not
+ contain a colon.
+
+ This clearly fails in the case a section name contains a colon, but
+ this should simply be avoided.
+
+ This function returns -1 in case of error.
+ */
+/*--------------------------------------------------------------------------*/
+
+int iniparser_getnsec(dictionary * d)
+{
+ int i ;
+ int nsec ;
+
+ if (d==NULL) return -1 ;
+ nsec=0 ;
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]==NULL)
+ continue ;
+ if (strchr(d->key[i], ':')==NULL) {
+ nsec ++ ;
+ }
+ }
+ return nsec ;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get name for section n in a dictionary.
+ @param d Dictionary to examine
+ @param n Section number (from 0 to nsec-1).
+ @return Pointer to char string
+
+ This function locates the n-th section in a dictionary and returns
+ its name as a pointer to a string statically allocated inside the
+ dictionary. Do not free or modify the returned string!
+
+ This function returns NULL in case of error.
+ */
+/*--------------------------------------------------------------------------*/
+
+char * iniparser_getsecname(dictionary * d, int n)
+{
+ int i ;
+ int foundsec ;
+
+ if (d==NULL || n<0) return NULL ;
+ foundsec=0 ;
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]==NULL)
+ continue ;
+ if (strchr(d->key[i], ':')==NULL) {
+ foundsec++ ;
+ if (foundsec>n)
+ break ;
+ }
+ }
+ if (foundsec<=n) {
+ return NULL ;
+ }
+ return d->key[i] ;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Dump a dictionary to an opened file pointer.
+ @param d Dictionary to dump.
+ @param f Opened file pointer to dump to.
+ @return void
+
+ This function prints out the contents of a dictionary, one element by
+ line, onto the provided file pointer. It is OK to specify @c stderr
+ or @c stdout as output files. This function is meant for debugging
+ purposes mostly.
+ */
+/*--------------------------------------------------------------------------*/
+void iniparser_dump(dictionary * d, FILE * f)
+{
+ dictionary_dump(d,f);
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Save a dictionary to a loadable ini file
+ @param d Dictionary to dump
+ @param f Opened file pointer to dump to
+ @return void
+
+ This function dumps a given dictionary into a loadable ini file.
+ It is Ok to specify @c stderr or @c stdout as output files.
+ */
+/*--------------------------------------------------------------------------*/
+
+void iniparser_dump_ini(dictionary * d, FILE * f)
+{
+ int i, j ;
+ char keym[ASCIILINESZ+1];
+ int nsec ;
+ char * secname ;
+ int seclen ;
+
+ if (d==NULL || f==NULL) return ;
+
+ nsec = iniparser_getnsec(d);
+ if (nsec<1) {
+ /* No section in file: dump all keys as they are */
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]==NULL)
+ continue ;
+ fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
+ }
+ return ;
+ }
+ for (i=0 ; i<nsec ; i++) {
+ secname = iniparser_getsecname(d, i) ;
+ seclen = (int)strlen(secname);
+ fprintf(f, "\n[%s]\n", secname);
+ sprintf(keym, "%s:", secname);
+ for (j=0 ; j<d->size ; j++) {
+ if (d->key[j]==NULL)
+ continue ;
+ if (!strncmp(d->key[j], keym, seclen+1)) {
+ fprintf(f,
+ "%-30s = %s\n",
+ d->key[j]+seclen+1,
+ d->val[j] ? d->val[j] : "");
+ }
+ }
+ }
+ fprintf(f, "\n");
+ return ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, return NULL if not found
+ @param d Dictionary to search
+ @param key Key string to look for
+ @return pointer to statically allocated character string, or NULL.
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ NULL is returned.
+ The returned char pointer is pointing to a string allocated in
+ the dictionary, do not free or modify it.
+
+ This function is only provided for backwards compatibility with
+ previous versions of iniparser. It is recommended to use
+ iniparser_getstring() instead.
+ */
+/*--------------------------------------------------------------------------*/
+char * iniparser_getstr(dictionary * d, char * key)
+{
+ return iniparser_getstring(d, key, NULL);
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param def Default value to return if key not found.
+ @return pointer to statically allocated character string
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the pointer passed as 'def' is returned.
+ The returned char pointer is pointing to a string allocated in
+ the dictionary, do not free or modify it.
+ */
+/*--------------------------------------------------------------------------*/
+char * iniparser_getstring(dictionary * d, char * key, char * def)
+{
+ char * lc_key ;
+ char * sval ;
+
+ if (d==NULL || key==NULL)
+ return def ;
+
+ lc_key = strdup(strlwc(key));
+ sval = dictionary_get(d, lc_key, def);
+ free(lc_key);
+ return sval ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to an int
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+ */
+/*--------------------------------------------------------------------------*/
+int iniparser_getint(dictionary * d, char * key, int notfound)
+{
+ char * str ;
+
+ str = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (str==INI_INVALID_KEY) return notfound ;
+ return atoi(str);
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to a double
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return double
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+ */
+/*--------------------------------------------------------------------------*/
+double iniparser_getdouble(dictionary * d, char * key, double notfound)
+{
+ char * str ;
+
+ str = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (str==INI_INVALID_KEY) return notfound ;
+ return atof(str);
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to a boolean
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+
+ A true boolean is found if one of the following is matched:
+
+ - A string starting with 'y'
+ - A string starting with 'Y'
+ - A string starting with 't'
+ - A string starting with 'T'
+ - A string starting with '1'
+
+ A false boolean is found if one of the following is matched:
+
+ - A string starting with 'n'
+ - A string starting with 'N'
+ - A string starting with 'f'
+ - A string starting with 'F'
+ - A string starting with '0'
+
+ The notfound value returned if no boolean is identified, does not
+ necessarily have to be 0 or 1.
+ */
+/*--------------------------------------------------------------------------*/
+int iniparser_getboolean(dictionary * d, char * key, int notfound)
+{
+ char * c ;
+ int ret ;
+
+ c = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (c==INI_INVALID_KEY) return notfound ;
+ if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
+ ret = 1 ;
+ } else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
+ ret = 0 ;
+ } else {
+ ret = notfound ;
+ }
+ return ret;
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Finds out if a given entry exists in a dictionary
+ @param ini Dictionary to search
+ @param entry Name of the entry to look for
+ @return integer 1 if entry exists, 0 otherwise
+
+ Finds out if a given entry exists in the dictionary. Since sections
+ are stored as keys with NULL associated values, this is the only way
+ of querying for the presence of sections in a dictionary.
+ */
+/*--------------------------------------------------------------------------*/
+
+int iniparser_find_entry(
+ dictionary * ini,
+ char * entry
+)
+{
+ int found=0 ;
+ if (iniparser_getstring(ini, entry, INI_INVALID_KEY)!=INI_INVALID_KEY) {
+ found = 1 ;
+ }
+ return found ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Set an entry in a dictionary.
+ @param ini Dictionary to modify.
+ @param entry Entry to modify (entry name)
+ @param val New value to associate to the entry.
+ @return int 0 if Ok, -1 otherwise.
+
+ If the given entry can be found in the dictionary, it is modified to
+ contain the provided value. If it cannot be found, -1 is returned.
+ It is Ok to set val to NULL.
+ */
+/*--------------------------------------------------------------------------*/
+
+int iniparser_setstr(dictionary * ini, char * entry, char * val)
+{
+ dictionary_set(ini, strlwc(entry), val);
+ return 0 ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Delete an entry in a dictionary
+ @param ini Dictionary to modify
+ @param entry Entry to delete (entry name)
+ @return void
+
+ If the given entry can be found, it is deleted from the dictionary.
+ */
+/*--------------------------------------------------------------------------*/
+void iniparser_unset(dictionary * ini, char * entry)
+{
+ dictionary_unset(ini, strlwc(entry));
+}
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Parse an ini file and return an allocated dictionary object
+ @param ininame Name of the ini file to read.
+ @return Pointer to newly allocated dictionary
+
+ This is the parser for ini files. This function is called, providing
+ the name of the file to be read. It returns a dictionary object that
+ should not be accessed directly, but through accessor functions
+ instead.
+
+ The returned dictionary must be freed using iniparser_free().
+ */
+/*--------------------------------------------------------------------------*/
+
+dictionary * iniparser_new(char *ininame)
+{
+ dictionary * d ;
+ char lin[ASCIILINESZ+1];
+ char sec[ASCIILINESZ+1];
+ char key[ASCIILINESZ+1];
+ char val[ASCIILINESZ+1];
+ char * where ;
+ FILE * ini ;
+ int lineno ;
+
+ if ((ini=fopen(ininame, "r"))==NULL) {
+ return NULL ;
+ }
+
+ sec[0]=0;
+
+ /*
+ * Initialize a new dictionary entry
+ */
+ d = dictionary_new(0);
+ lineno = 0 ;
+ while (fgets(lin, ASCIILINESZ, ini)!=NULL) {
+ lineno++ ;
+ where = strskp(lin); /* Skip leading spaces */
+ if (*where==';' || *where=='#' || *where==0)
+ continue ; /* Comment lines */
+ else {
+ if (sscanf(where, "[%[^]]", sec)==1) {
+ /* Valid section name */
+ strcpy(sec, strlwc(sec));
+ iniparser_add_entry(d, sec, NULL, NULL);
+ } else if (sscanf (where, "%[^=] = \"%[^\"]\"", key, val) == 2
+ || sscanf (where, "%[^=] = '%[^\']'", key, val) == 2
+ || sscanf (where, "%[^=] = %[^;#]", key, val) == 2) {
+ strcpy(key, strlwc(strcrop(key)));
+ /*
+ * sscanf cannot handle "" or '' as empty value,
+ * this is done here
+ */
+ if (!strcmp(val, "\"\"") || !strcmp(val, "''")) {
+ val[0] = (char)0;
+ } else {
+ strcpy(val, strcrop(val));
+ }
+ iniparser_add_entry(d, sec, key, val);
+ }
+ }
+ }
+ fclose(ini);
+ return d ;
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Free all memory associated to an ini dictionary
+ @param d Dictionary to free
+ @return void
+
+ Free all memory associated to an ini dictionary.
+ It is mandatory to call this function before the dictionary object
+ gets out of the current context.
+ */
+/*--------------------------------------------------------------------------*/
+
+void iniparser_free(dictionary * d)
+{
+ dictionary_del(d);
+}
+
+#ifdef __cplusplus
+}
+#endif
Added: cs/cigma/branches/cigma-0.9/src/iniparser.h
===================================================================
--- cs/cigma/branches/cigma-0.9/src/iniparser.h 2007-06-26 17:01:45 UTC (rev 7511)
+++ cs/cigma/branches/cigma-0.9/src/iniparser.h 2007-06-26 17:07:05 UTC (rev 7512)
@@ -0,0 +1,82 @@
+/*
+ Based upon libiniparser, by Nicolas Devillard
+ Hacked into 1 file (m-iniparser) by Freek/2005
+ Original terms following:
+
+ -- -
+
+ Copyright (c) 2000 by Nicolas Devillard (ndevilla AT free DOT fr).
+
+ Written by Nicolas Devillard. Not derived from licensed software.
+
+ Permission is granted to anyone to use this software for any
+ purpose on any computer system, and to redistribute it freely,
+ subject to the following restrictions:
+
+ 1. The author is not responsible for the consequences of use of
+ this software, no matter how awful, even if they arise
+ from defects in it.
+
+ 2. The origin of this software must not be misrepresented, either
+ by explicit claim or by omission.
+
+ 3. Altered versions must be plainly marked as such, and must not
+ be misrepresented as being the original software.
+
+ 4. This notice may not be removed or altered.
+
+ */
+
+
+#ifndef _INIPARSER_H_
+#define _INIPARSER_H_
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+typedef struct _dictionary_ {
+ /** Number of entries in dictionary */
+ int n;
+ /** Storage size */
+ int size;
+ /** List of string values */
+ char **val;
+ /** List of string keys */
+ char **key ;
+ /** List of hash values for keys */
+ unsigned *hash;
+} dictionary ;
+
+
+/* generated by genproto */
+
+dictionary * iniparser_new(char *ininame);
+void iniparser_free(dictionary * d);
+
+int iniparser_getnsec(dictionary * d);
+char * iniparser_getsecname(dictionary * d, int n);
+void iniparser_dump(dictionary * d, FILE * f);
+void iniparser_dump_ini(dictionary * d, FILE * f);
+char * iniparser_getkey(dictionary *d, char *section, char *key);
+char * iniparser_getstr(dictionary * d, char * key);
+char * iniparser_getstring(dictionary * d, char * key, char * def);
+int iniparser_getint(dictionary * d, char * key, int notfound);
+double iniparser_getdouble(dictionary * d, char * key, double notfound);
+int iniparser_getboolean(dictionary * d, char * key, int notfound);
+int iniparser_find_entry(dictionary * ini, char * entry);
+int iniparser_setstr(dictionary * ini, char * entry, char * val);
+void iniparser_unset(dictionary * ini, char * entry);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
More information about the cig-commits
mailing list