home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_05 / 9n05055a < prev    next >
Text File  |  1991-01-16  |  5KB  |  155 lines

  1.  
  2. /***********************************************
  3. * CreatRec.C
  4. *
  5. *  A method of handling the complex memory
  6. *  allocation required to create an array of
  7. *  character arrays in memory
  8. *
  9. *  The function declaration required is:
  10. *    char **CreateRecord(int nfields,
  11. *               int flen[], int InitString);
  12. *  where: nfields is the number of fields or
  13. *      strings to be created flen[] is an
  14. *      array of nfields-many ints, where each
  15. *      array element is the desired length
  16. *      of the corresponding field; InitString
  17. *      is the value to use when initializing the
  18. *      string space allocated by the function.
  19. *
  20. *  Returns: (char **).
  21. *  The programmer should use this pointer to
  22. *  access the strings created, and to free the
  23. *  allocated memory when finished.
  24. ***********************************************/
  25.  
  26. #include <stdio.h>
  27. #include <conio.h>
  28. #include <alloc.h>
  29. #include <mem.h>
  30. #include <stdlib.h>
  31.  
  32. char **CreateRecord(int nfields, int flen[],
  33.                    int InitString)
  34. {
  35.    char **field; /* this value is returned */
  36.    char *p;
  37.    int i=0, recsize=0, memsize;
  38.  
  39.    while(i<nfields){
  40.         recsize += flen[i++]; /* add up field sizes */
  41.         }
  42.    recsize += nfields; /* add space for a terminator for each field */
  43.  
  44.   /* total to allocate includes space for strings
  45.      and pointers to them: */
  46.   memsize = nfields*sizeof(char *) +
  47.                 recsize*sizeof(char);
  48.   field = (char **)malloc(memsize);
  49.   if(!field){
  50.        #ifdef ERR_INTERNAL
  51.             puts("Malloc failure in CreateRecord()");
  52.             exit(1); /* abort on failure */
  53.        #else
  54.             return NULL; /* return warning to caller */
  55.        #endif
  56.        }
  57.   /* initialize strings */       
  58.   memset(field,InitString,memsize);
  59.  
  60.   /* set p to point to the first byte
  61.      beyond the last pointer needed: */
  62.   p = (char *)&field[nfields];
  63.   for(i=0; i<nfields; i++){
  64.        field[i] = p; /* initialize the
  65.                         pointer array */
  66.        p = &p[ flen[i] ]; /* get address of
  67.                           last char of the field */
  68.        *p = '\0'; /* terminate the string */
  69.        p++; /* next string start address */
  70.        }
  71.   return field;
  72. } /* CreateRecord() */
  73.  
  74.  
  75. /******************************************************
  76.      A Example of a Routine that Uses CreateRecord()
  77.  ******************************************************/
  78.  
  79. #define ArraySize(x) ( sizeof(x)/sizeof(x[1]) )
  80. /* by ArtS: October, 1990. a general purpose macro to provide
  81. a count of the number of elements in any given array.
  82. NOTE that this works for
  83.         char c[] = "Art was here"; ...c is an array
  84. but NOT for
  85.         char *c =  "Art was here"; ...c is a pointer!
  86. */
  87.  
  88.  
  89. #define SPACE 32
  90.  
  91. /* This is the declaration you need to use CreateRecord(): */
  92. char **CreateRecord(int nfields, int flen[], int InitString);
  93.  
  94. /* A sample database fieldset: */
  95. char *label[] = { /* labels for example database record */
  96.      "    FName",
  97.      "    LName",
  98.      "  Address",
  99.      "     City",
  100.      "    State",
  101.      "      Zip",
  102.      "  Remarks"
  103.      };
  104. /* Field lengths for this sample database: */
  105. int array[] = { 20, 20, 30, 25, 2, 10, 50 };
  106.  
  107.  
  108. void main(void){
  109.      char **set;
  110.      int fieldcount;
  111.      int i;
  112.  
  113.      puts("\nSample calls to CreateRecord() for allocating string arrays\n");
  114.  
  115.      puts("Initialize a database record to blanks:");
  116.      fieldcount = ArraySize(array);
  117.      set = CreateRecord(fieldcount, array, SPACE);
  118.      if(!set){
  119.           printf("Error allocating memory for record\n");
  120.           exit(1);
  121.           }
  122.      for(i=0; i<fieldcount; i++){
  123.           printf(" %s: [%s]\n", label[i], set[i] );
  124.           }
  125.      puts("\nPress Key..."); getch();
  126.  
  127.      puts("Initialize a database record to underline characters:");
  128.      fieldcount = ArraySize(array);
  129.      set = CreateRecord(fieldcount, array, '_');
  130.      if(!set){
  131.           printf("Error allocating memory for record\n");
  132.           exit(1);
  133.           }
  134.      for(i=0; i<fieldcount; i++){
  135.           printf(" %s: [%s]\n", label[i], set[i] );
  136.           }
  137.      puts("\nPress Key..."); getch();
  138.  
  139.      free(set);
  140.  
  141.      puts("Initialize a set if five strings of length 12 to 'A':");
  142.      for(i=0; i<5; i++){
  143.           array[i] = 12; /* set all field lengths to 12 */
  144.           }
  145.      set = CreateRecord(5, array, 'A' );
  146.      if(!set){
  147.           printf("Error allocating memory for record\n");
  148.           exit(1);
  149.           }
  150.      for(i=0; i<5; i++){
  151.           printf(" item %d: [%s]\n", i, set[i] );
  152.           }
  153.      }/* main */
  154.  
  155.