home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / reverse / reverse.c next >
Encoding:
C/C++ Source or Header  |  1988-10-02  |  4.6 KB  |  139 lines

  1. /*=========================================================================*/
  2. /*                              reverse.c                                  */
  3. /*=========================================================================*/
  4. /* Reverse a text file so that the first line becomes the last, etc.       */
  5. /*=========================================================================*/
  6. /*                Copyright (c) 1988  by  James W. Leth                    */
  7. /*=========================================================================*/
  8.  
  9. #include <stdio.h>
  10.  
  11. /* Limiting parameters */
  12. #define VERSION     10
  13. #define MAX_LINES   10000
  14. #define LINE_LENGTH 256
  15.  
  16. /*=========================================================================*/
  17. /* TRACE macros.  If the symbol DEBUG is defined (i.e. via -DDEBUG on the  */
  18. /* command line of the compile command), then these macros will display    */
  19. /* messages on the standard error stream, with or without additional       */
  20. /* fprintf-style arguments.  Otherwise, they will do nothing.  When the    */
  21. /* program is debugged, these statements can all be deactivated by         */
  22. /* compiling without the DEBUG flag set; the statements don't have to      */
  23. /* be removed from the source code.                                        */
  24. /*                                                                         */
  25. /*=========================================================================*/
  26.  
  27. #ifdef DEBUG
  28. #define DOTRACE                 fprintf(stderr, "TRACE:\t")
  29. #define TRACE(msg)              DOTRACE;fprintf(stderr, msg)
  30. #define TRACE1(msg, a)          DOTRACE;fprintf(stderr, msg, a)
  31. #define TRACE2(msg, a, b)       DOTRACE;fprintf(stderr, msg, a, b)
  32. #define TRACE3(msg, a, b, c)    DOTRACE;fprintf(stderr, msg, a, b, c)
  33. #define TRACE4(msg, a, b, c, d) DOTRACE;fprintf(stderr, msg, a, b, c, d)
  34. #else
  35. #define TRACE(msg)
  36. #define TRACE1(msg, a)
  37. #define TRACE2(msg, a, b)
  38. #define TRACE3(msg, a, b, c)
  39. #define TRACE4(msg, a, b, c, d)
  40. #endif
  41.  
  42.  
  43.  
  44. /* This table is used to keep track of the seek position of the source
  45.  * file at the start of each line.
  46.  */
  47. long lineSeek[MAX_LINES];
  48.  
  49.  
  50. void
  51. usage(void)
  52. {
  53.     fprintf(stderr, "REVERSE Version %d.%d\n", VERSION / 10, VERSION % 10);
  54.     fprintf(stderr, "Usage:\treverse source-file target-file\n");
  55.     fprintf(stderr, "\tCopies each line of source-file into target-file,\n");
  56.     fprintf(stderr, "\tin reverse order (the first line becomes the last, etc.).\n");
  57.     fprintf(stderr, "\tCurrent limitations: %d lines, %d chars/line.\n",
  58.                                 MAX_LINES, LINE_LENGTH-1);
  59. }
  60.  
  61.  
  62. /*=========================================================================*/
  63. /*                                                                         */
  64. /*      M A I N                                                            */
  65. /*                                                                         */
  66. /*=========================================================================*/
  67.  
  68. void
  69. main (int argc, char **argv)
  70. {
  71.     char line[LINE_LENGTH];
  72.     FILE *source, *target;
  73.     int lineNum;
  74.     int c;
  75.  
  76.     if (argc != 3)
  77.     {
  78.         usage();
  79.         exit(1);
  80.     }
  81.  
  82.     /* Open the source and target files. */
  83.     if ((source = fopen(argv[1], "r")) == NULL)
  84.     {
  85.         perror("Can't open source file");
  86.         usage();
  87.         exit(1);
  88.     }
  89.     if ((target = fopen(argv[2], "w")) == NULL)
  90.     {
  91.         perror("Can't create target file");
  92.         usage();
  93.         exit(1);
  94.     }
  95.  
  96.     /* Read the source file, building a seek table for the start of each
  97.      * line.
  98.      */
  99.     lineSeek[0] = 0L;
  100.     for (lineNum = 1; lineNum < MAX_LINES;)
  101.     {
  102.         if ((c = getc(source)) == EOF)
  103.         {
  104.             break;
  105.         }
  106.         if (c == '\n')
  107.         {
  108.             lineSeek[lineNum] = ftell(source);
  109.             TRACE2("Line %d found at position %ld\n", lineNum, lineSeek[lineNum]);
  110.             ++lineNum;
  111.         }
  112.     }
  113.  
  114.     /* Now walk backwards through the seek table, copying the lines in
  115.      * reverse order from the source file to the target file.  LineNum
  116.      * now contains 1 more than the highest value for which there is
  117.      * a seek table entry.
  118.      */
  119.  
  120.     while (lineNum-- > 0)
  121.     {
  122.         TRACE2("Seeking line %d at position %ld\n", lineNum, lineSeek[lineNum]);
  123.         if (fseek(source, lineSeek[lineNum], SEEK_SET) != 0)
  124.         {
  125.             perror("Seek failed");
  126.             break;
  127.         }
  128.  
  129.         if (fgets(line,LINE_LENGTH,source) != NULL)
  130.         {
  131.             fputs(line, target);
  132.         }
  133.     }
  134.  
  135.     /* Close the files */
  136.     fclose(source);
  137.     fclose(target);
  138. }
  139.