GLYLIB  0.3.0b
get_keysvals.c
Go to the documentation of this file.
00001 /** \file get_keysvals.c Functions for extracting lists of keywords 
00002 and values.
00003 
00004 Contains functions to ease reading of files (slurped or not).
00005 Begun on 20080816 by BLFoley.
00006 */
00007 
00008 #include "mylib.h"
00009 //#include "general.h"
00010 
00011 /****************** get_keysvals_from_slurp() ******************/
00012 /** Reads in keyword/value pairs from a file, drops them in a
00013 structure designed for the purpose.
00014 
00015 Some notes:
00016 
00017         - If ignore_hash is 0, everything from a hash mark (#) to the end of the line is ignored.
00018         - Any line not containing the separator SEP is ignored.
00019         - Keywords and values may not be separated by a newline.
00020         - If newline_sep is set to zero, everything* from the start of the line to the first instance 
00021                 of SEP will be regarded as the KEYWORD and everything* from the first instance of SEP
00022                 to the end of the line will be regarded as the VALUE. *Intial and final whitespace will 
00023                 be removed from both keyword and value, but all else, including internal whitespace,
00024                 will be retained.
00025         - If newline_sep is not set to zero, neither keywords nor values may contain whitespace.
00026         - SEP should be passed as a string complete with a null terminator.
00027 */
00028 gly_keysvals get_keysvals_from_slurp(fileslurp F, const char *SEP, int newline_sep, int ignore_hash){
00029 int ga=0; ///< counter
00030 char *t; ///< temporary pointer
00031 gly_keysvals KV;
00032 
00033 KV.n=0;
00034 KV.K=(char**)calloc(1,sizeof(char*));
00035 KV.V=(char**)calloc(1,sizeof(char*));
00036 
00037 for(ga=0;ga<F.n;ga++){ ///< Loop through the slurp
00038         if(ignore_hash==0){
00039                 t=strchr(F.L[ga],'#'); ///< See if there is a hash in this line
00040                 if(t!=NULL) t[0]='\0'; ///< If there is a hash, call that the end of the string
00041                 }
00042         if(strstr(F.L[ga],SEP)==NULL) continue; ///< If no separator this line, ignore it
00043         if(newline_sep==0){///< If newline_sep is set
00044                 KV.n++;
00045                 KV.K=(char**)realloc(KV.K,KV.n*sizeof(char*));
00046                 KV.V=(char**)realloc(KV.V,KV.n*sizeof(char*));
00047                 t=strstr(F.L[ga],SEP); ///< Split string at SEP (find with strstr)
00048                 t[0]='\0'; ///< Set end of the keyword string
00049                 KV.K[KV.n-1]=strdup(prune_string_whitespace(F.L[ga])); ///< Prune whitespace and copy to K
00050                 t+=(strlen(SEP)); ///< Move pointer to the end of the separator
00051                 KV.V[KV.n-1]=strdup(prune_string_whitespace(t)); ///< Prune whitespace and copy to V
00052                 }
00053         else{
00054                 fprintf(stderr,"\n\nIn get_keysvals_from_slurp (or from file):\n");
00055                 fprintf(stderr,"The use of multiple keyword/value pairs on one line is not yet supported.\n");
00056                 fprintf(stderr,"Feel free to write it in...   Exiting now.\n\n");
00057                 exit(0);
00058 /** 
00059 Else
00060         - Grab K
00061         - Check for SEP 
00062         - Grab V
00063         --- Allow for line-crossing?  No!!! (what a pain...)
00064 */
00065                 }
00066         } // close loop through the slurp
00067 
00068 
00069 return KV;
00070 }
00071 /****************** get_keysvals_from_file() ******************/
00072 /** Reads in keyword/value pairs from a file, drops them in a
00073 structure designed for the purpose.
00074 */
00075 gly_keysvals get_keysvals_from_file(fileset F, const char *SEP, int newline_sep, int ignore_hash){
00076 fileslurp FS;
00077 gly_keysvals KV;
00078 
00079 FS=slurp_file(F); ///< Slurp file 
00080 KV=get_keysvals_from_slurp(FS, SEP, newline_sep, ignore_hash); ///< Call the slurp version...
00081 
00082 return KV;
00083 }
 All Classes Files Functions Variables Typedefs Defines