GenerateReInitArray.cpp

 #include "GenerateReInitArray.h"

const char *Keyword_1 = "static";
const char *Keyword_2 = "PegMenuDescription";
const char *Keyword_3 = "PEGCHAR";
const char *Keyword_4 = "(PEGCHAR*)";

#define openbrace '{'
#define closebrace '}'
#define hashsymbol '#'
#define comma ','

const char *openbracket = "[";
const char *semicolon = ";";

unsigned short ReadTillNewLineSymbol(char *tempVar1, char *tempLine);


/********************************************************************************************
Class : GenerateReInitArray
Function : Constructor
Remarks  : This function initialzes the variables.
********************************************************************************************/
GenerateReInitArray::GenerateReInitArray(char **argv):
SourceFile(NULL), TargetFile(NULL), hashIfFlag(false),
    hashElseFlag(false), hashEndFlag(false)
{
*ReInitData = 0;
*CompleteReInitData = 0;
fileName = argv[1];
TargetFileName = new char[ strlen(fileName) ];
OpenFile();
ReadFile();
DumpDataToFile();
}
/********************************************************************************************
Class : GenerateReInitArray
Function : Destructor
Remarks  : This function destructs the variables.
********************************************************************************************/
GenerateReInitArray::~GenerateReInitArray()
{
}
/********************************************************************************************
Class : GenerateReInitArray
Function : OpenFile
Remarks  : This function opens the source and target files.
********************************************************************************************/
void GenerateReInitArray::OpenFile()
{

if ( (SourceFile = fopen(fileName, "r")) == NULL )
{
printf("Error: Cannot open %s\n", fileName);
exit(1);
}

strcpy (TargetFileName, GetFileNameWithoutExtn());
strcat (TargetFileName, ".txt");

if ( (TargetFile = fopen(TargetFileName, "w")) == NULL)
{
printf("Error: Cannot open target file %s\n",TargetFileName);
exit(1);
}
}
/********************************************************************************************
Class : GenerateReInitArray
Function : CloseFile
Remarks  : This function returns closes the file.
********************************************************************************************/
void GenerateReInitArray::CloseFile()
{
fclose(SourceFile);
fclose(TargetFile);
}

/********************************************************************************************
Class : GenerateReInitArray
Function : GetFileNameWithoutExtn
Remarks  : This function returns just the name of the file. It strips the extension name
   from the file.
********************************************************************************************/
char* GenerateReInitArray::GetFileNameWithoutExtn()
{
char dot[2] = ".";
char *tempfileName; //Assuming the fileName size is less than 32 alphabets
tempfileName = strtok ( GenerateReInitArray::fileName, dot );
return tempfileName;
}

/********************************************************************************************
Class : GenerateReInitArray
Function : ReadFile
Remarks  : This function .
********************************************************************************************/
bool GenerateReInitArray::ReadFile()
{
ReadLine();
while (*line != NULL)
{
// Check for Keyword_1, Keyword_2 and Keyword_3 in this line, if they occur, then
// store the variable name after Keyword_2/3. This is the name of the
// PegMenuDescription array.
bool ret = GetArrayName();
if (ret == false)
{
//Discard the line because the line is not related to PegMenuDescription Array
ReadLine();
continue;
}
else {
//Get all lines till a delimiter is not found. The delimiter is SEMI COLON.
GetReInitArrayData();
//Process the lines and store them in a buffer.
ProcessReInitArrayData();
DumpDataToBuffer();
FlushData();
ReadLine();
}
}
return true;
}

/********************************************************************************************
Class : GenerateReInitArray
Function : GetArrayName
Remarks  : This function returns the PegMenuDescription array name after stripping rest data.
********************************************************************************************/
bool GenerateReInitArray::GetArrayName()
{
char tempStaticKeyword[16];
char tempPegMenuKeyword[32];
char tempStarKeyword[8];
char *tempVar1;

if (strstr (line, Keyword_1) != NULL) //Indicates first keyword is found
{
//Indicates second keyword is found
if(strstr (line, Keyword_2) != NULL)
{
//Check for Keyword 2 or Keyword 3, if Keyword_3, then the sscanf routine will have
// ome more temp var to hold the *
//Store the name of the PegMenuDescription array in variable ArrayName
sscanf(line, "%s %s %s",tempStaticKeyword, tempPegMenuKeyword, ArrayName);
tempVar1 = strtok(ArrayName, "[");
strcpy (ArrayName, tempVar1);
return true;
} else if (strstr (line, Keyword_3) != NULL) {
//Skip the Star now
sscanf(line, "%s %s %s %s",tempStaticKeyword, tempPegMenuKeyword, tempStarKeyword, ArrayName);
tempVar1 = strtok(ArrayName, "[");
strcpy (ArrayName, tempVar1);
return true;
}
else {
return false;
}
} else {
return false;
}
}

/********************************************************************************************
Class : GenerateReInitArray
Function : GetReInitArrayData
Remarks  : This function collects the complete ReInitMenu data from the
   PegMenuDescription Array. The function returns the data till the delimiter SEMI
   COLON is not found
********************************************************************************************/
void GenerateReInitArray::GetReInitArrayData()
{
bool DelimiterFound = true;
char *tmpVar1;
bool AppendDataFlag = true;

while (DelimiterFound == true)
{
tmpVar1 = strstr (line, ";");
if(tmpVar1 != NULL) //indicates that SEMICOLON is found
{
DelimiterFound = false;
strcat(ReInitData, line);
}
else
{
if(AppendDataFlag == true) {
strcpy(ReInitData, line);
AppendDataFlag = false;
}
else {
strcat(ReInitData, line);
}
ReadLine();
}
}
}
/********************************************************************************************
Class : GenerateReInitArray
Function : ProcessReInitArrayData
Return : void
Remarks  : This function collects the processes the raw ReInitData got from the function
   GetReInitArrayData. The function dumps the ReInitMenu function data in the array
   ReInitData array.
********************************************************************************************/
void GenerateReInitArray::ProcessReInitArrayData()
{
char tempVar1[MAX_REINIT_DATA];
char tempLine[MAX_LINE_LENGTH];
char *tempPtr;
char ProcessedReInitData[MAX_REINIT_DATA];
unsigned char i = 0;
unsigned char j;
unsigned short LineLength = 0;

*ProcessedReInitData = 0;

strcpy(tempVar1, ReInitData);
tempPtr = tempVar1;

while( *tempPtr != NULL )
{
LineLength = ReadTillNewLineSymbol ( tempPtr, tempLine );

//Anything that is succeeds after # has to be displayed as such. This is a macro.
if ( SearchHashSymbol(tempLine) )
{
sprintf(ProcessedReInitData, "%s\n\t%s",ProcessedReInitData, tempLine);
*tempLine = 0; //ReInitialize tempLine
//Increment the tempPtr to point to next line, +1 is done to omit the new line char
tempPtr = tempPtr + LineLength + 1;
if (hashElseFlag == true || hashEndFlag == true)
{
RestoreFlags();
}
continue ;
}

if ( GetStringIdName( tempLine ) )
{
//The Array Index has to be changed if #if or #else symbol is found
if (hashIfFlag == false && hashElseFlag == false && hashEndFlag == false) //Indicates that this is normal line
{
sprintf(ProcessedReInitData, "%s\n\t%s[%d].ptext = %s;",ProcessedReInitData, ArrayName, i, StringIdName);
j = i++;
}
else {
//Process starts for Array Index
// Till #else is not found, we will use j for keeping the ArrayIndex
// Once #else is found, restore i
sprintf(ProcessedReInitData, "%s\n\t%s[%d].ptext = %s;",ProcessedReInitData, ArrayName, ++j, StringIdName);
//j++;
}

//ReInitialize StringIdName
*(GenerateReInitArray::StringIdName) = 0;
}

*tempLine = 0; //ReInitialize tempLine
//Increment the tempPtr to point to next line, +1 is done to omit the new line char
tempPtr = tempPtr + LineLength + 1;
}

// The processed ReInitData is copied back to the ReInitData Array
strcpy(ReInitData, ProcessedReInitData);
}

/**********************************************************************************
Class Name : GenerateReInitArray
Function Name : GetStringIdName
Parameters : tempLine, the line where string id's are searched
Returns : true if the string id's are found, false otherwise


Remarks : The function searches for strings and string id's in the line. The
  design methodology for this module is as below

Conditions : a) Strings or string id's occur after two consecutive open braces
  b) (PEGCHAR*) keyword always preceeds strings or string Id's
: a) Strings or string id's occur after 2 consective open braces but
they occur in 2 subsequent lines
  b) (PEGCHAR*) keyword always preceeds strings or string Id's
: a) When the string id occurs in 2 sunsequent lines, it could be the
  the second or more subscript.
**********************************************************************************/
bool GenerateReInitArray::GetStringIdName(char *tempLine)
{
char *tmpPtr;
char *StringIdPtr;

tmpPtr = strstr(tempLine, Keyword_4); 

//If tmpPtr is returned NULL, it indicates end of buffer
if(tmpPtr == NULL)
return false;
/*
//This one is optional, it does not occur for the array's 2nd or more subsripts
tmpPtr = strstr(tmpPtr,   "{");

if(tmpPtr != NULL)
{
tmpPtr++; //Eat '{'
}
else
{
// Restore tmpPtr, but check for End of Line before doing this.
// if it is end of line, then the String Id Name is not found, it
// exists in the next subsequent line
if(*tmpPtr == NULL)
{
return false;
}
else {
tmpPtr = strstr(tempLine, "{");  //Restore the tmpPtr
tmpPtr++; //Eat '{'
}
}*/

// Next delimiter is the COMMA, between this is our STRING or STRINGID, put it
// in StringIdName
StringIdPtr = GenerateReInitArray::StringIdName;

while (*tmpPtr != comma && *tmpPtr != closebrace)
{
*StringIdPtr = *tmpPtr;
tmpPtr++; StringIdPtr++; //Update the StringIdName
}
*StringIdPtr = 0; //To make StringIdPtr a NULL terminated string
return true;
}

/**********************************************************************************
Function Name : SearchHashSymbol
Parameters : tempLine, the line where symbols are searched
Returns : true if the symbol is found, false otherwise
Remarks : The function searches for #ifdef, #ifndef, #else and #endif
  symbols in the line
**********************************************************************************/
bool GenerateReInitArray::SearchHashSymbol( char *tempLine)
{
if( (strstr(tempLine, "#ifdef")) != NULL)
{
hashIfFlag = true;
return true;
}

if( (strstr(tempLine, "#ifndef")) != NULL)
{
hashIfFlag = true;
return true;
}

if( (strstr(tempLine, "#else")) != NULL)
{
hashElseFlag = true;
return true;
}

if( (strstr(tempLine, "#endif")) != NULL)
{
hashEndFlag = true;
return true;
}

return false;
}

/**********************************************************************************
Class Name : GenerateReInitArray
Function Name : RestoreFlags
Parameters : None
Remarks : The function restores the hash flags. These flags keep the count
  on the ArrayIndex. When they are restored, it indicates that the
  program counter has passed the #endif symbol, so now we have to
  restore the flags for the next hash symbol
**********************************************************************************/
void GenerateReInitArray::RestoreFlags()
{
hashIfFlag = false;
hashElseFlag = false;
hashEndFlag  = false;
}
/**********************************************************************************
Function Name : ReadTillNewLineSymbol
Parameters : tempVar1, this is the string from which the line has to be taken
: tempLine, this is the memory, where the line will be written
Return : Returns the length of line.
: Returns the line in parameter, tempLine. The returned string is
  a NULL terminated string
Remarks : The function inputs the PegMenuDescription array and returns
  one line.
**********************************************************************************/
unsigned short ReadTillNewLineSymbol(char *tempVar1, char *tempLine)
{
char *tmp;
char *PtrTotmpline;
unsigned short LineLength = 0;

PtrTotmpline = tempLine;

tmp = tempVar1;
while (*tmp != '\n')
{
*PtrTotmpline = *tmp;
tmp++;
PtrTotmpline++;
LineLength++;
}

*PtrTotmpline = 0; //Make 'tempLine' a NULL terminated string

return LineLength;
}

/**********************************************************************************
Class Name : GenerateReInitArray
Function Name : DumpDataToBuffer
Returns : void
Remarks : The function copies the ReInitData buffer to the Main buffer.
  This main buffer will be copied to the text file.
**********************************************************************************/
void GenerateReInitArray::DumpDataToBuffer()
{
sprintf(CompleteReInitData,"%s\n\t%s\n",CompleteReInitData, ReInitData);
}

/**********************************************************************************
Class Name : GenerateReInitArray
Function Name : FlushData
Returns : void
Remarks : The function reinitializes the temporary buffers.
**********************************************************************************/
void GenerateReInitArray::FlushData()
{
*(GenerateReInitArray::ReInitData) = 0;
*(GenerateReInitArray::StringIdName) = 0;
*(GenerateReInitArray::ArrayName) = 0;
*(GenerateReInitArray::line) = 0;
}

/**********************************************************************************
Class Name : GenerateReInitArray
Function Name : DumpDataToFile
Returns : void
Remarks : The function dumps the CompleteReInitData to the Target file
**********************************************************************************/
void GenerateReInitArray::DumpDataToFile()
{
//Dump the header for ReInitMenu
fprintf (TargetFile, "void %s::ReInitMenu()\n{\n\t", GetFileNameWithoutExtn());

//while( *CompleteReInitData != NULL ) //Dump till the Complete ReInitData is not empty
//{
fprintf (TargetFile,"%s",CompleteReInitData);
//}

//Dump the footer for ReInitMenu
fprintf(TargetFile, "\n}\n");
CloseFile();
}

/**********************************************************************************
Class Name : GenerateReInitArray
Function Name : ReadLine
Parameter : None
Returns : true, if fgets returns the line, false, otherwise
Remarks : The function reads a single line of the SourceFile. It updates the
: data member 'line' with the got line.
**********************************************************************************/
bool GenerateReInitArray::ReadLine()
{
char* ret = fgets(line, MAX_LINE_LENGTH, SourceFile);
if (ret == NULL)
{
*line = 0;
return false;
}
else
return true;
}

/**************************END*OF*FILE*********************************************/

Project Homepage: