diff ReadParamsFromFile.c @ 6:d46150af45ad

Newly created project repository -- commit sub-states
author Me@portablequad
date Tue, 07 Feb 2012 12:51:29 -0800
parents a8744027c1a9
children 1bf28dddc45f 93b017e30c76
line diff
     1.1 --- a/ReadParamsFromFile.c	Fri Jan 27 15:51:11 2012 +0100
     1.2 +++ b/ReadParamsFromFile.c	Tue Feb 07 12:51:29 2012 -0800
     1.3 @@ -1,111 +1,111 @@
     1.4 -/* 
     1.5 - * Author: SeanHalle@yahoo.com
     1.6 - *
     1.7 - * Created on June 15, 2009, 10:12 AM
     1.8 - */
     1.9 -
    1.10 -#include <stdio.h>
    1.11 -#include <stdlib.h>
    1.12 -#include <string.h>
    1.13 -
    1.14 -#include "Param.h"
    1.15 -
    1.16 -ParamStruc * makeParamFromStrs( char * type, char *value );
    1.17 -
    1.18 -#define _GNU_SOURCE
    1.19 -
    1.20 -/*Copied from gnu's win32 lib
    1.21 - */
    1.22 - ssize_t
    1.23 -getline( char **lineptr, size_t *n, FILE *stream )
    1.24 - {
    1.25 -   if ( lineptr == NULL || n == NULL)  return -1;
    1.26 -   if (*lineptr == NULL || *n == 0)
    1.27 -    { *n = 500; //max length of line in a config file
    1.28 -      *lineptr = (char *) malloc( *n );
    1.29 -      if (*lineptr == NULL) return -1;
    1.30 -    }
    1.31 -   if( fgets( *lineptr, *n, stream ) )
    1.32 -      return *n;
    1.33 -   else
    1.34 -      return -1;
    1.35 - }
    1.36 -
    1.37 - void
    1.38 -readParamFileIntoBag( char *paramFileName, ParamBag * bag )
    1.39 - {
    1.40 -   size_t lineSz = 0;
    1.41 -   FILE* paramFile;
    1.42 -   char* line = NULL;
    1.43 -
    1.44 -   char* paramType;//  = malloc( 12 );  //"double" is the longest type
    1.45 -   char* paramName;//  = malloc( 500 ); //max of 500 chars in name
    1.46 -   char* paramValue;// = malloc( 500 ); //max of 500 chars in value
    1.47 -   
    1.48 -   lineSz = 500; //max length of line in a config file
    1.49 -   line = (char *) malloc( lineSz );
    1.50 -   if( line == NULL )
    1.51 -    { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
    1.52 -      return;
    1.53 -    }
    1.54 -   
    1.55 -   
    1.56 -   paramFile = fopen( paramFileName, "r" );
    1.57 -   if( paramFile == NULL ) 
    1.58 -    { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
    1.59 -   fseek( paramFile, 0, SEEK_SET );
    1.60 -   while( !feof( paramFile ) )
    1.61 -    { while( getline( &line, &lineSz, paramFile ) != -1 )
    1.62 -       {
    1.63 -         char *lineEnd = line + strlen(line) +1;
    1.64 -         char *searchPos = line;
    1.65 -         
    1.66 -         if( *line == '\n') continue; //blank line
    1.67 -         if( *line == '/' ) continue; //comment line
    1.68 -
    1.69 -            //read the param type
    1.70 -         paramType = line; //start of string
    1.71 -         int foundIt = 0;
    1.72 -         for( ; searchPos < lineEnd && !foundIt; searchPos++)
    1.73 -          { if( *searchPos == ',' )
    1.74 -             { foundIt = 1;
    1.75 -               *searchPos = 0; //mark end of string
    1.76 -             }
    1.77 -          }
    1.78 -            //get rid of leading spaces
    1.79 -         for( ; searchPos < lineEnd; searchPos++)
    1.80 -          { if( *searchPos != ' ' ) break;
    1.81 -          }
    1.82 -            //read the param name
    1.83 -         paramName = searchPos;
    1.84 -         foundIt = 0;
    1.85 -         for( ; searchPos < lineEnd && !foundIt; searchPos++)
    1.86 -          { if( *searchPos == ',' )
    1.87 -             { foundIt = 1;
    1.88 -               *searchPos = 0; //mark end
    1.89 -             }
    1.90 -          }
    1.91 -            //get rid of leading spaces
    1.92 -         for( ; searchPos < lineEnd; searchPos++)
    1.93 -          { if( *searchPos != ' ' ) break;
    1.94 -          }
    1.95 -            //read the param value
    1.96 -         paramValue = searchPos;
    1.97 -         foundIt = 0;
    1.98 -         for( ; searchPos < lineEnd && !foundIt; searchPos++)
    1.99 -          { if( *searchPos == '\n' )
   1.100 -             { foundIt = 1;
   1.101 -               *searchPos = 0; //mark end
   1.102 -             }
   1.103 -          }
   1.104 -         if( foundIt )
   1.105 -          { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
   1.106 -             ParamStruc *
   1.107 -            paramStruc = makeParamFromStrs( paramType, paramValue );
   1.108 -            addParamToBag( paramName, paramStruc, bag );
   1.109 -          }
   1.110 -       }
   1.111 -    }
   1.112 -   free( line );
   1.113 - }
   1.114 -
   1.115 +/* 
   1.116 + * Author: SeanHalle@yahoo.com
   1.117 + *
   1.118 + * Created on June 15, 2009, 10:12 AM
   1.119 + */
   1.120 +
   1.121 +#include <stdio.h>
   1.122 +#include <stdlib.h>
   1.123 +#include <string.h>
   1.124 +
   1.125 +#include "Param.h"
   1.126 +
   1.127 +ParamStruc * makeParamFromStrs( char * type, char *value );
   1.128 +
   1.129 +#define _GNU_SOURCE
   1.130 +
   1.131 +/*Copied from gnu's win32 lib
   1.132 + */
   1.133 + ssize_t
   1.134 +getline( char **lineptr, size_t *n, FILE *stream )
   1.135 + {
   1.136 +   if ( lineptr == NULL || n == NULL)  return -1;
   1.137 +   if (*lineptr == NULL || *n == 0)
   1.138 +    { *n = 500; //max length of line in a config file
   1.139 +      *lineptr = (char *) malloc( *n );
   1.140 +      if (*lineptr == NULL) return -1;
   1.141 +    }
   1.142 +   if( fgets( *lineptr, *n, stream ) )
   1.143 +      return *n;
   1.144 +   else
   1.145 +      return -1;
   1.146 + }
   1.147 +
   1.148 + void
   1.149 +readParamFileIntoBag( char *paramFileName, ParamBag * bag )
   1.150 + {
   1.151 +   size_t lineSz = 0;
   1.152 +   FILE* paramFile;
   1.153 +   char* line = NULL;
   1.154 +
   1.155 +   char* paramType;//  = malloc( 12 );  //"double" is the longest type
   1.156 +   char* paramName;//  = malloc( 500 ); //max of 500 chars in name
   1.157 +   char* paramValue;// = malloc( 500 ); //max of 500 chars in value
   1.158 +   
   1.159 +   lineSz = 500; //max length of line in a config file
   1.160 +   line = (char *) malloc( lineSz );
   1.161 +   if( line == NULL )
   1.162 +    { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
   1.163 +      return;
   1.164 +    }
   1.165 +   
   1.166 +   
   1.167 +   paramFile = fopen( paramFileName, "r" );
   1.168 +   if( paramFile == NULL ) 
   1.169 +    { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
   1.170 +   fseek( paramFile, 0, SEEK_SET );
   1.171 +   while( !feof( paramFile ) )
   1.172 +    { while( getline( &line, &lineSz, paramFile ) != -1 )
   1.173 +       {
   1.174 +         char *lineEnd = line + strlen(line) +1;
   1.175 +         char *searchPos = line;
   1.176 +         
   1.177 +         if( *line == '\n') continue; //blank line
   1.178 +         if( *line == '/' ) continue; //comment line
   1.179 +
   1.180 +            //read the param type
   1.181 +         paramType = line; //start of string
   1.182 +         int foundIt = 0;
   1.183 +         for( ; searchPos < lineEnd && !foundIt; searchPos++)
   1.184 +          { if( *searchPos == ',' )
   1.185 +             { foundIt = 1;
   1.186 +               *searchPos = 0; //mark end of string
   1.187 +             }
   1.188 +          }
   1.189 +            //get rid of leading spaces
   1.190 +         for( ; searchPos < lineEnd; searchPos++)
   1.191 +          { if( *searchPos != ' ' ) break;
   1.192 +          }
   1.193 +            //read the param name
   1.194 +         paramName = searchPos;
   1.195 +         foundIt = 0;
   1.196 +         for( ; searchPos < lineEnd && !foundIt; searchPos++)
   1.197 +          { if( *searchPos == ',' )
   1.198 +             { foundIt = 1;
   1.199 +               *searchPos = 0; //mark end
   1.200 +             }
   1.201 +          }
   1.202 +            //get rid of leading spaces
   1.203 +         for( ; searchPos < lineEnd; searchPos++)
   1.204 +          { if( *searchPos != ' ' ) break;
   1.205 +          }
   1.206 +            //read the param value
   1.207 +         paramValue = searchPos;
   1.208 +         foundIt = 0;
   1.209 +         for( ; searchPos < lineEnd && !foundIt; searchPos++)
   1.210 +          { if( *searchPos == '\n' )
   1.211 +             { foundIt = 1;
   1.212 +               *searchPos = 0; //mark end
   1.213 +             }
   1.214 +          }
   1.215 +         if( foundIt )
   1.216 +          { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
   1.217 +             ParamStruc *
   1.218 +            paramStruc = makeParamFromStrs( paramType, paramValue );
   1.219 +            addParamToBag( paramName, paramStruc, bag );
   1.220 +          }
   1.221 +       }
   1.222 +    }
   1.223 +   free( line );
   1.224 + }
   1.225 +