home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / function / 1482 < prev    next >
Encoding:
Text File  |  1992-12-23  |  2.5 KB  |  125 lines

  1. Path: sparky!uunet!news.larc.nasa.gov!lynx.larc.nasa.gov!goodrich
  2. From: goodrich@lynx.larc.nasa.gov (Mike Goodrich)
  3. Newsgroups: comp.lang.functional
  4. Subject: Completely functional program in C
  5. Date: 23 Dec 1992 21:07:23 GMT
  6. Organization: NASA Langley Research Center, Hampton, VA  USA
  7. Lines: 113
  8. Distribution: world
  9. Message-ID: <1hakebINNdsa@rave.larc.nasa.gov>
  10. NNTP-Posting-Host: lynx.larc.nasa.gov
  11.  
  12.  
  13. Greetings,
  14.     The following is an example of a functional program in C I wrote after
  15. reading MaClannan's book on FP. I would like to invite comments from the FP
  16. community. I was pleased to find that the Sun C optimizer knows about tail recursion optimization so that this program runs as fast as the conventional
  17. C version that it is based on.
  18.  
  19.  
  20.  
  21.  
  22.  
  23. /*
  24.  * Hexdmp.c
  25.  * 
  26.  * Author: Mike Goodrich (804) 864-1790   goodrich@128.155.25.38  (goodrich@lynx.larc.nasa.gov)
  27.  * 
  28.  * Purpose to dump a given file to STDOUT in hex and ascii
  29.  *
  30.  * NOTE: This version was rewritten to adhere to the 'functional program' paradigm
  31.  *
  32.  * Usage:    hexdmp <filename>
  33.  * 
  34.  * Note:    If compiling under Microsoft C must use -J to overide signed char
  35.  * default.
  36.  */
  37.  
  38.  
  39.  
  40. #include    <stdio.h>
  41. #define REC_SIZE    16
  42. static unsigned char HexTab[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  43. 'a', 'b', 'c', 'd', 'e', 'f'};
  44.  
  45. void            PutHexLine(), PutTextLine(), DoLine(), DumpFile();
  46.  
  47.  
  48. main(argc, argv)
  49.     int             argc;
  50.     char          **argv;
  51. {
  52.     unsigned char   buff[REC_SIZE];
  53.  
  54.     if (argc < 2) {
  55.         printf("\nUsage: hexdmp <filename>\n");
  56.         exit(0);
  57.     }
  58.     DumpFile(fopen(argv[1], "rb"), buff);
  59. }
  60.  
  61. void
  62. PutTextLine(NumRead, buff)
  63.     short           NumRead;
  64.     unsigned char  *buff;
  65. {
  66.     switch (NumRead) {
  67.     case 0:
  68.         return;
  69.     default:
  70.         if (*buff < ' ' || *buff > '~') {    /* printable ? */
  71.             printf(".");
  72.         } else {
  73.             printf("%c", *buff);
  74.         }
  75.         PutTextLine(NumRead - 1, buff + 1);
  76.     }
  77. }
  78.  
  79. void
  80. PutHexLine(NumRead, buff)
  81.     short           NumRead;
  82.     unsigned char  *buff;
  83. {
  84.     switch (NumRead) {
  85.     case 0:
  86.         printf("\t");
  87.         return;
  88.     default:
  89.         printf("%02x ", *buff);
  90.         PutHexLine(NumRead - 1, buff + 1);
  91.     }
  92. }
  93.  
  94. void
  95. DoLine(NumRead, buff, count, fp)
  96.     short           NumRead, count;
  97.     unsigned char  *buff;
  98.     FILE           *fp;
  99. {
  100.     switch (NumRead) {
  101.     case 0:
  102.         return;
  103.     default:
  104.         printf("\n<%d>\t", count * REC_SIZE);
  105.         PutHexLine(NumRead, buff);
  106.         PutTextLine(NumRead, buff);
  107.         DoLine(fread(buff, 1, REC_SIZE, fp), buff, count + 1, fp);
  108.     }
  109. }
  110.  
  111. void 
  112. DumpFile(fp, buff)
  113.     FILE *fp;
  114.     unsigned char  *buff;
  115. {
  116.     if (fp == NULL) {
  117.         printf("Error: cannot open input file\n");
  118.         exit(-1);
  119.     }
  120.     DoLine(fread(buff, 1, REC_SIZE, fp), buff, 0, fp);
  121. }
  122.  
  123.  
  124.  
  125.