home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / MISC / stf.lzh / renum.c < prev    next >
C/C++ Source or Header  |  1994-04-24  |  7KB  |  256 lines

  1. /************************************************************/
  2. /* renum.c                                                  */
  3. /************************************************************/
  4. /* this program will renumber a stfortune style quote file. */
  5. /************************************************************/
  6. /* written by Scott McGee 2/27/93                           */
  7. /************************************************************/
  8. /* syntax:                                                  */
  9. /*         renum infile outfile                             */
  10. /*         (if outfile is not specified, stdout is used)    */
  11. /*         (if infile is not specified, stdin is used)      */
  12. /************************************************************/
  13. /* History:                                                 */
  14. /*                                                          */
  15. /* date     who what                                        */
  16. /* 06/21/93 sam added multiline credit handling & code to   */
  17. /*              protect against infile same as outfile.     */
  18. /* 06/22/93 sam added command line options, help, etc       */
  19. /************************************************************/
  20. /* Copyright 1993 by Scott McGee. All rights reserved.      */
  21. /*                                                          */
  22. /* This program is my own work and is protected by          */
  23. /* copyright laws. It is provided free of charge on the     */
  24. /* condition that it not be altered or sold for profit      */
  25. /* without my prior express written permission. If you make */
  26. /* changes you feel improve the program, please send them   */
  27. /* to me. If I agree, I will add your changes to my code    */
  28. /* (with comments giving you *credit, of course). I can be  */
  29. /* reached by email at smcgee@microware.com                 */
  30. /************************************************************/
  31.  
  32.  
  33.  
  34. /* NOTE TO USERS: if you are compiling this on anything other than an OS-9
  35.    system, or a sun3 or sun4, you may have to look through the following
  36.    definitions to see if you need to define any from the command line for
  37.    your compiler. For instance, if you are compiling on a system that has
  38.    no string.h header, you should define NO_STRING_H on the compiler command
  39.    line. Also, be sure to modify the default paths found below the defines!
  40. */
  41.  
  42. /* define some capabilities dealing with portability issues:
  43.  
  44. NO_STRING_H        system DOES NOT HAVE a string.h header (use strings.h)
  45. */
  46.  
  47. #ifndef _UCC        /* no Ultra C compiler */
  48. #    ifdef OSK        /* old cc for osk */
  49. #        define NO_STRING_H
  50. #    endif
  51. #    ifdef _OS9000    /* old cc for os-9000 */
  52. #        define NO_STRING_H
  53. #    endif
  54. #endif
  55.  
  56. /*
  57. OSK_HELP        system CAN use -? for help (otherwise, use -h)
  58. */
  59.  
  60. #ifdef OSK            /* OS-9 680x0 system */
  61. #    define OSK_HELP
  62. #endif
  63.  
  64. #ifdef _OS9000        /* OS-9000 system */
  65. #    define OSK_HELP
  66. #endif
  67.  
  68. #ifdef OS9            /* OS-9 6809 system */
  69. #    define OSK_HELP
  70. #endif
  71.  
  72.  
  73. #include <stdio.h>
  74.  
  75. #ifndef NO_STRING_H
  76. #    include <string.h>            /* good, we can use string.h */
  77. #else
  78. #    include <strings.h>            /* use strings.h and redefine rindex */
  79. #    define strrchr(a,b) rindex(a,b)
  80. #endif
  81.  
  82.  
  83. #define TRUE 1
  84. #define FALSE 0
  85. #define DEFAULT -1
  86.  
  87.  
  88. /* define a global, prog_name, to hold the actual program name */
  89. char *prog_name;
  90.  
  91.  
  92.  
  93. void do_help(){
  94.     fprintf(stderr, "\n%s: a program to renumber quote files for StFortune\n\n", prog_name);
  95.     fprintf(stderr, "Usage:\n");
  96.     fprintf(stderr, "  %s (opts) infile outfile\n", prog_name);
  97.     fprintf(stderr, "  %s (opts) <infile >outfile\n", prog_name);
  98.     fprintf(stderr, "  %s (opts) infile >outfile\n", prog_name);
  99.     fprintf(stderr, "     infile and outfile refer to the input file name and\n");
  100.     fprintf(stderr, "     the output file name respetively. If the input file\n");
  101.     fprintf(stderr, "     is specified, it must come first. If it is not specified,\n");
  102.     fprintf(stderr, "     stdin is assumed. Similarly, if no output file is given,\n");
  103.     fprintf(stderr, "     stdout is assumed. The input file and the output file\n");
  104.     fprintf(stderr, "     may NOT be the same file!\n\n");
  105.     fprintf(stderr, "Options      -q      : quiet mode\n");
  106.     fprintf(stderr, "             -v      : verbose mode\n");
  107. #ifdef OSK_HELP
  108.     fprintf(stderr, "             -?      : print this help message\n");
  109. #else
  110.     fprintf(stderr, "             -h      : print this help message\n");
  111. #endif
  112.     fprintf(stderr, "\n\t(Copyright 1993 by Scott McGee)\n\n");
  113.     exit(0);
  114. }
  115.  
  116.  
  117. main(argc, argv)
  118.   int argc;
  119.   char **argv;
  120. {
  121.     FILE *inFile;
  122.     FILE *outFile;
  123.     char *inName = NULL;
  124.     char *outName = NULL;
  125.     char buff[128];
  126.     char buff2[128];
  127.     char progbuff[128];
  128.     char *ptr;
  129.     char *status;
  130.     int  verbose = DEFAULT;
  131.     int  count;
  132.     int  i;
  133.  
  134. /* set up prog_name */
  135.     strcpy(progbuff, argv[0]);
  136.     ptr = strrchr(progbuff, '/');
  137.     if(ptr != NULL)
  138.         prog_name = ptr+1;
  139.     else
  140.         prog_name = progbuff;
  141.  
  142. /* parse arguements */
  143.     for(i=1;i<argc;i++){
  144.         if((argv[i][0] == '-') || (argv[i][0] == '/')){
  145.             switch (argv[i][1]){
  146.                 case '?':
  147.                 case 'h':
  148.                     do_help();
  149.                 case 'q':
  150.                     verbose = FALSE;
  151.                     break;
  152.                 case 'v':
  153.                     verbose = TRUE;
  154.                     break;
  155.                 default :
  156.                     fprintf(stderr, "%s: unknown option \"%s\"\n", prog_name, argv[i]);
  157.                     do_help();
  158.             }
  159.         }else if(inName == NULL){
  160.             inName = argv[i];
  161.         }else if(outName == NULL){
  162.             outName = argv[i];
  163.             if(strcmp(inName, outName) == 0){
  164.                 fprintf(stderr, "%s: ERROR! Input file same as output file\n", prog_name);
  165.                 fprintf(stderr, "%s: Cannot write to input file!\n", prog_name);
  166.                 exit(0);
  167.             }
  168.         }else{
  169.             fprintf(stderr, "%s: Too many arguements!\n", prog_name);
  170.             exit(0);
  171.         }
  172.     }
  173.  
  174.     if(inName != NULL){
  175.         inFile = fopen(inName, "r");
  176.         if(inFile == NULL){
  177.             fprintf(stderr, "%s: Couldn't open input file %s!\n", prog_name, inName);
  178.             exit(0);
  179.         }
  180.     }else
  181.         inFile = stdin;
  182.  
  183.     if(outName != NULL){
  184.         outFile = fopen(outName, "w");
  185.         if(outFile == NULL){
  186.             fprintf(stderr, "%s: Couldn't open output file %s!\n", prog_name, outName);
  187.             exit(0);
  188.         }
  189.         if(verbose == DEFAULT)
  190.             verbose = TRUE;
  191.     }else
  192.         outFile = stdout;
  193.  
  194.     status = fgets(buff, 128, inFile);
  195.     if((verbose == TRUE) && (inName != NULL))
  196.         fprintf(stderr, "\nFile %s says %d entries.\n", inName, atoi(buff));
  197.     fputs(buff, outFile);
  198.  
  199.     count = 0;
  200.  
  201.     while(status != NULL){
  202.         status = fgets(buff, 128, inFile);
  203.         if(status != NULL){
  204.             sprintf(buff, "%5d", count);
  205.             buff[5] = '%';
  206.             fputs(buff, outFile);
  207.             if(strncmp(buff+6, "    --", 6) == 0)
  208.                 count++;
  209.         }
  210.     }
  211.  
  212.     count--;
  213.     if(verbose == TRUE)
  214.         fprintf(stderr, "%d actual entries.\n", count);
  215.  
  216.     if(argc > 2){
  217.         fclose(outFile);
  218.         outFile = fopen(outName, "w");
  219.         fseek(inFile, 0, 0);
  220.         status = fgets(buff, 128, inFile);
  221.         sprintf(buff2, "%d", count);
  222.         count = 0;
  223.         ptr = buff;
  224.         while(isdigit(*ptr))
  225.             ptr++;
  226.         strcat(buff2, ptr);
  227.         fputs(buff2, outFile);
  228.         while(status != NULL){
  229.             status = fgets(buff, 128, inFile);
  230.             if(status != NULL){
  231.                 if(strncmp(buff+5, "%    --", 7) == 0){
  232.                     sprintf(buff, "%5d", count);
  233.                     buff[5] = '%';
  234.                     fputs(buff, outFile);
  235.                     count++;
  236.                 }else if(strncmp(buff+5, "\\    --", 7) == 0){ /* contunuation */
  237.                     sprintf(buff, "%5d", count);
  238.                     buff[5] = '\\';
  239.                     fputs(buff, outFile);
  240.                     status = fgets(buff, 128, inFile);
  241.                     if(status != NULL){
  242.                         sprintf(buff, "%5d", count);
  243.                         buff[5] = '%';
  244.                         fputs(buff, outFile);
  245.                     }
  246.                     count++;
  247.                 }else{
  248.                     sprintf(buff, "%5d", count);
  249.                     buff[5] = '%';
  250.                     fputs(buff, outFile);
  251.                 }
  252.             }
  253.         }
  254.     }
  255. }
  256.