home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / ctc 1.6 / eol-convert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-11  |  2.7 KB  |  115 lines  |  [TEXT/KAHL]

  1. /* eol-convert.c
  2.  *
  3.  * converts the EOL in TEXT files to the chosen system
  4.  * History...
  5.  * RMF    Deallocated memory on errors, and closed file if error allocating memory.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <Gestalt.h>
  11. #include <Memory.h>
  12. #include "ctc.h"
  13.  
  14.  
  15. #define STRING_TERMINATOR    ('\0')
  16. #define CR    ((unsigned char) 0x0d)
  17. #define LF    ((unsigned char) 0x0a)
  18.  
  19. int process (FILE *path, char *EOL, char *buffer)
  20. {    char *start, *ptr;
  21.  
  22.     start = buffer;
  23.     for (ptr = buffer; *ptr; )
  24.         switch (*ptr) {
  25.             default:    ptr++;    break;
  26.             
  27.             case CR:    /* MAC or DOS end of line has been found */
  28.                 *ptr = STRING_TERMINATOR;
  29.                 fprintf (path, "%s%s", start, EOL);
  30.                 ptr++;
  31.                 if (*ptr == LF) ptr++;    /* skip the LF - eg. MSDOS */
  32.                 start = ptr;
  33.                 break;
  34.                 
  35.             case LF:    /* UNIX end of line has been found */
  36.                 *ptr = STRING_TERMINATOR;
  37.                 fprintf (path, "%s%s", start, EOL);
  38.                 ptr++;
  39.                 start = ptr;
  40.                 break;
  41.             }
  42.             
  43.     if (start < ptr)    /* write the last line, maybe an EOL */
  44.         fprintf (path, "%s", start);
  45.     return 0;
  46. }    /* End of () */
  47.  
  48.  
  49. void ConvertFile (char *thefile, short EOLmode)
  50. {    FILE *path;
  51.     long int length, qty;
  52.     Handle h = nil;            /* RMF - Added support for System 7 tempory memory */
  53.     char *buffer, EOL[6];
  54.     Boolean HasTempMem = false;
  55.     long resp;
  56.     OSErr err;
  57.  
  58.     switch (EOLmode) {
  59.         case DOS:    strcpy (EOL, "\r\n\0"); break;
  60.         case MAC:    strcpy (EOL, "\r\0"); break;
  61.         case UNIX:    strcpy (EOL, "\n\0"); break;
  62.         default:    return;
  63.         }
  64.     
  65.     if ( Gestalt(gestaltOSAttr, &resp) == noErr) {
  66.          HasTempMem = (resp & ( 1 << gestaltTempMemTracked) ) && 
  67.              (resp & ( 1 << gestaltTempMemSupport) ) && 
  68.              (resp & ( 1 << gestaltRealTempMemory) );
  69.             }
  70.                 
  71.     path = fopen (thefile, "rb");
  72.     if (!path) {
  73.         SysBeep(0);
  74.         return;        /* could not open the file for read */
  75.         }
  76.     /*
  77.      * find the length of the file 
  78.      * and allocate a buffer for its contents
  79.      */
  80.     fseek (path, (long int) 0, SEEK_END);
  81.     length = ftell (path);
  82.     fseek (path, (long int) 0, SEEK_SET);
  83.     
  84.     h = nil;
  85.     if (HasTempMem) h = TempNewHandle((Size) length+1, &err);
  86.     if (!h) h = NewHandle ((Size) length+1);
  87.     
  88.     if (!h) goto bailopen;            /* RMF - not enough memory for the buffer */
  89.     buffer = (char *) *h;
  90.     
  91.     buffer[length] = STRING_TERMINATOR;    /* just in case */
  92.     
  93.     /*
  94.      * read the file into the buffer
  95.      */
  96.     qty = fread ( (void *) buffer, (size_t) length, (size_t) 1, path);
  97.     
  98.     /*
  99.      * re-open the file for writing
  100.      * RMF - NOTE: File type and creator get set to: '????' by this routine...
  101.      */
  102.     freopen (thefile, "wb", path);
  103.     if (!path) goto bailopen;        /* could not open the file for write */
  104.     
  105.     fseek (path, (long int) 0, SEEK_SET);
  106.     /*
  107.      * do the conversion of the EOL characters
  108.      */
  109.     process (path, EOL, buffer);
  110.     
  111. bailopen:        /* RMF */
  112.     fclose (path);
  113.     if (h) DisposeHandle(h);
  114. }    /* End of () */
  115.