Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
PRE tag and old messages
Message
 
 
To
25/01/2002 12:50:52
General information
Forum:
Level Extreme
Category:
Other
Miscellaneous
Thread ID:
00609996
Message ID:
00610632
Views:
19
>>BTW, how about Sergey's message? What if it's a C++ code, for example?
>
>Oh, well, I really like assumptions! ;)
>
>Try it. :)
#include <pro_ext.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char FAR *NullTerminate(Value FAR *cVal)
{
	char *RetVal;
	if (!_SetHandSize(cVal->ev_handle, cVal->ev_length + 1))
	{
		_Error(182); // "Insufficient memory"
	}
	
	((char FAR *) _HandToPtr(cVal->ev_handle))[cVal->ev_length] = '\0';
	RetVal = (char FAR *) _HandToPtr(cVal->ev_handle);
	return RetVal; 
}

void FAR ExpandChars(ParamBlk FAR *parm)
{
	/*
	Parameters :
	cInFile  : Input filename
	cOutFile : Output filename
	cStuff   : Char to seek for replacement
	cStuff2  : String to replace char to
	cChars   : Delimiter Char to filter out, optional - Default doublequote
	*/
	const BUF_SIZE = 4096;
	FILE *fchanin, *fchanout ;
	int numRead,i,j,p,sLen;
    BYTE buffer[BUF_SIZE], outbuffer[BUF_SIZE];
	
	// Prepare parameters
	char *filein  = NullTerminate(&parm->p[0].val);		// Input file
	char *fileout = NullTerminate(&parm->p[1].val);		// Output file
	char *cStuffStr = NullTerminate(&parm->p[2].val);
	char cStuff = cStuffStr[0] ;		// Char to seek for replacement
	char *cStuff2 = NullTerminate(&parm->p[3].val);		// String to replace char to
	sLen = strlen(cStuff2);

	char *cChars = "\"" ;                 // Delimiter Char to filter out - Default dblquotes;
	if ( parm->pCount == 5 )
		cChars = NullTerminate(&parm->p[4].val); ;	    // Delimiter Char to filter out
	char ch = cChars[0];
	// Prepare parameters
	
	_HLock(parm->p[0].val.ev_handle);					// Lock handle 
	if ((fchanin = fopen(filein,"r")) < 0)			    // Open input file
	{
		_UserError("Could not open file.");				// Couldn't open file 
	}
	_HUnLock(parm->p[0].val.ev_handle);					// UnLock handle 

	
	_HLock(parm->p[1].val.ev_handle);					// Lock handle 
	if ((fchanout = fopen(fileout,"w")) < 0)			// Open output file
	{
		_UserError("Could not open file.");				// Couldn't open file 
	}
	_HUnLock(parm->p[1].val.ev_handle);					// UnLock handle 
	
	

      // Read in byte blocks,
      // remember how many bytes were actually read,
      // and try to write that many out. This loop ends
      // when there are no more bytes to read.

      do
      {
         numRead = fread( buffer, sizeof( char ), BUF_SIZE, fchanin );
		 for(i=0, j=0;i<numRead;i++)
		 {
			 if ( buffer[i] != ch )
			 {
				 if ( buffer[i] != cStuff )
					 outbuffer[j++] = buffer[i];
				 else
				 {
					 for(p=0;p<sLen;)
						 outbuffer[j++] = cStuff2[p++];
				 }
			 }
		 }
         int numwritten = fwrite( outbuffer, sizeof( char ), j, fchanout );
      }
      while (numRead > 0);



	fclose(fchanout);
	fclose(fchanin);
}


FoxInfo myFoxInfo[] =
{
	{"Char2String", (FPFI) ExpandChars, 5, "CCCC.C"},
};


extern "C" {
FoxTable _FoxTable =
{
	(FoxTable *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};
}
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform