I was tired of having to work around Pilrc not adding #include guards to 
generated headers. Rather than continue to work around it, I decided to 
see how much effort it would take to fix. It was actually very easy.

At the same time, I put the #defines in natural order instead of 
reversed.

Here are the needed changes. These three functions replace WriteIncFile 
in pilrc.c:

/* Linked list reversal -- linear algorithm. */
static SYM*
ReverseList ( SYM* head )
{
  SYM* currentNode = head;
  SYM* prevNode = NULL;
  while ( currentNode != NULL )
  {
    SYM* temp = currentNode->psymNext;
    currentNode->psymNext = prevNode;
    prevNode = currentNode;
    currentNode = temp;
  }
  return prevNode;
}

/* Convert file name to identifier. Identifier includes
   all letters (in uppercase) and digits, with other
   characters mapped to _. An all-digit identifier is
   safe since we prepend and postpend the identifier
   with _.
   Example:
     Input: Header.h
     Returns: _HEADER_H_ */
static char*
FilenameToID ( char *szFile )
{
  char *guardStr = malloc( strlen( szFile ) + 3 );
  char *guardP = guardStr;
  *guardP++ = '_';
  while ( *szFile )
  {
    int c = *szFile++;
    if ( isalpha( c ) || isdigit( c ) )
    {
      *guardP++ = toupper( c );
    }
    else
    {
      *guardP++ = '_';
    }
  }
  *guardP++ = '_';
  *guardP++ = 0;
  return guardStr;
}

static void
WriteIncFile(char *szFile)
{
  FILE *fh;
  SYM *psym;
  char* guardStr = FilenameToID( szFile );

  fh = fopen(szFile, "wt");
  if (fh == NULL)
    Error3("Unable to open include file:", szFile, strerror(errno));
  if (!vfQuiet)
    printf("writing include file: %s\n", szFile);
  fprintf(fh, "/* pilrc generated file.  Do not edit!*/\n");
  fprintf(fh, "#ifndef %s\n#define %s\n\n", guardStr, guardStr );
  psymFirst = ReverseList( psymFirst );
  for (psym = psymFirst; psym != NULL; psym = psym->psymNext)
  {
    if (psym->fAutoId)
      fprintf(fh, "#define %s %d\n", psym->sz, psym->wVal);
  }
  /* does anyone else use the list? If so, we need to flip
     it around again here. */
  fprintf(fh, "\n#endif /* %s */\n", guardStr );
  free(guardStr);
  fclose(fh);
}


-- 
Steven Fisher; [EMAIL PROTECTED]
"Morituri Nolumus Mori."

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please 
see http://www.palmos.com/dev/support/forums/

Reply via email to