123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- #import "qedefs.h"
- @implementation Dict
- - init
- {
- [super initCount:0
- elementSize:sizeof(dict_t)
- description:NULL];
- return self;
- }
- - print
- {
- int i;
- dict_t *d;
-
- for (i=0 ; i<numElements ; i++)
- {
- d = [self elementAt: i];
- printf ("%s : %s\n",d->key, d->value);
- }
- return self;
- }
- /*
- ===========
- copyFromZone
- JDC
- ===========
- */
- - copyFromZone:(NXZone *)zone
- {
- id new;
- int i;
- dict_t *d;
- char *old;
-
- new = [super copyFromZone: zone];
- for (i=0 ; i<numElements ; i++)
- {
- d = [self elementAt: i];
- old = d->key;
- d->key = malloc(strlen(old)+1);
- strcpy (d->key, old);
-
- old = d->value;
- d->value = malloc(strlen(old)+1);
- strcpy (d->value, old);
- }
-
- return new;
- }
- - initFromFile:(FILE *)fp
- {
- [self init];
- return [self parseBraceBlock:fp];
- }
- //===============================================
- //
- // Dictionary pair functions
- //
- //===============================================
- //
- // Write a { } block out to a FILE*
- //
- - writeBlockTo:(FILE *)fp
- {
- int max;
- int i;
- dict_t *d;
-
- fprintf(fp,"{\n");
- max = [super count];
- for (i = 0;i < max;i++)
- {
- d = [super elementAt:i];
- fprintf(fp,"\t{\"%s\"\t\"%s\"}\n",d->key,d->value);
- }
- fprintf(fp,"}\n");
-
- return self;
- }
- //
- // Write a single { } block out
- //
- - writeFile:(char *)path
- {
- FILE *fp;
-
- fp = fopen(path,"w+t");
- if (fp != NULL)
- {
- printf("Writing dictionary file %s.\n",path);
- fprintf(fp,"// QE_Project file %s\n",path);
- [self writeBlockTo:fp];
- fclose(fp);
- }
- else
- {
- printf("Error writing %s!\n",path);
- return NULL;
- }
- return self;
- }
- //===============================================
- //
- // Utility methods
- //
- //===============================================
- //
- // Find a keyword in storage
- // Returns * to dict_t, otherwise NULL
- //
- - (dict_t *) findKeyword:(char *)key
- {
- int max;
- int i;
- dict_t *d;
-
- max = [super count];
- for (i = 0;i < max;i++)
- {
- d = [super elementAt:i];
- if (!strcmp(d->key,key))
- return d;
- }
-
- return NULL;
- }
- //
- // Change a keyword's string
- //
- - changeStringFor:(char *)key to:(char *)value
- {
- dict_t *d;
- dict_t newd;
-
- d = [self findKeyword:key];
- if (d != NULL)
- {
- free(d->value);
- d->value = malloc(strlen(value)+1);
- strcpy(d->value,value);
- }
- else
- {
- newd.key = malloc(strlen(key)+1);
- strcpy(newd.key,key);
- newd.value = malloc(strlen(value)+1);
- strcpy(newd.value,value);
- [self addElement:&newd];
- }
- return self;
- }
- //
- // Search for keyword, return the string *
- //
- - (char *)getStringFor:(char *)name
- {
- dict_t *d;
-
- d = [self findKeyword:name];
- if (d != NULL)
- return d->value;
-
- return "";
- }
- //
- // Search for keyword, return the value
- //
- - (unsigned int)getValueFor:(char *)name
- {
- dict_t *d;
-
- d = [self findKeyword:name];
- if (d != NULL)
- return atol(d->value);
-
- return 0;
- }
- //
- // Return # of units in keyword's value
- //
- - (int) getValueUnits:(char *)key
- {
- id temp;
- int count;
-
- temp = [self parseMultipleFrom:key];
- count = [temp count];
- [temp free];
-
- return count;
- }
- //
- // Convert List to string
- //
- - (char *)convertListToString:(id)list
- {
- int i;
- int max;
- char tempstr[4096];
- char *s;
- char *newstr;
-
- max = [list count];
- tempstr[0] = 0;
- for (i = 0;i < max;i++)
- {
- s = [list elementAt:i];
- strcat(tempstr,s);
- strcat(tempstr," ");
- }
- newstr = malloc(strlen(tempstr)+1);
- strcpy(newstr,tempstr);
-
- return newstr;
- }
- //
- // JDC: I wrote this to simplify removing vectors
- //
- - removeKeyword:(char *)key
- {
- dict_t *d;
- d = [self findKeyword:key];
- if (d == NULL)
- return self;
- [self removeElementAt:d - (dict_t*)dataPtr];
- return self;
- }
- //
- // Delete string from keyword's value
- //
- - delString:(char *)string fromValue:(char *)key
- {
- id temp;
- int count;
- int i;
- char *s;
- dict_t *d;
-
- d = [self findKeyword:key];
- if (d == NULL)
- return NULL;
- temp = [self parseMultipleFrom:key];
- count = [temp count];
- for (i = 0;i < count;i++)
- {
- s = [temp elementAt:i];
- if (!strcmp(s,string))
- {
- [temp removeElementAt:i];
- free(d->value);
- d->value = [self convertListToString:temp];
- [temp free];
-
- break;
- }
- }
- return self;
- }
- //
- // Add string to keyword's value
- //
- - addString:(char *)string toValue:(char *)key
- {
- char *newstr;
- char spacing[] = "\t";
- dict_t *d;
-
- d = [self findKeyword:key];
- if (d == NULL)
- return NULL;
- newstr = malloc(strlen(string) + strlen(d->value) + strlen(spacing) + 1);
- strcpy(newstr,d->value);
- strcat(newstr,spacing);
- strcat(newstr,string);
- free(d->value);
- d->value = newstr;
-
- return self;
- }
- //===============================================
- //
- // Use these for multiple parameters in a keyword value
- //
- //===============================================
- char *searchStr;
- char item[4096];
- - setupMultiple:(char *)value
- {
- searchStr = value;
- return self;
- }
- - (char *)getNextParameter
- {
- char *s;
-
- if (!searchStr)
- return NULL;
- strcpy(item,searchStr);
- s = FindWhitespcInBuffer(item);
- if (!*s)
- searchStr = NULL;
- else
- {
- *s = 0;
- searchStr = FindNonwhitespcInBuffer(s+1);
- }
- return item;
- }
- //
- // Parses a keyvalue string & returns a Storage full of those items
- //
- - (id) parseMultipleFrom:(char *)key
- {
- #define ITEMSIZE 128
- id stuff;
- char string[ITEMSIZE];
- char *s;
-
- s = [self getStringFor:key];
- if (s == NULL)
- return NULL;
-
- stuff = [[Storage alloc]
- initCount:0
- elementSize:ITEMSIZE
- description:NULL];
-
- [self setupMultiple:s];
- while((s = [self getNextParameter]))
- {
- bzero(string,ITEMSIZE);
- strcpy(string,s);
- [stuff addElement:string];
- }
-
- return stuff;
- }
- //===============================================
- //
- // Dictionary pair parsing
- //
- //===============================================
- //
- // parse all keyword/value pairs within { } 's
- //
- - (id) parseBraceBlock:(FILE *)fp
- {
- int c;
- dict_t pair;
- char string[1024];
-
- c = FindBrace(fp);
- if (c == -1)
- return NULL;
-
- while((c = FindBrace(fp)) != '}')
- {
- if (c == -1)
- return NULL;
- // c = FindNonwhitespc(fp);
- // if (c == -1)
- // return NULL;
- // CopyUntilWhitespc(fp,string);
- // JDC: fixed to allow quoted keys
- c = FindNonwhitespc(fp);
- if (c == -1)
- return NULL;
- c = fgetc(fp);
- if ( c == '\"')
- CopyUntilQuote(fp,string);
- else
- {
- ungetc (c,fp);
- CopyUntilWhitespc(fp,string);
- }
- pair.key = malloc(strlen(string)+1);
- strcpy(pair.key,string);
-
- c = FindQuote(fp);
- CopyUntilQuote(fp,string);
- pair.value = malloc(strlen(string)+1);
- strcpy(pair.value,string);
-
- [super addElement:&pair];
- c = FindBrace(fp);
- }
-
- return self;
- }
- @end
- //===============================================
- //
- // C routines for string parsing
- //
- //===============================================
- int GetNextChar(FILE *fp)
- {
- int c;
- int c2;
-
- c = getc(fp);
- if (c == EOF)
- return -1;
- if (c == '/') // parse comments
- {
- c2 = getc(fp);
- if (c2 == '/')
- {
- while((c2 = getc(fp)) != '\n');
- c = getc(fp);
- }
- else
- ungetc(c2,fp);
- }
- return c;
- }
- void CopyUntilWhitespc(FILE *fp,char *buffer)
- {
- int count = 800;
- int c;
-
- while(count--)
- {
- c = GetNextChar(fp);
- if (c == EOF)
- return;
- if (c <= ' ')
- {
- *buffer = 0;
- return;
- }
- *buffer++ = c;
- }
- }
- void CopyUntilQuote(FILE *fp,char *buffer)
- {
- int count = 800;
- int c;
-
- while(count--)
- {
- c = GetNextChar(fp);
- if (c == EOF)
- return;
- if (c == '\"')
- {
- *buffer = 0;
- return;
- }
- *buffer++ = c;
- }
- }
- int FindBrace(FILE *fp)
- {
- int count = 800;
- int c;
-
- while(count--)
- {
- c = GetNextChar(fp);
- if (c == EOF)
- return -1;
- if (c == '{' ||
- c == '}')
- return c;
- }
- return -1;
- }
- int FindQuote(FILE *fp)
- {
- int count = 800;
- int c;
-
- while(count--)
- {
- c = GetNextChar(fp);
- if (c == EOF)
- return -1;
- if (c == '\"')
- return c;
- }
- return -1;
- }
- int FindWhitespc(FILE *fp)
- {
- int count = 800;
- int c;
-
- while(count--)
- {
- c = GetNextChar(fp);
- if (c == EOF)
- return -1;
- if (c <= ' ')
- {
- ungetc(c,fp);
- return c;
- }
- }
- return -1;
- }
- int FindNonwhitespc(FILE *fp)
- {
- int count = 800;
- int c;
-
- while(count--)
- {
- c = GetNextChar(fp);
- if (c == EOF)
- return -1;
- if (c > ' ')
- {
- ungetc(c,fp);
- return c;
- }
- }
- return -1;
- }
- char *FindWhitespcInBuffer(char *buffer)
- {
- int count = 1000;
- char *b = buffer;
-
- while(count--)
- if (*b <= ' ')
- return b;
- else
- b++;
- return NULL;
- }
- char *FindNonwhitespcInBuffer(char *buffer)
- {
- int count = 1000;
- char *b = buffer;
-
- while(count--)
- if (*b > ' ')
- return b;
- else
- b++;
- return NULL;
- }
|