int loadPostHeader(char * postHeaderFilename , struct post * ourPost) { FILE * fp = fopen(postHeaderFilename,"r"); if (fp == 0 ) { fprintf(stderr,"Cannot open loadPostHeader file %s \n",postHeaderFilename); return 0; } char line [LINE_MAX_LENGTH]={0}; //Allocate a token parser struct InputParserC * ipc=0; ipc = InputParser_Create(LINE_MAX_LENGTH,5); if (ipc==0) { fprintf(stderr,"Cannot allocate memory for new loadBoardSettings parser\n"); return 0; } while (!feof(fp)) { //We get a new line out of the file unsigned int readOpResult = (fgets(line,LINE_MAX_LENGTH,fp)!=0); if ( readOpResult != 0 ) { //We tokenize it unsigned int words_count = InputParser_SeperateWords(ipc,line,0); if ( words_count > 0 ) { if (InputParser_WordCompareNoCaseAuto(ipc,0,(char*)"OP")==1) { InputParser_GetWord(ipc,1,ourPost->op,MAX_STRING_SIZE); } else if (InputParser_WordCompareNoCaseAuto(ipc,0,(char*)"IMAGENAME")==1) { ourPost->hasFile=1; InputParser_GetWord(ipc,1,ourPost->fileOriginalName,MAX_STRING_SIZE); } else if (InputParser_WordCompareNoCaseAuto(ipc,0,(char*)"TIMESTAMP")==1) { ourPost->creation.year = InputParser_GetWordInt(ipc,1); ourPost->creation.month = InputParser_GetWordInt(ipc,2); ourPost->creation.day = InputParser_GetWordInt(ipc,3); ourPost->creation.hour = InputParser_GetWordInt(ipc,4); ourPost->creation.minute = InputParser_GetWordInt(ipc,5); ourPost->creation.second = InputParser_GetWordInt(ipc,6); } } } } InputParser_Destroy(ipc); fclose(fp); return 1; }
int main() { //InputParserC is a library to tokenize words from a string "zero,one,two,three(four)" -> "zero" "one" "two" "three" "four"..! printf("Hello World , InputParserC linked version is %s \n",InputParserC_Version()); //To use it you must first allocate a InputParserC structure! struct InputParserC * ipc=0; //After you declared the structure you will have to initialize it to accomodate the data you want to input //The next command initializes our input parser to accomodate a max of 512 different words , which result from 5 seperating delimeters //In the example "zero,one,two,three(four)" the delimeters used are , ( ) and \0 ipc = InputParser_Create(512,5); // The default delimiters are.. printf("The default delimiters are.. "); int i=0; for ( i = 0; i< ipc->max_delimeter_count; i++ ) { printf("%u ascii or %c ",InputParser_GetDelimeter(ipc,i),InputParser_GetDelimeter(ipc,i)); } //We will now tokenize the string "zero,one,two,three(four)" , we set the last parameter to zero to indicate that //input parser should keep a copy of the string , because after the call is done it will be disposed of..! //The return value ( res ) is the number of tokens extracted! int res = InputParser_SeperateWords(ipc,"zero,one,2,three(four)",1); printf("\nInput Parser tokenized a total of %u tokens \n",res); //To retrieve the tokens as a string you will need some memory to store the strings in! //The following word_space has space for a string with up to 128 characters char word_space[128]={0}; //We now want to get the token with number 1 and store it in word_space! //Please note that the tokens are counted from 0 to n-1 where n is the total number of tokens.. /* "zero,one,two,three(four)" 0 1 2 3 4 = a total of n=5 tokens ! */ // The following command will return the second token ( token with number 1 ) and store it to the word_space // that contains up to 128 character strings InputParser_GetWord(ipc,1,word_space,128); printf("InputParser_GetWord returns %s\n",word_space); //If you want you can retrieve an upcase/lowercase version of the string InputParser_GetUpcaseWord(ipc,1,word_space,128); printf("InputParser_GetUpcaseWord returns %s\n",word_space); //If the token is a number you can return an integer! :) i = InputParser_GetWordInt(ipc,2); printf("InputParser_GetWordInt(ipc,2) should return 2 , our build returns %u\n",i); // After you are finished with the InputParser , you should deallocate it to free memory..! InputParser_Destroy(ipc); printf("\n\nThat concludes the rationale tutorial \n Press any key to start XP bug check.. \n\n"); getchar(); // Now run some tests to find bugs , if they return 0 SeperateWordsC should be working ok! return IntermediateTests(); }
int loadPostInfo(struct website * configuration,unsigned int postNum) { char filename[FILENAME_MAX]={0}; snprintf(filename,FILENAME_MAX,"src/Services/MyBlog/res/posts/info%u.html",postNum); fprintf(stderr," Loading post info %u (%s) .. \n",postNum,filename); ssize_t read; FILE * fp = fopen(filename,"r"); if (fp!=0) { struct InputParserC * ipc = InputParser_Create(512,4); InputParser_SetDelimeter(ipc,1,'('); InputParser_SetDelimeter(ipc,2,','); InputParser_SetDelimeter(ipc,3,')'); struct tagItemList tags; char * line = NULL; size_t len = 0; while ((read = getline(&line, &len, fp)) != -1) { //printf("Retrieved line of length %zu :\n", read); //printf("%s", line); InputParser_SeperateWords(ipc,line,0); if ( InputParser_WordCompareNoCaseAuto(ipc,0,"TITLE") ) { InputParser_GetWord(ipc,1,configuration->post.item[postNum].title ,MAX_STR); } else if ( InputParser_WordCompareNoCaseAuto(ipc,0,"DATE") ) { InputParser_GetWord(ipc,1,configuration->post.item[postNum].dateStr,MAX_STR); } else if ( InputParser_WordCompareNoCaseAuto(ipc,0,"AUTHOR") ) { InputParser_GetWord(ipc,1,configuration->post.item[postNum].author ,MAX_STR); } //if ( InputParser_WordCompareNoCaseAuto(ipc,0,"TAGS") ) { InputParser_GetWord(ipc,0,configuration->post.item[postNum].tags ,MAX_STR); } else //if ( InputParser_WordCompareNoCaseAuto(ipc,0,"COMMENTS") ) { InputParser_GetWord(ipc,0,line,MAX_STR); } } fprintf(stderr," Post Info %u --------------\n",postNum); fprintf(stderr," Title : %s \n",configuration->post.item[postNum].title); fprintf(stderr," Date : %s \n",configuration->post.item[postNum].dateStr); fprintf(stderr," Author : %s \n",configuration->post.item[postNum].author); fclose(fp); if (line) { free(line); } InputParser_Destroy(ipc); return 1; } return 0; }
void ParseString(struct InputParserC * ipc,char * thestr) { int i,z; char word_space[max_ret_word]={0}; printf("\n\nParsing a string.. ( %s ) \n",thestr); time_t msec = time(NULL) * 1000; int res = InputParser_SeperateWords(ipc,thestr,0); time_t msec2 = time(NULL) * 1000; printf("Words Seperated ( %u ) at %u msec.. ",res,(unsigned int) (msec2-msec)); printf("Int Check "); for (i=0; i<res; i++) { z=InputParser_GetWordInt(ipc,i); printf(" %u = %u ",i,z); } printf("\n\n"); printf("Length Check "); for (i=0; i<res; i++) { printf(" %u = %u ",i,InputParser_GetWordLength(ipc,i)); } printf("\n\n"); printf("String Check "); for (i=0; i<res; i++) { InputParser_GetWord(ipc,i,word_space,max_ret_word); printf(" %u = %s ",i,word_space); } printf("\n\n"); printf("Upcase String Check "); for (i=0; i<res; i++) { InputParser_GetUpcaseWord(ipc,i,word_space,max_ret_word); printf(" %u = %s ",i,word_space); } printf("\n\n"); printf("Lowercase String Check "); for (i=0; i<res; i++) { InputParser_GetLowercaseWord(ipc,i,word_space,max_ret_word); printf(" %u = %s ",i,word_space); } printf("\n\n"); printf("Erroneous String Check "); strcpy(word_space,""); for (i=res; i<res+2; i++) { InputParser_GetWord(ipc,i,word_space,max_ret_word); printf(" %u = %s ",i,word_space); } printf("\n\n"); }
int processCommand(struct InputParserC * ipc , struct motionStats * st ,char * line , unsigned int words_count , unsigned int startAtFrame) { if (InputParser_WordCompareAuto(ipc,0,"MOVE")) { int frameNum = InputParser_GetWordInt(ipc,2); if (frameNum<startAtFrame) { return 0; } float x = InputParser_GetWordFloat(ipc,3); float y = InputParser_GetWordFloat(ipc,4); float z = InputParser_GetWordFloat(ipc,5); float qW = InputParser_GetWordFloat(ipc,6); float qX = InputParser_GetWordFloat(ipc,7); float qY = InputParser_GetWordFloat(ipc,8); float qZ = InputParser_GetWordFloat(ipc,9); updatePosition(st,x,y,z,qW,qX,qY,qZ); } else if (InputParser_WordCompareAuto(ipc,0,"POSE")) { int frameNum = InputParser_GetWordInt(ipc,2); if (frameNum<startAtFrame) { return 0; } //fprintf(stdout,"Found Frame %u \n",InputParser_GetWordInt(ipc,1)); char str[128]; if (InputParser_GetWord(ipc,3,str,128)!=0) {//Flush next frame float x = InputParser_GetWordFloat(ipc,4); float y = InputParser_GetWordFloat(ipc,5); float z = InputParser_GetWordFloat(ipc,6); unsigned int where2work=0; if ( getJointMemoryID(st,str,&where2work) ) { updateJoint(st,where2work,x,y,z); } //fprintf(stdout," %0.2f %0.2f %0.2f \n",x,y,z); return 1; } } return 0; }
int loadPageInfo(struct website * configuration,unsigned int postNum) { char filename[FILENAME_MAX]={0}; snprintf(filename,FILENAME_MAX,"src/Services/MyBlog/res/pages/info%u.html",postNum); fprintf(stderr," Loading widget info %u (%s) .. \n",postNum,filename); ssize_t read; FILE * fp = fopen(filename,"r"); if (fp!=0) { struct InputParserC * ipc = InputParser_Create(512,4); InputParser_SetDelimeter(ipc,1,'('); InputParser_SetDelimeter(ipc,2,','); InputParser_SetDelimeter(ipc,3,')'); struct tagItemList tags; char * line = NULL; size_t len = 0; while ((read = getline(&line, &len, fp)) != -1) { InputParser_SeperateWords(ipc,line,0); if ( InputParser_WordCompareNoCaseAuto(ipc,0,"TITLE") ) { InputParser_GetWord(ipc,1,configuration->pages.item[postNum].title ,MAX_STR); } } fprintf(stderr," Page Title %u --------------\n",postNum); fprintf(stderr," Title : %s \n",configuration->pages.item[postNum].title); fclose(fp); if (line) { free(line); } InputParser_Destroy(ipc); return 1; } return 0; }
int readVirtualStream(struct VirtualStream * newstream) { #if USE_FILE_INPUT #if PRINT_DEBUGGING_INFO fprintf(stderr,"readVirtualStream(%s) called \n",newstream->filename); #endif //Our stack variables .. unsigned int readOpResult = 0; char line [LINE_MAX_LENGTH]={0}; //Try and open filename FILE * fp = fopen(newstream->filename,"r"); if (fp == 0 ) { fprintf(stderr,"Cannot open trajectory stream %s \n",newstream->filename); return 0; } //Find out the size of the file..! fseek (fp , 0 , SEEK_END); unsigned long lSize = ftell (fp); rewind (fp); fprintf(stderr,"Opening a %lu byte file %s \n",lSize,newstream->filename); //Allocate a token parser struct InputParserC * ipc=0; ipc = InputParser_Create(LINE_MAX_LENGTH,5); if (ipc==0) { fprintf(stderr,"Cannot allocate memory for new stream\n"); return 0; } newstream->fileSize = lSize; //Add a dummy CAMERA Object here! growVirtualStreamObjectsTypes(newstream,OBJECT_TYPES_TO_ADD_STEP); strcpy( newstream->objectTypes[0].name , "camera" ); strcpy( newstream->objectTypes[0].model , "camera" ); ++newstream->numberOfObjectTypes; growVirtualStreamObjects(newstream,OBJECTS_TO_ADD_STEP); strcpy( newstream->object[0].name, "camera"); strcpy( newstream->object[0].typeStr, "camera"); strcpy( newstream->object[0].value, "camera"); newstream->object[0].type = 0; //Camera newstream->object[0].R =0; newstream->object[0].G =0; newstream->object[0].B =0; newstream->object[0].Transparency=0; ++newstream->numberOfObjects; // CAMERA OBJECT ADDED newstream->debug=0; //Everything is set , Lets read the file! while (!feof(fp)) { //We get a new line out of the file readOpResult = (fgets(line,LINE_MAX_LENGTH,fp)!=0); if ( readOpResult != 0 ) { //We tokenize it unsigned int words_count = InputParser_SeperateWords(ipc,line,0); if ( words_count > 0 ) { if (InputParser_WordCompareNoCase(ipc,0,(char*)"DEBUG",5)==1) { fprintf(stderr,"DEBUG Mode on\n"); newstream->debug=1; } else /*! REACHED AN AUTO REFRESH DECLERATION ( AUTOREFRESH(1500) ) argument 0 = AUTOREFRESH , argument 1 = value in milliseconds (0 = off ) */ if (InputParser_WordCompareNoCase(ipc,0,(char*)"AUTOREFRESH",11)==1) { newstream->autoRefresh = InputParser_GetWordInt(ipc,1); } else /*! REACHED AN INTERPOLATE TIME SWITCH DECLERATION ( INTERPOLATE_TIME(1) ) argument 0 = INTERPOLATE_TIME , argument 1 = (0 = off ) ( 1 = on )*/ if (InputParser_WordCompareNoCase(ipc,0,(char*)"INTERPOLATE_TIME",16)==1) { //The configuration INTERPOLATE_TIME is the "opposite" of this flag ignore time newstream->ignoreTime = InputParser_GetWordInt(ipc,1); // so we flip it here.. , the default is not ignoring time.. if (newstream->ignoreTime == 0 ) { newstream->ignoreTime=1; } else { newstream->ignoreTime=0; } } else /*! REACHED AN BACKGROUND DECLERATION ( BACKGROUND(0,0,0) ) argument 0 = BACKGROUND , argument 1 = R , argument 2 = G , argument 3 = B , */ if (InputParser_WordCompareNoCase(ipc,0,(char*)"BACKGROUND",10)==1) { //The configuration INTERPOLATE_TIME is the "opposite" of this flag ignore time newstream->backgroundR = (float) InputParser_GetWordInt(ipc,1) / 255; newstream->backgroundG = (float) InputParser_GetWordInt(ipc,2) / 255; newstream->backgroundB = (float) InputParser_GetWordInt(ipc,3) / 255; // so we flip it here.. , the default is not ignoring time.. } else /*! REACHED AN OBJECT TYPE DECLERATION ( OBJECTTYPE(spatoula_type,"spatoula.obj") ) argument 0 = OBJECTTYPE , argument 1 = name , argument 2 = value */ if (InputParser_WordCompareNoCase(ipc,0,(char*)"OBJECTTYPE",10)==1) { char name[MAX_PATH]={0}; char model[MAX_PATH]={0}; InputParser_GetWord(ipc,1,name,MAX_PATH); InputParser_GetWord(ipc,2,model,MAX_PATH); addObjectTypeToVirtualStream( newstream , name, model ); } else /*! REACHED AN OBJECT DECLERATION ( OBJECT(something,spatoula_type,0,255,0,0,0,1.0,spatoula_something) ) argument 0 = OBJECT , argument 1 = name , argument 2 = type , argument 3-5 = RGB color , argument 6 Transparency , argument 7 = No Color , argument 8 = Scale argument 9 = String Freely formed Data */ if (InputParser_WordCompareNoCase(ipc,0,(char*)"OBJECT",6)==1) { char name[MAX_PATH]={0} , typeStr[MAX_PATH]={0}; InputParser_GetWord(ipc,1,name,MAX_PATH); InputParser_GetWord(ipc,2,typeStr,MAX_PATH); unsigned char R = (unsigned char) InputParser_GetWordInt(ipc,3); unsigned char G = (unsigned char) InputParser_GetWordInt(ipc,4); unsigned char B = (unsigned char) InputParser_GetWordInt(ipc,5); unsigned char Alpha = (unsigned char) InputParser_GetWordInt(ipc,6) ; unsigned char nocolor = (unsigned char) InputParser_GetWordInt(ipc,7); float scale = (float) InputParser_GetWordFloat(ipc,8); //Value , not used : InputParser_GetWord(ipc,8,newstream->object[pos].value,15); addObjectToVirtualStream(newstream ,name,typeStr,R,G,B,Alpha,nocolor,0,0,scale); } else /*! REACHED A POSITION DECLERATION ( POS(hand,0, 0.0,0.0,0.0 , 0.0,0.0,0.0,0.0 ) ) argument 0 = POS , argument 1 = name , argument 2 = time in MS , argument 3-5 = X,Y,Z , argument 6-9 = Rotations*/ if (InputParser_WordCompareNoCase(ipc,0,(char*)"POS",3)==1) { char name[MAX_PATH]; InputParser_GetWord(ipc,1,name,MAX_PATH); unsigned int time = InputParser_GetWordInt(ipc,2); float pos[7]={0}; pos[0] = InputParser_GetWordFloat(ipc,3); pos[1] = InputParser_GetWordFloat(ipc,4); pos[2] = InputParser_GetWordFloat(ipc,5); pos[3] = InputParser_GetWordFloat(ipc,6); pos[4] = InputParser_GetWordFloat(ipc,7); pos[5] = InputParser_GetWordFloat(ipc,8); pos[6] = InputParser_GetWordFloat(ipc,9); int coordLength=7; addPositionToObject( newstream , name , time , (float*) pos , coordLength ); } else /*! REACHED A PROJECTION MATRIX DECLERATION ( PROJECTION_MATRIX( ... 16 values ... ) ) argument 0 = PROJECTION_MATRIX , argument 1-16 matrix values*/ if (InputParser_WordCompareNoCase(ipc,0,(char*)"PROJECTION_MATRIX",17)==1) { newstream->projectionMatrixDeclared=1; int i=1; for (i=1; i<=16; i++) { newstream->projectionMatrix[i-1] = (double) InputParser_GetWordFloat(ipc,i); } fprintf(stderr,"Projection Matrix given to TrajectoryParser\n"); } else /*! REACHED A MODELVIEW MATRIX DECLERATION ( MODELVIEW_MATRIX( ... 16 values ... ) ) argument 0 = MODELVIEW_MATRIX , argument 1-16 matrix values*/ if (InputParser_WordCompareNoCase(ipc,0,(char*)"MODELVIEW_MATRIX",16)==1) { newstream->modelViewMatrixDeclared=1; int i=1; for (i=1; i<=16; i++) { newstream->modelViewMatrix[i-1] = (double) InputParser_GetWordFloat(ipc,i); } fprintf(stderr,"ModelView Matrix given to TrajectoryParser\n"); } } // End of line containing tokens } //End of getting a line while reading the file } fclose(fp); InputParser_Destroy(ipc); return 1; #else fprintf(stderr,RED "This build of Trajectory parser does not have File Input compiled in!\n" NORMAL); fprintf(stderr,YELLOW "Please rebuild after setting USE_FILE_INPUT to 1 on file TrajectoryParser.cpp or .c\n" NORMAL); fprintf(stderr,YELLOW "also note that by enabling file input you will also need to link with InputParser_C.cpp or .c \n" NORMAL); fprintf(stderr,YELLOW "It can be found here https://github.com/AmmarkoV/InputParser/ \n" NORMAL); #endif return 0; }
/* void InputParser::SetWord(int num,char * tmp) { } */ unsigned int InputParser::GetWord(int num,char * thestr,unsigned int thestrsize) { return InputParser_GetWord(ipc,num,thestr,thestrsize); }
int loadSite( char * filename ) { boardHashMap = hashMap_Create( 100 , 100 , 0 , 1 /*We should have sorting enabled..!*/ ); threadHashMap = hashMap_Create( 10000 , 1000 , 0 , 1 /*We should have sorting enabled..!*/ ); unsigned int numberOfElements=0; char what2GetBack[1024]={0}; AmmServer_ExecuteCommandLine("ls data/board -al | cut -d ' ' -f10 | wc -l ", what2GetBack , 1024 ); numberOfElements = atoi(what2GetBack); ourSite.boards = (struct board * ) malloc(sizeof(struct board) * MAX_BOARDS); if (ourSite.boards == 0 ) { fprintf(stderr,"Cannot allocate memory to hold boards , failed to load "); return 0; } ourSite.maxNumberOfBoards = MAX_BOARDS; ourSite.numberOfBoards = 0; strncpy(ourSite.siteName ,filename ,MAX_STRING_SIZE ); threadIndexPage = AmmServer_ReadFileToMemoryHandler("data/simple.html"); //------------------------------------------------------ char line [LINE_MAX_LENGTH]={0}; //Try and open filename FILE * fp = fopen(filename,"r"); if (fp == 0 ) { fprintf(stderr,"Cannot open trajectory stream %s \n",filename); return 0; } //Allocate a token parser struct InputParserC * ipc=0; ipc = InputParser_Create(LINE_MAX_LENGTH,5); if (ipc==0) { fprintf(stderr,"Cannot allocate memory for new stream\n"); return 0; } while (!feof(fp)) { //We get a new line out of the file unsigned int readOpResult = (fgets(line,LINE_MAX_LENGTH,fp)!=0); if ( readOpResult != 0 ) { //We tokenize it unsigned int words_count = InputParser_SeperateWords(ipc,line,0); if ( words_count > 0 ) { if (InputParser_WordCompareNoCase(ipc,0,(char*)"SITENAME",8)==1) { InputParser_GetWord(ipc,1, ourSite.siteName , MAX_STRING_SIZE ); } else if (InputParser_WordCompareNoCase(ipc,0,(char*)"SITEDESCRIPTION",8)==1) { InputParser_GetWord(ipc,1, ourSite.siteDescription , MAX_STRING_SIZE ); } } } } InputParser_Destroy(ipc); fclose(fp); DIR *dp; struct dirent *ep; dp = opendir ("data/board"); if (dp != NULL) { while (ep = readdir (dp)) { if (strcmp(ep->d_name,".")==0) { } else if (strcmp(ep->d_name,"..")==0) { } else { //fprintf(stderr,"Adding board %s \n",ep->d_name); addBoardToSite( &ourSite , ep->d_name ); } } closedir (dp); } else { fprintf(stderr,"Cannot open directory to list channels \n"); } return 1; }