// Header File // Created 04/05/2008; 21:24:52 // C Source File // Created 11/09/2006; 19:17:45 // Delete or comment out the items you do not need. #define COMMENT_STRING "Preprocessor for Newprog." #define COMMENT_PROGRAM_NAME "preproc." #define COMMENT_VERSION_STRING "V 0.1" #define COMMENT_VERSION_NUMBER 0,1,0,0 /* major, minor, revision, subrevision */ #define COMMENT_BW_ICON \ {0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000} #define COMMENT_GRAY_ICON \ {0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000}, \ {0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000, \ 0b0000000000000000} #include #define fatal(s) ({ST_showHelp (s); return;}) #define Text_NewLine (char[]){0x0D, 0x20} //Structs definition typedef struct { char *fptr, *cptr; unsigned short plen; HANDLE h; } Files ; typedef struct { char *start; //pointed to the first byte of the intervale char *end; //pointed to the byte in front of the last byte of the intervale } Intervale ; typedef struct { char *array; Intervale inter; } Array ; //Files manipulation functions void CloseOpenedFile(Files *fileptr) { if (fileptr->h) HeapUnlock (fileptr->h); if (fileptr->cptr != fileptr->fptr) free (fileptr->cptr); if(fileptr) free(fileptr); } //Functions for writing HANDLE CreateFile2 (const char *FileName) // Returns a handle, H_NULL in case of error { HANDLE h; SYM_ENTRY *sym_entry; char str[30], *sptr = str; *sptr = 0; while ((*++sptr = *FileName++)); if (!(h = HeapAlloc (HeapMax ()))) return H_NULL; if (!(sym_entry = DerefSym (SymAdd (sptr)))) { HeapFree (h); return H_NULL; } *(long*) HeapDeref (sym_entry->handle = h) = 0x00010000; //Je pense que ça inscrit la taille (1 + 3 = 4 octets) du fichier et le type (ici type =0x00) return h; } void AppendCharToFile2 (HANDLE h, unsigned char c) { char *base = HeapDeref(h); unsigned short len = *(unsigned short*)base; if (len > HeapSize(h) - 10) return; *(unsigned short*)base = len + 1; base[len+2] = c; } void AppendBlockToFile2 (HANDLE h, void *addr, unsigned short len) { unsigned short i; for (i = len; i; i--) AppendCharToFile2 (h, *((char*)addr)++); } void CloseCreatedFile (HANDLE h) { AppendCharToFile2 (h,0); AppendCharToFile2 (h,0x2D); HeapUnlock (h); HeapRealloc (h, *(unsigned short*)HeapDeref(h) + 3); } void CloseCreatedFile_type (HANDLE h,int type) { if(type==STR_TAG) AppendCharToFile2 (h,0); AppendCharToFile2 (h,type); HeapUnlock (h); HeapRealloc (h, *(unsigned short*)HeapDeref(h) + 3); } void AppendStrToFile1 (HANDLE handle, void *str) { AppendBlockToFile2 (handle, str, strlen(str)); } //Functions for outputing errors (file preerror) //Functions for reading Files* OpenFile(const char *FileName) { char *fptr, *cptr; unsigned short plen; SYM_ENTRY *SymPtr; HANDLE h; Files *fileptr; SymPtr = DerefSym (SymFind (SYMSTR (FileName))); if (!SymPtr) {fatal ("Program not found");} h = SymPtr->handle; if (HeapGetLock (h)) { cptr = fptr = HeapDeref (h); h = 0; } else { cptr = fptr = HLock (h); } plen = *(short*)(cptr) + 3; if (SymPtr->flags.bits.archived) { if (!(cptr = malloc (plen))) { if (h) HeapUnlock (h); fatal ("Out of memory"); } memcpy (cptr, fptr, plen); } //Create a struct of type file if(!(fileptr=malloc(sizeof(Files)))) { if (h) HeapUnlock (h); if (cptr != fptr) free (cptr); fatal("Memory error"); } fileptr->fptr=fptr; fileptr->cptr=cptr; fileptr->plen=plen; fileptr->h=h; return fileptr; } Intervale *SetIntervale(Intervale *inter, char *start, char *end) { inter->start = start; inter->end = end; return inter; } Intervale *CreateIntervale(void) { Intervale *inter; inter = (Intervale *)(malloc(sizeof(Intervale))); if(!inter) {printf("Memory error\n"); ngetchx();} SetIntervale(inter, NULL, NULL); return inter; } Intervale *CreateOpenedFileIntervale(Files* fileptr) { Intervale *inter; inter = CreateIntervale(); SetIntervale(inter, (fileptr->fptr), (fileptr->fptr+fileptr->plen)); return inter; } Intervale *CreateOpenedFileTextUsefullIntervale(Files* fileptr) //Avoid memory first 2 byte and the last byte of the file { Intervale *inter; inter = CreateIntervale(); //SetIntervale(inter, (fileptr->fptr)+4, (fileptr->fptr+fileptr->plen)-2); SetIntervale(inter, (fileptr->fptr)+3, (fileptr->fptr+fileptr->plen)-2); return inter; } Intervale *CreateOpenedFilePrgmUsefullIntervale(Files* fileptr) //Avoid memory first 4 byte and the last byte of the file { Intervale *inter; inter = CreateIntervale(); //SetIntervale(inter, (fileptr->fptr)+4, (fileptr->fptr+fileptr->plen)-2); SetIntervale(inter, (fileptr->fptr)+5, (fileptr->fptr+fileptr->plen)-11); return inter; } short GetTextIntervaleLineIntervale(Intervale *FileIntervale, Intervale *LineIntervale) //return NULL if not the last Line { /*This function get the first Line of the intervale FileIntervale, reduce FileIntervale without this Line and return the Line Extracted in LineIntervalle*/ //Extract the readeable Line : ie without comment byte. You must transform the LineIntervale to a string to display it with printf. char *i; i=FileIntervale->start+1; LineIntervale->start=FileIntervale->start+1; //if(i!=20) { printf("No comment allowed in Text File\nWill be ignored\n");} //It is a Protection while(!(*i==0x0D || *i==0x00)) i++; LineIntervale->end=i; if(*i==0) //End of intervale, ie end of the TextFile { FileIntervale->start=i+1; //printf("ici : "); PrintHexIntervale(FileIntervale); ngetchx(); return 1; } else { FileIntervale->start=i+1; return NULL; } } short GetPrgmIntervaleLineIntervale(Intervale *FileIntervale, Intervale *LineIntervale) //return NULL if not the last Line { return GetTextIntervaleLineIntervale(FileIntervale, LineIntervale); } long GetTextIntervaleNumLine(Intervale *inte) //Return the number of Lines of a TextIntervale without modify it. { Intervale copy,i; long n; n=1; copy=*inte; while(!GetTextIntervaleLineIntervale(©, &i)) n++; return n; } long GetPrgmIntervaleNumLine(Intervale *inte) //Return the number of Lines of a PrgmIntervale without modify it. { return GetTextIntervaleNumLine(inte); } //Tools for Intervale structs manipulations Intervale *CopyIntervale(Intervale *dest, Intervale *source) { dest->start = source->start; dest->end = source->end; return dest; } /*Intervale GetStringIntervale(char* string) //end will point on \0 { Intervale inter; inter.start = string; inter.end = (string+strlen(string)); return inter; }*/ long IntervaleSize(Intervale *inter) { return (long)(inter->end - inter->start); } void PrintAsciiIntervale(Intervale* inter) { char *i; printf("Intervale ascii : "); for(i=inter->start;iend;i++) printf("%c",*i); printf("\nSize = %ld\n", IntervaleSize(inter)); } void PrintDecIntervale(Intervale* inter) { char *i; printf("Intervale Dec : "); for(i=inter->start;iend;i++) printf("%d ",*i); printf("\nSize = %ld\n", IntervaleSize(inter)); } void PrintHexIntervale(Intervale* inter) { char *i; printf("Intervale Hex : "); for(i=inter->start;iend;i++) printf("%x ",*i); printf("\nSize = %ld\n", IntervaleSize(inter)); } Intervale *CreateStrIntervale(char *str) { Intervale *inter; inter = (Intervale *)(malloc(sizeof(Intervale))); SetIntervale(inter, str, str + strlen(str)); return inter; } Intervale *CreateListIntervale(char *list, long len) { Intervale *inter; inter = (Intervale *)(malloc(len)); SetIntervale(inter, list, list + len); return inter; } Intervale *CreateTextArrayLastByteIntervale(Array *array) { Intervale *inter; inter = CreateIntervale(); SetIntervale(inter, (array->inter).end - 1, (array->inter).end - 1); return inter; } void DeleteIntervale(Intervale *inter) { if(inter) free(inter); } short IntervaleCmp(Intervale *i1, Intervale *i2) //Return 1 if same content (and size obviously), else return NULL { long size_dif,i,size; size_dif = IntervaleSize(i1) - IntervaleSize(i2); if(size_dif!=0) { return NULL; } else { size = IntervaleSize(i1); for(i=0;istart + i) != *( i2->start + i)) return NULL; } return 1; } } short FoundIntervaleInIntervale(Intervale* big , Intervale* little, Intervale *result) { //Search for the content of little in the content of big. Compare it to strtstr c function. //Return 1 if has been found, else return NULL. Set result as the interval where it has been found in big long size_little; char *i,*i_end; Intervale i_temp; size_little = IntervaleSize(little); if(size_little<=0) { SetIntervale(result, big->end, big->end); return NULL; } else { i_end = big->end - IntervaleSize(little); //Optimisation for speed } for(i=big->start ; i <= i_end ; i++) { SetIntervale(&i_temp, i, i + size_little); if(IntervaleCmp(&i_temp, little)) //If found equal then { CopyIntervale(result, &i_temp); return 1; } } SetIntervale(result, big->end, big->end); return NULL; } Intervale *CopyContentIntervaleToContentIntervale(Intervale *dest, Intervale *source) { long size_source,i; size_source = IntervaleSize(source); if(size_source > IntervaleSize(dest)) { printf("Warning !\nIn CopyContentIntervaleToContentIntervale\ndest must be bigger than source !\n"); ngetchx(); return dest; // Be careffull ! } for(i=0;istart+i) = *(source->start+i); } return dest; } // Array functions /* typedef struct { char *array; Intervale inter; } Array ; */ void PrintAsciiArray(Array *array) { printf("Array :\nPointer array = %lp\n",array->array); PrintAsciiIntervale(&(array->inter)); } void PrintHexArray(Array *array) { printf("Array :\nPointer array = %lp\n",array->array); PrintHexIntervale(&(array->inter)); } void PrintDecArray(Array *array) { printf("Array :\nPointer array = %lp\n",array->array); PrintDecIntervale(&(array->inter)); } Array *CreateArrayFromIntervale(Intervale *inter) { long size; Array *dest; //if(dest!=NULL) return dest; //If the pointer is affected, don't create a new array dest = (Array *)(malloc(sizeof(Array))); if(dest==NULL) { printf("Memory error\n"); ngetchx(); return NULL;} size = IntervaleSize( inter); dest->array = (char*)malloc(size); if(dest->array == NULL) { printf("Memory error\n"); ngetchx(); return NULL;} SetIntervale(&(dest->inter), dest->array, (dest->array)+size); CopyContentIntervaleToContentIntervale(&(dest->inter), inter); //Copy the content of inter in the array return dest; } Array *CreateArrayFromTextFile(char *Filename) //Open the file, copy its content in an array, close the file { Intervale *inter; Array *array; Files *file; file = NULL; file = OpenFile(Filename); if(file==NULL) { printf("Opening file error\n%s\n", Filename); ngetchx(); return NULL;} inter = CreateOpenedFileTextUsefullIntervale(file); array = CreateArrayFromIntervale(inter); DeleteIntervale(inter); CloseOpenedFile(file); return array; } void DeleteArray(Array *array) { if(array) { if(array->array) free(array->array); free(array); } } long GetNumberOfTimeOccuredIntervaleContentInIntervaleContent(Intervale *big, Intervale *little) { Intervale *result,*moving_Intervale; long n; result = CreateIntervale(); moving_Intervale = CreateIntervale(); *moving_Intervale = *big; n=0; while(FoundIntervaleInIntervale(moving_Intervale, little, result)) { n++; //Increment occured counter SetIntervale(moving_Intervale, result->end, moving_Intervale->end); } DeleteIntervale(result); DeleteIntervale(moving_Intervale); return n; } short IsIntervaleInIntervale(Intervale *big, Intervale *little) { if( little->start >= big->start && little->start <= big->end && little->end >= big->start && little->end <= big->end) { return 1; } else return NULL; } short ReplaceOneArrayContentWithIntervaleContent(Array *array, Intervale *little, Intervale *replace) { //Copy the content of replace in big at the little location. bad said, i know... long new_size, dif, dif2; Intervale *big, *new_Intervale; new_Intervale = CreateIntervale(); big = &array->inter; if(IsIntervaleInIntervale(big,little)) { dif = IntervaleSize(replace) - IntervaleSize(little); dif2 = little->start - big->start; new_size = IntervaleSize(&array->inter) + dif; if(dif!=0) array->array = realloc (array->array, new_size); //realloc copy the content to the new space if(!array->array) fatal("replace error"); SetIntervale(new_Intervale, array->array + dif2, array->array + dif2 + IntervaleSize(replace)); if(dif!=0) memmove(new_Intervale->end, array->array + dif2 + IntervaleSize(little), big->end - little->end); SetIntervale(big, array->array, array->array + new_size); CopyContentIntervaleToContentIntervale(new_Intervale, replace); DeleteIntervale(new_Intervale); return 1; } else { DeleteIntervale(new_Intervale); printf("Pas dans l'intervale !\n"); ngetchx(); return NULL; } } short ReplaceArrayContentWithIntervaleContent(Array *array, Intervale *little, Intervale *replace, Intervale *moving) { //Copy the content of replace in big at the little location. bad said, i know... long new_size, dif, dif2; Intervale *big, *new_Intervale; new_Intervale = CreateIntervale(); big = &array->inter; if(IsIntervaleInIntervale(big,little)) { dif = IntervaleSize(replace) - IntervaleSize(little); dif2 = little->start - big->start; new_size = IntervaleSize(&array->inter) + dif; if(dif!=0) array->array = realloc (array->array, new_size); //realloc copy the content to the new space if(!array->array) fatal("replace error"); SetIntervale(new_Intervale, array->array + dif2, array->array + dif2 + IntervaleSize(replace)); if(dif!=0) memmove(new_Intervale->end, array->array + dif2 + IntervaleSize(little), big->end - little->end); SetIntervale(big, array->array, array->array + new_size); SetIntervale(moving, array->array + dif2 + IntervaleSize(replace), array->array + new_size); CopyContentIntervaleToContentIntervale(new_Intervale, replace); DeleteIntervale(new_Intervale); return 1; } else { DeleteIntervale(new_Intervale); printf("Pas dans l'intervale !\n"); ngetchx(); return NULL; } } short ReplaceAllAreaContentFromIntervaleContent(Array *array, Intervale *find, Intervale *replace) { //Find find Content in inter Content and replace it by replace Content Intervale *moving_Intervale, *result, *inter; short i; result = CreateIntervale(); moving_Intervale = CreateIntervale(); inter = &array->inter; *moving_Intervale = *inter; i=0; while(FoundIntervaleInIntervale(moving_Intervale, find, result)) { ReplaceArrayContentWithIntervaleContent(array, result, replace, moving_Intervale); i++; } DeleteIntervale(result); DeleteIntervale(moving_Intervale); return i; } short ReplaceAllAreaContentFromStr(Array *array, char *findstr, char *replacestr) { short n; Intervale *find_Intervale,*replace_Intervale; find_Intervale = CreateStrIntervale(findstr); replace_Intervale = CreateStrIntervale(replacestr); n = ReplaceAllAreaContentFromIntervaleContent(array, find_Intervale, replace_Intervale); DeleteIntervale(find_Intervale); DeleteIntervale(replace_Intervale); return n; } short ArrayToTextFile(Array *array, char *Filename) { HANDLE handle; handle = CreateFile2 (Filename); if(handle != H_NULL) { AppendBlockToFile2(handle, array->array, IntervaleSize(&array->inter)); CloseCreatedFile_type (handle, TEXT_TAG); return 1; } else return 0; } short ReplaceAllTextFileOccuredIntervaleContent(char *Filename, Intervale *inter, Intervale *replace) { Array *source_Array; short n; source_Array = CreateArrayFromTextFile(Filename); n = ReplaceAllAreaContentFromIntervaleContent(source_Array, inter, replace); if(!ArrayToTextFile(source_Array, Filename)) {printf("Replace all error"); ngetchx();} DeleteArray(source_Array); return n; } short ReplaceAllTextFileOccuredStr(char *Filename, char *find, char *replace) { Array *source_Array; Intervale *inter_find,*inter_replace; short n; inter_find = CreateIntervale(); inter_replace = CreateIntervale(); source_Array = CreateArrayFromTextFile(Filename); n = ReplaceAllAreaContentFromStr(source_Array, find, replace); if(!ArrayToTextFile(source_Array, Filename)) {printf("Replace all error"); ngetchx();} DeleteIntervale(inter_find); DeleteIntervale(inter_replace); DeleteArray(source_Array); return n; } short AppendIntervaleContentToTextEndArray(Array* array, Intervale *inter) { short r; Intervale *last_byte; last_byte = CreateIntervale(); SetIntervale(last_byte, (array->inter).end - 1, (array->inter).end - 1); r = ReplaceOneArrayContentWithIntervaleContent(array, last_byte, inter); DeleteIntervale(last_byte); return r; } short AppendStrToTextEndArray(Array* array, char *str) { short r; Intervale *str_inter; str_inter = CreateStrIntervale(str); r = AppendIntervaleContentToTextEndArray(array, str_inter); DeleteIntervale(str_inter); return r; } short AppendTextFileWithStr(char *Filename, char *str) { Array *source_Array; short r; source_Array = CreateArrayFromTextFile(Filename); r = AppendStrToTextEndArray(source_Array, str); if(!ArrayToTextFile(source_Array, Filename)) {printf("Append error"); ngetchx();} DeleteArray(source_Array); return r; } short AppendNewLineBytesToTextArray(Array* array) { short r; Intervale *inter; inter = CreateListIntervale(Text_NewLine, 2); r = AppendIntervaleContentToTextEndArray(array, inter); DeleteIntervale(inter); return r; } short AppendToANewLineTheIntervaleContentToTextEndArray(Array* array, Intervale *inter) { short r; Intervale *last_byte; AppendNewLineBytesToTextArray(array); last_byte = CreateTextArrayLastByteIntervale(array); r = ReplaceOneArrayContentWithIntervaleContent(array, last_byte, inter); DeleteIntervale(last_byte); return r; } short AppendToANewLineStrToTextEndArray(Array *array, char *str) { short r; Intervale *inter; inter = CreateStrIntervale(str); r = AppendToANewLineTheIntervaleContentToTextEndArray(array, inter); DeleteIntervale(inter); return r; } short AppendToANewLineStrToTextFile(char *Filename, char *str) { Array *source_Array; short r; source_Array = CreateArrayFromTextFile(Filename); r = AppendToANewLineStrToTextEndArray(source_Array, str); if(!ArrayToTextFile(source_Array, Filename)) {printf("Append to a New Line error"); ngetchx();} DeleteArray(source_Array); return r; } char Is_alphabetical_char(unsigned char c) //return 1 if c is between a & z or return 0 { if(c>'z' && c<0) return 0; else return 1; } char Is_complete_word(Intervale *inter) //return 1 if inter is a complete word, ie with spaces or newline surrounding the inter { if(Is_alphabetical_char(*(inter->start - 1 )) || Is_alphabetical_char(*(inter->end + 1 ))) return 0; else return 1; } char Is_alone_assimilated_overall_the_Prgm_line(Intervale *line_Prgm_intervale, Intervale *inter) //return whether the line contain an expression alone (only 1 word). return 1 for instance : " start " for instance with space admitted , return 0 for example with " start gotcha" { char *i; for(i = line_Prgm_intervale->start ; i < inter->start ; i++) if(Is_alphabetical_char(*i)) { return 0;} for(i = inter->end + 1 ; i <= line_Prgm_intervale->end ; i++) if(Is_alphabetical_char(*i)) { return 0;} return 1; } /* void preprocesseur(char *source_str) { //ESI arg = top_estack; //char *source_str; File *source; Intervale *source_intervale, *startc_TAG, *endc_TAG, *line_inter, *reducing_inter, *temp_inter1, *temp_inter2; unsigned int n; //long i; //if(GetArgType (arg) != STR_TAG) fatal("String File expected"); //source_str = GetStrnArg(arg); source = OpenFile(source_str); source_intervale = CreateOpenedFilePrgmUsefullIntervale(source); CopyIntervale(reducing_inter, source_intervale); //Copy the prgm interval for manipulation startc_TAG = CreateStrIntervale("startc"); endc_TAG = CreateStrIntervale("endc"); while(!GetTextIntervaleLineIntervale(reducing_inter, line_inter)) //Watch for startc or endc on every line { //line_inter = line captured n=1; //nb lines if(FoundIntervaleInIntervale(line_inter , startc_TAG, temp_inter) == 1) //if startc tag is found then... { //then search for endc tag } { //Search for the content of little in the content of big. Compare it to strtstr c function. //Return 1 if has been found, else return NULL. Set result as the interval where it has been found in big n++; } { //This function get the first Line of the intervale FileIntervale, reduce FileIntervale without this Line and return the Line Extracted in LineIntervalle //Extract the readeable Line : ie without comment byte. You must transform the LineIntervale to a string to display it with printf. printf("Nb line = %ld", GetPrgmIntervaleNumLine(source_intervale)); ngetchx(); PrintAsciiIntervale(source_intervale); ngetchx(); DeleteIntervale(source_intervale); DeleteIntervale(startc_TAG); DeleteIntervale(endc_TAG); ReplaceAllTextFileOccuredStr(source_str, "e", "xXx"); AppendTextFileWithStr(source_str, "OO"); AppendToANewLineStrToTextFile(source_str, "new"); }*/