head 1.7; access; symbols; locks sauge:1.7; strict; comment @ * @; 1.7 date 2004.11.20.10.46.35; author sauge; state Exp; branches; next 1.6; 1.6 date 2004.11.20.10.21.38; author sauge; state Exp; branches; next 1.5; 1.5 date 2004.11.20.09.14.12; author sauge; state Exp; branches; next 1.4; 1.4 date 2004.11.20.09.12.22; author sauge; state Exp; branches; next 1.3; 1.3 date 2004.11.20.08.57.46; author sauge; state Exp; branches; next 1.2; 1.2 date 2004.11.20.08.54.26; author sauge; state Exp; branches; next 1.1; 1.1 date 2004.11.20.08.53.34; author sauge; state Exp; branches; next ; desc @Working with no quotes in data @ 1.7 log @Added CleanMem @ text @/************************************************************************/ /* Shared library routine for parsing .csv files */ /* Scott Auge sauge@@amduus.com scott_auge@@yahoo.com */ /************************************************************************/ char *RCS = "$Header: /home/sauge/code/C/importer/prsimport.c,v 1.6 2004/11/20 10:21:38 sauge Exp sauge $"; /* Comment out to turn off or use -DDEBUG in compiler invocation */ /* This should be compiled with -shared on gcc or what ever */ /* creates a shared memory executable on your operating system. */ /* #define DEBUG */ #include #include "prsimport.h" #ifdef DEBUG #include #endif /************************************************************************/ /* Given a line from a .csv file, determine how many entries there are. */ /* Strings are contained in " and " */ /* Quotes are double quoted as in """" is for a string " */ /* Numbers are contained without " and " */ /* Warning: If the data size in the column for the given Line is */ /* greater than the space allocated in Junk, this routine will */ /* wash out! */ /************************************************************************/ int NumColumns (char *Line, char *Junk) { int iNumColumns = 0; /* Loop GetColumn incrementing column number till it complains */ while(1) { if (GetColumn(Line, iNumColumns, Junk) == 0) iNumColumns++; else break; } return iNumColumns; } /* int NumColumns () */ /************************************************************************/ /* Given a line and column number, return the text in that line. Fun- */ /* ction returns the error or no error. */ /* You must allocate the space for Value yourself. */ /************************************************************************/ int GetColumn (char *cpLine, int iColumn, char *cpValue) { typedef enum {FALSE = 0, TRUE = 1} bool; int iCurColumn = 0, iCurLinePos = 0, iCurValuePos = 0; char cCurChar; bool InString = FALSE; int iLineLen = strlen(cpLine); #ifdef DEBUG printf("1:iLineLen=%i\n", iLineLen); #endif /* Find the column we want */ while (1) { /* If we are on the column we want, bail outta here */ if (iCurColumn == iColumn) break; /* Ran out of string - return impossible column number error */ if (iCurLinePos > iLineLen) return -1; /* Grab a character from line and move forward for the next one */ cCurChar = *(cpLine + iCurLinePos); iCurLinePos++; #ifdef DEBUG printf("2:cCurChar = %c, iCurLinePos=%i\n", cCurChar, iCurLinePos); #endif /* If we got a ", we need to know if we are in a string, or if */ /* we are working a "" which is a " in the string. */ /* If we are working a "" then skip over it. If not, then we */ /* are in a string delimiter. Toggle the InString flag. */ if (cCurChar == '"' && *(cpLine + iCurLinePos) == '"') iCurLinePos++; else if (cCurChar == '"') { InString = InString == FALSE ? TRUE : FALSE; continue; } /* If we got a comma and we ain't in a string, then we found */ /* a new column! */ if (cCurChar == ',' && InString == FALSE) iCurColumn++; } /* while () */ /* Start pulling out the data we want */ while (1) { /* Ran out of string - return impossible column number error */ if (iCurLinePos >= iLineLen) return 0; /* Grab a character from line and move forward for the next one */ cCurChar = *(cpLine + iCurLinePos); iCurLinePos++; #ifdef DEBUG printf("2:cCurChar = %c, iCurLinePos=%i\n", cCurChar, iCurLinePos); #endif /* If we got a ", we need to know if we are in a string, or if */ /* we are working a "" which is a " in the string. */ /* If we are working a "" then skip over it. If not, then we */ /* are in a string delimiter. Toggle the InString flag. */ if (cCurChar == '"' && *(cpLine + iCurLinePos) == '"') iCurLinePos++; else if (cCurChar == '"') { InString = InString == FALSE ? TRUE : FALSE; continue; } /* If we got a comma and we ain't in a string, then we found */ /* a new column! We are done! */ if (cCurChar == ',' && InString == FALSE) break; *(cpValue + iCurValuePos) = cCurChar; iCurValuePos++; } /* while () */ return 0; } /* GetColumn () */ /************************************************************************/ /* Progress doesn't provide much to clean up memory once it has been */ /* used. Even unallocating and re-allocating doesn't work to clean */ /* char strings. Run the memptr's through this to clean em out. */ /************************************************************************/ int ClearMem (char *M, int Size) { memset((char *)M, '\0', Size); } /* ClearMem */ @ 1.6 log @Removed junk routine @ text @d6 1 a6 1 char *RCS = "$Header: /home/sauge/code/C/importer/prsimport.c,v 1.5 2004/11/20 09:14:12 sauge Exp sauge $"; d31 1 a31 1 int NumColumns (char *Line, char *Junk, int JunkSize) { d38 1 a38 1 if (GetColumn(Line, iNumColumns, Junk, JunkSize) == 0) d54 1 a54 1 int GetColumn (char *cpLine, int iColumn, char *cpValue, int icpValueSize) { a62 3 /* Clean out of memory the previous value */ memset ((char *)cpValue, '\0', icpValueSize); d147 11 @ 1.5 log @comments explaining compile @ text @d6 1 a6 1 char *RCS = "$Header: /home/sauge/code/C/importer/prsimport.c,v 1.4 2004/11/20 09:12:22 sauge Exp sauge $"; d26 3 d31 1 a31 1 int NumColumns (char *Line) { a34 6 /* Warning: If the data size in the column for the given Line is */ /* greater than the space allocated in Junk, this routine will */ /* wash out! */ char Junk[32000]; d38 1 a38 1 if (GetColumn(Line, iNumColumns, Junk) == 0) d54 1 a54 1 int GetColumn (char *cpLine, int iColumn, char *cpValue) { d63 3 a148 10 /************************************************************************/ /* Sometimes we don't know the max size that Value in GetColumn should */ /* be. This will compute the size of memory needed to store a given */ /* column's data. */ /************************************************************************/ int LenColumn (char *cpLine, int iColumn) { } @ 1.4 log @IT WORKS!!!! YEA!!! @ text @d6 1 a6 1 char *RCS = "$Header: /home/sauge/code/C/importer/prsimport.c,v 1.3 2004/11/20 08:57:46 sauge Exp sauge $"; d9 3 @ 1.3 log @Added large allocation to Junk in NumColumns @ text @d6 1 a6 1 char *RCS = "$Header: /home/sauge/code/C/importer/prsimport.c,v 1.2 2004/11/20 08:54:26 sauge Exp sauge $"; d89 8 a96 4 if (cCurChar == '"') if (!*(cpLine + iCurLinePos + 1) == '"') InString = InString == FALSE ? TRUE : FALSE; d124 9 a132 7 if (cCurChar == '"') if (*(cpLine + iCurLinePos + 1) == '"') iCurLinePos++; else InString = InString == FALSE ? TRUE : FALSE; @ 1.2 log @Added Header for RCS @ text @d6 1 a6 1 char *RCS = "$Header$"; d28 6 a33 1 char Junk[120]; @ 1.1 log @Initial revision @ text @d6 2 @