DeQuote
This sample program provides a fast way to remove the " character from a character string. Several times I've seen people asking for the ability to create comma-separated-value text files that don't have the " delimiters around the character fields. Warning this will break big time if your character fields contain any commas! The strtofile() and filetostr() functions make reading and writing files trivial. The chrtran() function can be used to eliminate the " characters but it's performance on strings over a few kilobytes is horrid. The strtran() function will also do the task quickly, but this code is twice as fast as strtran().
Usage:
declare integer DeQuote in dequote.dll integer nBufferLength, string @ cBuffer lcData = filetostr( "junk.csv" ) lnNewLen = DeQuote( len( lcData ), @lcData ) strtofile( left( lcData, lnNewLen ), "junk1.txt" )
The code compacts the string in place and returns the new length, which is used in the call to left() to get only the dequoted text. This also illustrates how easy it is to create a DLL in Visual C++ and use it from Visual FoxPro.
To use the file unzip it with subdirectory information into a directory, because it will create subdirectories under itself that the VC++ project needs. Download dequote.zip (15K) you can just use the dequote.dll inside the zip file, all of the rest of the files are the VC++ project and source code files.
Here is the member function that does the work:
int CDequoteApp::DeQuote(int nBufferLength, LPSTR cBuffer) { char *cpIn; char *cpOut; cpIn = cBuffer; // start both pointers at the beginning of the buffer cpOut = cpIn; for ( int i = 0; i < nBufferLength; i++, cpIn++ ) { if ( *cpIn != '"' ) // that's a " inside two ' *cpOut++ = *cpIn; // copy the character and advance the output pointer } return ( cpOut - cBuffer ); }