home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-09-09 | 6.5 KB | 359 lines | [TEXT/MPS ] |
- // dictionaries.c
- //
- // distionary functions for CIncludesCode MPW tool
-
-
- #include <StdLib.h>
- #include <Memory.h>
- #include <StdIO.h>
- #include <String.h>
- #include "CIncludesCode.h"
-
- extern long totalWords;
- extern long numFiles;
- extern ptrArray *argvPtr;
- extern strArray **filesHdl;
- extern Handle dictionary[numDictionaries];
- extern Handle dependencies;
-
- short dictionaryIndex(char ch)
- {
- if (ch == '_')
- {
- return 0;
- } // if ch
- else
- {
- return ((ch & 0xDF) - '@');
- } // else if ch
- } // dictionaryIndex()
-
- void insertWord(short fileIndex,long filePosition,char *s)
- {
- Handle dictHdl;
- long curSize;
- long endOffset;
- char *p;
- short len = strlen(s);
-
- if (len == 0)
- {
- return;
- } // if len
-
- totalWords++;
-
- dictHdl = dictionary[dictionaryIndex(*s)];
- curSize = GetHandleSize(dictHdl);
- endOffset = *((long*) (*dictHdl));
-
- if ((8 + endOffset + len + 1) > curSize)
- {
- SetHandleSize(dictHdl,curSize + 1024);
- if (MemError())
- {
- fprintf(stderr,"MemError = %d\n",MemError());
- errorExit ("Could not resize dictionary handle!");
- } // if MemError()
- } // if 8
-
- p = *dictHdl + endOffset;
- putShort(p,fileIndex);
- putLong((p + 2),filePosition);
- strcpy((p + (2 + 4)),s);
-
- *((long*) (*dictHdl)) += ((2 + 4 + 1) + len); // fileIndex = 2,filePostion = 4,'\0' = 1
- } // insertWord()
-
- #define W(s) insertWord(-1,0,s)
-
- void fillReservedWords(void)
- {
- W("asm");
- W("auto");
- W("break");
- W("case");
- W("catch");
- W("char");
- W("class");
- W("comp");
- W("const");
- W("continue");
- W("default");
- W("delete");
- W("do");
- W("double");
- W("else");
- W("entry");
- W("enum");
- W("extended");
- W("extern");
- W("float");
- W("for");
- W("friend");
- W("goto");
- W("if");
- W("inherited");
- W("inline");
- W("int");
- W("long");
- W("new");
- W("operator");
- W("overload");
- W("pascal");
- W("private");
- W("protected");
- W("public");
- W("register");
- W("return");
- W("short");
- W("signed");
- W("sizeof");
- W("static");
- W("struct");
- W("switch");
- W("template");
- W("this");
- W("typedef");
- W("union");
- W("unsigned");
- W("virtual");
- W("void");
- W("volatile");
- W("while");
- W("cdecl");
- W("near");
- W("far");
- W("huge");
- W("interrupt");
- } // fillReservedWords
-
- void initDictionaries(void)
- {
- short i;
-
- fprintf(stderr,"Initializing dictionaries...\n");
-
- for (i = 0; i < numDictionaries; ++i)
- {
- dictionary[i] = NewHandle(4);
- *((long*) (*dictionary[i])) = 4; /* "End Pointer" */
- } // for i
-
- fillReservedWords();
- } // initDictionaries()
-
-
- void DisposDictionaries(void)
- {
- short i;
-
- for (i = 0; i < numDictionaries; ++i)
- {
- DisposHandle(dictionary[i]);
- } // for i
- } // DisposeDictionaries()
-
- char *extractIdentifier(char *dest,char *src)
- {
- char *destPtr = dest;
- short count;
-
- for (count = 0; validChar(*src) && (count < 64); ++count)
- {
- *dest++ = *src++;
- } // for count
- if (*src == '.') // return NULL string for structure members,etc.
- {
- while ((*src == '.') || validChar(*src))
- {
- src++;
- } // while *src
- *destPtr = '\0';
- } // if *src
- else
- {
- *dest = '\0';
- } // else if *src
- return src;
- } // extractIdentifier
-
- long nextIdentifier(char *s,Handle dataHdl,long offset,long totalSize)
- // parse text looking for valid identifiers
-
- {
- char *base = StripAddress(*dataHdl);
- char *p = base + offset;
- char *q = base + totalSize;
- char ch;
- Boolean done;
-
- while (p < q)
- {
- ch = *p++;
-
- if (ch == '"')
- {
- // skip string literals
- // while ((*p++ != '"') || (*(p-2) == '\\')) // original code does not correctly handle cases like "hello\\"
- // ;
- do
- {
- ch = *p++;
- done = ((ch == '"') && !((*(p - 1) == '\\') && (*(p - 2) != '\\')));
- } // do
- while(!done);
- } // if ch
- else
- {
- if (ch == '#')
- {
- // skip pre-processor commands
- while (validChar(*p++))
- ;
- } // if ch
- else
- {
- if ((ch == '/') && (*p == '/'))
- {
- // skip comments
- while (*p++ != '\n')
- ;
- } // if ch
- else
- {
- if ((ch == '/') && (*p == '*'))
- {
- // skip comments
- while ((*p++ != '/') || (*(p-2) != '*'))
- ;
- } // if ch
- else
- {
- if (validStart(ch))
- {
- p = extractIdentifier(s,p - 1);
- if (*s)
- {
- return ((long) (p - base));
- } // if *s
- } // if validStart()
- } // else if ch
- } // else if ch
- } // else if ch
- } // else if ch
- } // while p
- return totalSize;
- } // nextIdentifier()
-
- long searchDict(char *s,Handle dictHdl)
- // returns offset or 0
-
- {
- char *base = StripAddress(*dictHdl);
- char *p = base + (4 + 2 + 4); // endOffset = 4,fileIndex = 2,filePostion = 4;
- char *limit = base + *((long*) base);
-
- if (*s)
- {
- while (p < limit)
- {
- if (strcmp(p,s) == 0)
- {
- return ((long) (p - base - (2 + 4))); // fileIndex = 2,filePostion = 4
- } // if strcmp()
- else
- {
- p += strlen(p) + (2 + 4 + 1); // fileIndex = 2,filePostion = 4,'\0' = 1
- } // else if strcmp()
- } // while p
- } // if s
- return 0;
- } // searchDict()
-
- long parseFile(Handle dataHdl,short fileIndex)
- {
- short oldFile;
- long offset;
- long count = 0;
- long pos = 0;
- long totalSize = GetHandleSize(dataHdl) - 1;
- Handle dictHdl;
- char s[64];
-
- while ((pos = nextIdentifier(s,dataHdl,pos,totalSize)) < totalSize)
- {
- dictHdl = dictionary[dictionaryIndex(*s)];
-
- offset = searchDict(s,dictHdl);
- if (offset)
- {
- oldFile = getShort(*dictHdl + offset);
-
- if ((oldFile != -1) && (oldFile != fileIndex) && isDependent(*dependencies,oldFile,fileIndex))
- {
- putShort((*dictHdl + offset),fileIndex);
- putLong((*dictHdl + offset + 2),pos);
- } // if oldFile
- } // if offset
- else
- {
- count++;
- insertWord(fileIndex,pos,s);
- } // else if offset
- } // while pos
- return count;
- } // parseFile()
-
- void fillDictionaries(void)
- {
- short i;
- short maxLength = maxFilename();
- long count = 0;
- Handle dataHdl;
-
- fprintf(stderr,"\nParsing Files:\n");
- for (i = 0; i < numFiles; ++i)
- {
- fprintf(stderr," %-*s",maxLength,(**filesHdl)[i]);
- dataHdl = loadDataFile((*argvPtr)[i + 2]);
- count = parseFile(dataHdl,i);
- DisposHandle(dataHdl);
- fprintf(stderr," %4d new\n",count);
- } // for i
-
- fprintf(stderr,"\nTotal Entries = %ld\n",totalWords);
- } // fillDictionaries()
-
- void writeDictionary(Handle dictHdl)
- {
- long pos = 4;
- long endOffset = *((long*) (*dictHdl));
- char s[64];
-
- while (pos < endOffset)
- {
- fprintf(stderr,"%3d ",getShort(*dictHdl + pos));
- pos += 2;
- strcpy(s,*dictHdl + pos);
- fprintf(stderr,"%s\n",s);
- pos += strlen(s) + 1;
- } // while pos
- } // writeDictionary()
-
- void writeAllDictionaries(void)
- {
- short i;
-
- for (i = 0; i < numDictionaries; ++i)
- {
- writeDictionary(dictionary[i]);
- } // for i
- } // writeAllDictionaries()
-
- void writeSpecificDirectory(char ch)
- {
- writeDictionary(dictionary[dictionaryIndex(ch)]);
- } // writeSpecificDirectory()
-
- // end of dictionaries.c