each option's values are string arrays and can be converted to int if they
authorEleni Maria Stea <elene.mst@gmail.com>
Tue, 12 Feb 2013 16:18:59 +0000 (18:18 +0200)
committerEleni Maria Stea <elene.mst@gmail.com>
Tue, 12 Feb 2013 16:18:59 +0000 (18:18 +0200)
contain numerical values

src/cfg.c
src/cfg.h
src/gliar.c

index 124449e..50c4f9c 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -11,11 +11,11 @@ the Free Software Foundation, either version 3 of the License, or
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+along with this program.       If not, see <http://www.gnu.org/licenses/>.
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
@@ -27,7 +27,7 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 #include "cfg.h"
 
 static char *stripspace(char *s);
 #include "cfg.h"
 
 static char *stripspace(char *s);
-
+static void concat_values(struct cfgopt *opt);
 
 struct cfgopt *gliar_load_cfg(const char *fname)
 {
 
 struct cfgopt *gliar_load_cfg(const char *fname)
 {
@@ -59,46 +59,58 @@ struct cfgopt *gliar_load_cfg(const char *fname)
                        if(opt) {
                                opt->next = optlist;
                                optlist = opt;
                        if(opt) {
                                opt->next = optlist;
                                optlist = opt;
+                               concat_values(opt);
                        }
 
                        if((opt = malloc(sizeof *opt))) {
                                if((opt->key = malloc(strlen(line) + 1))) {
                                        strcpy(opt->key, line);
                                        opt->str_val = 0;
                        }
 
                        if((opt = malloc(sizeof *opt))) {
                                if((opt->key = malloc(strlen(line) + 1))) {
                                        strcpy(opt->key, line);
                                        opt->str_val = 0;
-          opt->num_val = 0;
-          opt->type = unknown;
+                                       opt->num_val = 0;
+                                       opt->str_count = 0;
+                                       opt->type = GLIAR_STRING;
                                } else {
                                        free(opt);
                                        opt = 0;
                                }
                        }
                } else {
                                } else {
                                        free(opt);
                                        opt = 0;
                                }
                        }
                } else {
-                       char *tmp;
-                       int prev_len = opt->str_val ? strlen(opt->str_val) : 0;
+                       int new_sz = opt->str_count + 1;
+                       char **tmp;
+
+                       char *end;
+                       int num = strtol(line, &end, 10);
 
 
-                       if(opt && (tmp = realloc(opt->str_val, prev_len + strlen(line) + 2))) {
-        opt->type = str;
+                       if(!*end) {
+                               opt->num_val = line;
+                               opt->type = GLIAR_NUMBER;
+                       }
+
+                       if(opt && (tmp = realloc(opt->str_val, new_sz * sizeof(char*)))) {
                                opt->str_val = tmp;
                                opt->str_val = tmp;
-                               if(prev_len) {
-                                       strcat(opt->str_val, " ");
-                                       strcat(opt->str_val, line);
-                               } else {
-                                       strcpy(opt->str_val, line);
+                               if((opt->str_val[new_sz - 1] = malloc(strlen(line) + 1))) {
+                                       strcpy(opt->str_val[new_sz -1], line);
+                                       opt->str_count = new_sz;
                                }
                        }
                                }
                        }
+
+                       if(new_sz > 1) {
+                               opt->type = GLIAR_STRING;
+                       }
                }
        }
 
        if(opt) {
                opt->next = optlist;
                optlist = opt;
                }
        }
 
        if(opt) {
                opt->next = optlist;
                optlist = opt;
+               concat_values(opt);
        }
 
        fclose(fp);
        return optlist;
 }
 
        }
 
        fclose(fp);
        return optlist;
 }
 
-const char *gliar_find_opt(struct cfgopt *list, const char *name)
+const struct cfgopt *gliar_find_opt(struct cfgopt *list, const char *name)
 {
        if(!list || !name) {
                return 0;
 {
        if(!list || !name) {
                return 0;
@@ -106,7 +118,7 @@ const char *gliar_find_opt(struct cfgopt *list, const char *name)
 
        while(list) {
                if(strcmp(list->key, name) == 0) {
 
        while(list) {
                if(strcmp(list->key, name) == 0) {
-                       return list->str_val;
+                       return list;
                }
                list = list->next;
        }
                }
                list = list->next;
        }
@@ -117,7 +129,15 @@ void gliar_print_opt(struct cfgopt *list)
 {
        printf("OPTIONS\n");
        while(list) {
 {
        printf("OPTIONS\n");
        while(list) {
-               printf("\"%s\" -> \"%s\"\n", list->key, list->str_val);
+               if(list->type == GLIAR_NUMBER) {
+                       printf("\"%s\" -> %d\n", list->key, list->num_val);
+               }
+               else {
+                       int i;
+                       for(i=0; i<list->str_count; i++) {
+                               printf("\"%s\" -> \"%s\"\n", list->key, list->str_val[i]);
+                       }
+               }
                list = list->next;
        }
 }
                list = list->next;
        }
 }
@@ -133,3 +153,24 @@ static char *stripspace(char *s)
        }
        return s;
 }
        }
        return s;
 }
+
+static void concat_values(struct cfgopt *opt)
+{
+       int i;
+       int sz = opt->str_count - 1;
+
+       for(i=0; i<opt->str_count; i++) {
+               sz += strlen(opt->str_val[i]);
+       }
+
+       if(!(opt->conc_vals = malloc(sz + 1))) {
+               return;
+       }
+
+       *opt->conc_vals = 0;
+       for(i=0; i<opt->str_count - 1; i++) {
+               strcat(opt->conc_vals, opt->str_val[i]);
+               strcat(opt->conc_vals, " ");
+       }
+       strcat(opt->conc_vals, opt->str_val[i]);
+}
index 4f18d2d..c665782 100644 (file)
--- a/src/cfg.h
+++ b/src/cfg.h
@@ -11,11 +11,11 @@ the Free Software Foundation, either version 3 of the License, or
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+along with this program.       If not, see <http://www.gnu.org/licenses/>.
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
@@ -23,21 +23,24 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 #ifndef CFG_H_
 #define CFG_H_
 
 #ifndef CFG_H_
 #define CFG_H_
 
-enum TYPE {unknown, flt, dbl, boolean, integer, str};
+enum type {GLIAR_NUMBER, GLIAR_STRING};
 
 struct cfgopt {
        char *key;
 
 struct cfgopt {
        char *key;
-       char *str_val;
+       char **str_val;
+       int str_count;
 
 
-  double num_val;
-  enum TYPE type;
+       char* conc_vals;
+
+       int num_val;
+       enum type type;
 
        struct cfgopt *next;
 };
 
 struct cfgopt *gliar_load_cfg(const char *fname);
 
 
        struct cfgopt *next;
 };
 
 struct cfgopt *gliar_load_cfg(const char *fname);
 
-const char *gliar_find_opt(struct cfgopt *list, const char *name);
+const struct cfgopt *gliar_find_opt(struct cfgopt *list, const char *name);
 
 void gliar_print_opt(struct cfgopt *list);
 
 
 void gliar_print_opt(struct cfgopt *list);
 
index 0508a10..1173a43 100644 (file)
@@ -11,11 +11,11 @@ the Free Software Foundation, either version 3 of the License, or
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+along with this program.       If not, see <http://www.gnu.org/licenses/>.
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
@@ -38,7 +38,7 @@ static const void* (*gl_get_booleanv)(GLenum, GLboolean*);
 static const void* (*gl_get_doublev)(GLenum, GLdouble*);
 static const void* (*gl_get_floatv)(GLenum, GLfloat*);
 static const void* (*gl_get_integerv)(GLenum, GLint*);
 static const void* (*gl_get_doublev)(GLenum, GLdouble*);
 static const void* (*gl_get_floatv)(GLenum, GLfloat*);
 static const void* (*gl_get_integerv)(GLenum, GLint*);
-static const void* (*gl_get_integer64v)(GLenum, GLint64*); 
+static const void* (*gl_get_integer64v)(GLenum, GLint64*);
 
 static const void* (*gl_get_booleani_v)(GLenum, GLuint, GLboolean*);
 static const void* (*gl_get_doublei_v)(GLenum, GLuint, GLdouble*);
 
 static const void* (*gl_get_booleani_v)(GLenum, GLuint, GLboolean*);
 static const void* (*gl_get_doublei_v)(GLenum, GLuint, GLdouble*);
@@ -73,19 +73,19 @@ static int init(void)
        }
 
        gl_get_string = dlsym(RTLD_NEXT, "glGetString");
        }
 
        gl_get_string = dlsym(RTLD_NEXT, "glGetString");
-/*  gl_get_stringi = dlsym(RTLD_NEXT, "glGetStringi");
+/*     gl_get_stringi = dlsym(RTLD_NEXT, "glGetStringi");
 
 
-  gl_get_booleanv = dlsym(RTLD_NEXT, "glGetBooleanv");
-  gl_get_doublev = dlsym(RTLD_NEXT, "glGetDoublev");
-  gl_get_floatv = dlsym(RTLD_NEXT, "glGetFloatv");
-  gl_get_integerv = dlsym(RTLD_NEXT, "glGetIntegerv");
-  gl_get_integer64v = dlsym(RTLD_NEXT, "glGetInteger64v");
+       gl_get_booleanv = dlsym(RTLD_NEXT, "glGetBooleanv");
+       gl_get_doublev = dlsym(RTLD_NEXT, "glGetDoublev");
+       gl_get_floatv = dlsym(RTLD_NEXT, "glGetFloatv");
+       gl_get_integerv = dlsym(RTLD_NEXT, "glGetIntegerv");
+       gl_get_integer64v = dlsym(RTLD_NEXT, "glGetInteger64v");
 
 
-  gl_get_booleani_v = dlsym(RTLD_NEXT, "glGetBooleani_v");
-  gl_get_doublei_v = dlsym(RTLD_NEXT, "glGetDoublei_v");
-  gl_get_floati_v = dlsym(RTLD_NEXT, "glGetFloati_v");
-  gl_get_integeri_v = dlsym(RTLD_NEXT, "glGetIntegeri_v");
-  gl_get_integer64i_v = dlsym(RTLD_NEXT, "glGetInteger64i_v");*/
+       gl_get_booleani_v = dlsym(RTLD_NEXT, "glGetBooleani_v");
+       gl_get_doublei_v = dlsym(RTLD_NEXT, "glGetDoublei_v");
+       gl_get_floati_v = dlsym(RTLD_NEXT, "glGetFloati_v");
+       gl_get_integeri_v = dlsym(RTLD_NEXT, "glGetIntegeri_v");
+       gl_get_integer64i_v = dlsym(RTLD_NEXT, "glGetInteger64i_v");*/
 
        done_init = 1;
        return 0;
 
        done_init = 1;
        return 0;
@@ -93,7 +93,8 @@ static int init(void)
 
 const GLubyte *glGetString(GLenum name)
 {
 
 const GLubyte *glGetString(GLenum name)
 {
-       const char *key, *value;
+       const char *key;
+       const struct cfgopt *option;
 
        init();
 
 
        init();
 
@@ -110,20 +111,20 @@ const GLubyte *glGetString(GLenum name)
                key = "extensions";
                break;
 
                key = "extensions";
                break;
 
-  case GL_RENDERER:
-    key = "renderer";
-    break;
+       case GL_RENDERER:
+               key = "renderer";
+               break;
 
 
-  case GL_SHADING_LANGUAGE_VERSION:
-    key = "sl version";
-    break;
+       case GL_SHADING_LANGUAGE_VERSION:
+               key = "sl version";
+               break;
 
        default:
                key = 0;
        }
 
 
        default:
                key = 0;
        }
 
-       if(key && (value = gliar_find_opt(cfglist, key))) {
-               return value;
+       if(key && (option = gliar_find_opt(cfglist, key))) {
+               return (const GLubyte*)option->conc_vals;
        }
 
        return gl_get_string(name);
        }
 
        return gl_get_string(name);