Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
changeset 14:594c0ee5a2b9
deprecated default brch
author | Me@portablequad |
---|---|
date | Mon, 13 Feb 2012 13:07:22 -0800 |
parents | bddabc0956b9 |
children | |
files | Param.h ParamBag.c ReadParamsFromFile.c __brch__DEPRECATED_README __brch__pure_C |
diffstat | 5 files changed, 8 insertions(+), 376 deletions(-) [+] |
line diff
1.1 --- a/Param.h Mon Feb 13 13:04:40 2012 -0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,56 +0,0 @@ 1.4 -/* 1.5 - * 1.6 - * Author: SeanHalle@yahoo.com 1.7 - * 1.8 - * Created on November 19, 2009, 6:30 PM 1.9 - */ 1.10 - 1.11 -#ifndef _PARAM_H 1.12 -#define _PARAM_H 1.13 - 1.14 -typedef 1.15 -struct 1.16 - { int type; 1.17 - int intValue; 1.18 - char * strValue; 1.19 - float floatValue; 1.20 - } 1.21 -ParamStruc; 1.22 - 1.23 -#define INT_PARAM_TYPE 0 1.24 -#define STRING_PARAM_TYPE 1 1.25 -#define FLOAT_PARAM_TYPE 2 1.26 - 1.27 -#define PARAM_BAG_HASHSIZE 1024 1.28 - 1.29 -typedef struct _ParamBagHashEntry ParamBagHashEntry; 1.30 - 1.31 -struct _ParamBagHashEntry 1.32 - { 1.33 - char *key; 1.34 - ParamStruc *param; 1.35 - struct _ParamBagHashEntry *next; 1.36 - } 1.37 -/*ParamBagHashEntry*/; 1.38 - 1.39 - 1.40 -typedef 1.41 -struct 1.42 - { int bagSz; 1.43 - ParamBagHashEntry* *entries; 1.44 - } 1.45 -ParamBag; 1.46 - 1.47 - 1.48 -ParamBag *makeParamBag(); 1.49 -void readParamFileIntoBag( char *paramFileName, ParamBag * bag ); 1.50 -ParamStruc *getParamFromBag( char *key, ParamBag * bag ); 1.51 -int addParamToBag( char* key, ParamStruc *param, ParamBag *bag ); 1.52 -void freeParamBag( ParamBag *bag ); 1.53 -//char *paramBagToString( ParamBag * bag ); 1.54 -ParamStruc *makeParamStruc(); 1.55 -ParamStruc *makeParamFromStrs( char * type, char *value ); 1.56 -ssize_t getline( char **lineptr, size_t *n, FILE *stream ); 1.57 - 1.58 -#endif /* _PARAM_H */ 1.59 -
2.1 --- a/ParamBag.c Mon Feb 13 13:04:40 2012 -0800 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,203 +0,0 @@ 2.4 -/* 2.5 - * Copyright 2009 OpenSourceStewardshipFoundation.org 2.6 - * Licensed under GNU General Public License version 2 2.7 - * 2.8 - * Based on code posted to a discussion group on the web. (Forgot to mark 2.9 - * down where got it from) 2.10 - * 2.11 - * Author: seanhalle@yahoo.com 2.12 - * 2.13 - * Created on November 14, 2009, 9:00 PM 2.14 - */ 2.15 -#include <string.h> 2.16 -#include <stdio.h> 2.17 -#include <stdlib.h> 2.18 - 2.19 -#include "Param.h" 2.20 - 2.21 -void freeParamStruc( ParamStruc * param ); 2.22 -void freeParamBagHashEntry( ParamBagHashEntry *entry ); 2.23 -ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 2.24 -unsigned int hashThisKey( char *s, int hashSz ); 2.25 -void nullOutParamBagHashEntries( ParamBag *bag ); 2.26 - 2.27 - ParamBag * 2.28 -makeParamBag() 2.29 - { ParamBag * retBag; 2.30 - retBag = malloc( sizeof( ParamBag ) ); 2.31 - retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); 2.32 - retBag->bagSz = PARAM_BAG_HASHSIZE; 2.33 - nullOutParamBagHashEntries( retBag ); 2.34 - 2.35 - return retBag; 2.36 - } 2.37 - 2.38 - void 2.39 -nullOutParamBagHashEntries( ParamBag *bag ) 2.40 - { int i, bagSz; 2.41 - bagSz = bag->bagSz; 2.42 - ParamBagHashEntry ** entries = bag->entries; 2.43 - for( i = 0; i < bagSz; i++ ) 2.44 - entries[ i ] = NULL; 2.45 - } 2.46 - 2.47 - unsigned int 2.48 -hashKey( char *s, int hashSz ) 2.49 - { unsigned int h = 0; 2.50 - 2.51 - for( ; *s != 0; s++ ) 2.52 - h = *s + h*31; 2.53 - return h % hashSz; 2.54 - } 2.55 - 2.56 -/*Need this to be separated out, for use in both getParam and putParam 2.57 - */ 2.58 - ParamBagHashEntry * 2.59 -lookupKeyInHash( char *key, ParamBag * bag ) 2.60 - { unsigned int 2.61 - hashIndex = hashKey( key, bag->bagSz ); 2.62 - ParamBagHashEntry* 2.63 - hashEntry = bag->entries[ hashIndex ]; 2.64 - for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 2.65 - { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 2.66 - } 2.67 - return NULL; 2.68 - } 2.69 - 2.70 - ParamStruc * 2.71 -getParamFromBag( char *key, ParamBag * bag ) 2.72 - { ParamBagHashEntry *entry; 2.73 - entry = lookupKeyInHash( key, bag ); 2.74 - if( entry == NULL ) return NULL; 2.75 - 2.76 - return entry->param; 2.77 - } 2.78 - 2.79 - int 2.80 -addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) 2.81 - { unsigned int hashIdx; 2.82 - ParamBagHashEntry* hashEntry; 2.83 - hashEntry = lookupKeyInHash( key, bag ); 2.84 - if( hashEntry == NULL ) 2.85 - { hashIdx = hashKey( key, bag->bagSz ); 2.86 - hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); 2.87 - if( hashEntry == NULL ) return 0; 2.88 - hashEntry->key = strdup( key ); 2.89 - if( hashEntry->key == NULL ) return 0; 2.90 - hashEntry->next = (bag->entries)[hashIdx]; 2.91 - (bag->entries)[hashIdx] = hashEntry; 2.92 - } 2.93 - else 2.94 - { freeParamStruc( hashEntry->param ); 2.95 - } 2.96 - hashEntry->param = param; 2.97 - return 1; 2.98 - } 2.99 - 2.100 - 2.101 - void 2.102 -freeParamBag( ParamBag *bag ) 2.103 - { int i; 2.104 - ParamBagHashEntry *hashEntry, *temp, **entries; 2.105 - 2.106 - entries = bag->entries; 2.107 - for( i=0; i < bag->bagSz; i++ ) 2.108 - { if( entries[i] != NULL ) 2.109 - { hashEntry = entries[i]; 2.110 - while( hashEntry != NULL ) 2.111 - { 2.112 - temp = hashEntry->next; 2.113 - freeParamBagHashEntry( hashEntry ); 2.114 - hashEntry = temp; 2.115 - } 2.116 - } 2.117 - } 2.118 - } 2.119 - 2.120 - void 2.121 -freeParamBagHashEntry( ParamBagHashEntry *entry ) 2.122 - { 2.123 - freeParamStruc( entry->param ); 2.124 - free( entry->key ); //was malloc'd above, so free it 2.125 - free( entry ); 2.126 - } 2.127 - 2.128 - void 2.129 -freeParamStruc( ParamStruc * param ) 2.130 - { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); 2.131 - free( param ); 2.132 - } 2.133 - 2.134 - ParamStruc * 2.135 -makeParamStruc() 2.136 - { ParamStruc *retStruc; 2.137 - retStruc = malloc( sizeof( ParamStruc ) ); 2.138 - retStruc->floatValue = 0.0; 2.139 - retStruc->intValue = 0; 2.140 - retStruc->strValue = NULL; 2.141 - 2.142 - return retStruc; 2.143 - } 2.144 - 2.145 -void 2.146 -removeEndWhtSpaceFromStr( char *str ) 2.147 - { int n; 2.148 - 2.149 - n = strlen ( str ); 2.150 - while( --n >= 0 ) 2.151 - { 2.152 - if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') 2.153 - break; 2.154 - } 2.155 - str[n + 1] = '\0'; 2.156 - } 2.157 - 2.158 - 2.159 -ParamStruc * 2.160 -makeParamFromStrs( char * type, char *value ) 2.161 - { ParamStruc *retParam; 2.162 - retParam = makeParamStruc(); 2.163 - switch(*type) 2.164 - { case 'i': 2.165 - { retParam->type = INT_PARAM_TYPE; 2.166 - retParam->intValue = atoi( value ); 2.167 - } break; 2.168 - case 's': 2.169 - { retParam->type = STRING_PARAM_TYPE; 2.170 - retParam->strValue = malloc( strlen(value) + 1); 2.171 - strcpy( retParam->strValue, value ); 2.172 - removeEndWhtSpaceFromStr( retParam->strValue ); 2.173 - } break; 2.174 - case 'f': 2.175 - { retParam->type = FLOAT_PARAM_TYPE; 2.176 - retParam->floatValue = atof( value ); 2.177 - } break; 2.178 - } 2.179 - return retParam; 2.180 - } 2.181 - 2.182 - 2.183 -/* A pretty useless but good debugging function, 2.184 - which simply displays the hashtable in (key.value) pairs 2.185 -*/ 2.186 -/*void paramBagToString( ParamBag * bag ) 2.187 - { int i; 2.188 - ParamBagHashEntry *t; 2.189 - for( i = 0; i < bag->bagSz; i++ ) 2.190 - { t = entries[i]; 2.191 - if( t == NULL ) 2.192 - strcat_m( retStr, &"()" ); 2.193 - else 2.194 - { strcat_m( retStr, &"(" ); 2.195 - for( ; t != NULL; t = t->next ) 2.196 - { strcat_m( retStr, &" " ); 2.197 - strcat_m( retStr, t->key ); 2.198 - strcat_m( retStr, &"." ); 2.199 - strcat_m( retStr, paramToString( t->param ) ); 2.200 - strcat_m( retStr, &" " ); 2.201 - } 2.202 - strcat_m( retStr, &")" ); 2.203 - } 2.204 - } 2.205 - } 2.206 -*/
3.1 --- a/ReadParamsFromFile.c Mon Feb 13 13:04:40 2012 -0800 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,111 +0,0 @@ 3.4 -/* 3.5 - * Author: SeanHalle@yahoo.com 3.6 - * 3.7 - * Created on June 15, 2009, 10:12 AM 3.8 - */ 3.9 - 3.10 -#include <stdio.h> 3.11 -#include <stdlib.h> 3.12 -#include <string.h> 3.13 - 3.14 -#include "Param.h" 3.15 - 3.16 -ParamStruc * makeParamFromStrs( char * type, char *value ); 3.17 - 3.18 -#define _GNU_SOURCE 3.19 - 3.20 -/*Copied from gnu's win32 lib 3.21 - */ 3.22 - ssize_t 3.23 -getline( char **lineptr, size_t *n, FILE *stream ) 3.24 - { 3.25 - if ( lineptr == NULL || n == NULL) return -1; 3.26 - if (*lineptr == NULL || *n == 0) 3.27 - { *n = 500; //max length of line in a config file 3.28 - *lineptr = (char *) malloc( *n ); 3.29 - if (*lineptr == NULL) return -1; 3.30 - } 3.31 - if( fgets( *lineptr, *n, stream ) ) 3.32 - return *n; 3.33 - else 3.34 - return -1; 3.35 - } 3.36 - 3.37 - void 3.38 -readParamFileIntoBag( char *paramFileName, ParamBag * bag ) 3.39 - { 3.40 - size_t lineSz = 0; 3.41 - FILE* paramFile; 3.42 - char* line = NULL; 3.43 - 3.44 - char* paramType;// = malloc( 12 ); //"double" is the longest type 3.45 - char* paramName;// = malloc( 500 ); //max of 500 chars in name 3.46 - char* paramValue;// = malloc( 500 ); //max of 500 chars in value 3.47 - 3.48 - lineSz = 500; //max length of line in a config file 3.49 - line = (char *) malloc( lineSz ); 3.50 - if( line == NULL ) 3.51 - { printf( "\nIn readParamFileIntoBag: no mem for line\n" ); 3.52 - return; 3.53 - } 3.54 - 3.55 - 3.56 - paramFile = fopen( paramFileName, "r" ); 3.57 - if( paramFile == NULL ) 3.58 - { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); } 3.59 - fseek( paramFile, 0, SEEK_SET ); 3.60 - while( !feof( paramFile ) ) 3.61 - { while( getline( &line, &lineSz, paramFile ) != -1 ) 3.62 - { 3.63 - char *lineEnd = line + strlen(line) +1; 3.64 - char *searchPos = line; 3.65 - 3.66 - if( *line == '\n') continue; //blank line 3.67 - if( *line == '/' ) continue; //comment line 3.68 - 3.69 - //read the param type 3.70 - paramType = line; //start of string 3.71 - int foundIt = 0; 3.72 - for( ; searchPos < lineEnd && !foundIt; searchPos++) 3.73 - { if( *searchPos == ',' ) 3.74 - { foundIt = 1; 3.75 - *searchPos = 0; //mark end of string 3.76 - } 3.77 - } 3.78 - //get rid of leading spaces 3.79 - for( ; searchPos < lineEnd; searchPos++) 3.80 - { if( *searchPos != ' ' ) break; 3.81 - } 3.82 - //read the param name 3.83 - paramName = searchPos; 3.84 - foundIt = 0; 3.85 - for( ; searchPos < lineEnd && !foundIt; searchPos++) 3.86 - { if( *searchPos == ',' ) 3.87 - { foundIt = 1; 3.88 - *searchPos = 0; //mark end 3.89 - } 3.90 - } 3.91 - //get rid of leading spaces 3.92 - for( ; searchPos < lineEnd; searchPos++) 3.93 - { if( *searchPos != ' ' ) break; 3.94 - } 3.95 - //read the param value 3.96 - paramValue = searchPos; 3.97 - foundIt = 0; 3.98 - for( ; searchPos < lineEnd && !foundIt; searchPos++) 3.99 - { if( *searchPos == '\n' ) 3.100 - { foundIt = 1; 3.101 - *searchPos = 0; //mark end 3.102 - } 3.103 - } 3.104 - if( foundIt ) 3.105 - { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue ); 3.106 - ParamStruc * 3.107 - paramStruc = makeParamFromStrs( paramType, paramValue ); 3.108 - addParamToBag( paramName, paramStruc, bag ); 3.109 - } 3.110 - } 3.111 - } 3.112 - free( line ); 3.113 - } 3.114 -
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/__brch__DEPRECATED_README Mon Feb 13 13:07:22 2012 -0800 4.3 @@ -0,0 +1,8 @@ 4.4 + 4.5 +There are two versions of the library -- one for pure C use, the other for use inside VMS or within applications written in a VMS-based language (IE, inside a top-level function or a call descendant of a top-level function) -- but only when the VMS is the "MC_shared" version. 4.6 + 4.7 +The reason is that VMS that uses shared memory on multicores moves the SlaveVPs around among cores. But, the libC and glibC malloc stores info at the top of the stack (a "clever" hack), for a speed improvement. So, when VMS manipulates the stack pointer, and/or moves Slaves to different cores, the "free" seg faults (that was FUN to figure out ; ) So, this version of VMS implements its own malloc. 4.8 + 4.9 +It is anticipated that the MC_split version, where each core has separate data, and messages are sent between cores, can handle malloc and free to use the glibC version. 4.10 + 4.11 +For now, update to the version of the library you wish to use..
5.1 --- a/__brch__pure_C Mon Feb 13 13:04:40 2012 -0800 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,6 +0,0 @@ 5.4 - 5.5 -This branch is for use in pure C code (*not* inside VMS-based language application code) 5.6 - 5.7 -There are two versions of the library -- one for pure C use, the other for use inside VMS or within applications written in a VMS-based language (IE, inside a top-level function or a call descendant of a top-level function) -- but only when the VMS is the "MC_shared" version. 5.8 - 5.9 -The reason is that VMS that uses shared memory on multicores moves the SlaveVPs around among cores. But, the libC and glibC malloc stores info at the top of the stack (a "clever" hack), for a speed improvement. So, when VMS manipulates the stack pointer, and/or moves Slaves to different cores, the "free" seg faults (that was FUN to figure out ; ) So, this version of VMS implements its own malloc. 5.10 \ No newline at end of file