home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / icf186.zip / CONVERT.C < prev    next >
Text File  |  1988-06-15  |  4KB  |  179 lines

  1. /*
  2. Convert database ASCII file records to ICF PRN format.
  3. Copyright by M.Arnow 1988.
  4. All rights reserved.
  5. */
  6. #include <stdio.h>
  7. #include <dir.h>
  8. #include <string.h>
  9.  
  10. void main()
  11. {
  12. register int i,j,k;
  13. char in_name[80],out_name[80];
  14. char path[80],ut[80],drive[3],dir[66],file[9],ext[5];
  15. char answer[80],flag;
  16. unsigned char a,*combo,*buffer,*b[19],*ptr;
  17. FILE *in,*out,*util;
  18. int fields,wide[50],ln[3],w[3],fn,t_width;
  19.  
  20. printf("\n\
  21. Program to convert ASCII output files of database programs into ICF PRN files.");
  22.  
  23. printf("\n\n\n\
  24. This program should be used if the ASCII file uses one line for each record.\n\n\
  25. Each field from the input file will become a line in the PRN file.  You have\n\
  26. the option of combining the first two or three fields into the first line of\n\
  27. the PRN file.\n");
  28.  
  29. printf("\nInput file name: ");
  30. scanf("%s",in_name);
  31. in = fopen(in_name,"r");
  32. if(!in)
  33.     {
  34.     printf("\nCan't open %s.\n",in_name);
  35.     exit(1);
  36.     }
  37. printf("Output file name: ");
  38. scanf("%s",out_name);
  39. fnsplit(out_name,drive,dir,file,ext);
  40. fnmerge(ut,drive,dir,file,".PRN");
  41. out = fopen(ut,"w");
  42. if(!out)
  43.     {
  44.     printf("\nCan't open %s\n",path);
  45.     exit(1);
  46.     }
  47.  
  48. printf("\nHow many fields per record of input file (Max. fields is 50)? ");
  49. scanf("%d",&fields);
  50. if(fields<=0 || fields>50)
  51.     {
  52.     printf("\nRange error.\n");
  53.     exit(1);
  54.     }
  55.  
  56. printf("\n");
  57. for(i=0;i<19;++i) wide[i] = 0;
  58. for(i=0,t_width=0;i<fields;++i)
  59.     {
  60.     printf("How many characters wide is a field #%d (Max. width is 100)? ",i+1);
  61.     scanf("%d",&wide[i]);
  62.     if(wide<=0 || wide[i]>100)
  63.         {
  64.         printf("\nRange error.\n");
  65.         exit(1);
  66.         }
  67.     t_width += wide[i];
  68.     }
  69.  
  70. do
  71.     {
  72.     flag = 0;
  73.     printf("\nDo you wish to combine fields 1,2, and optionally 3 into one line (Y/N)? ");
  74.     scanf("%s",answer);
  75.     if(answer[0]=='y' || answer[0]=='Y') flag = 1;
  76.     if(answer[0]=='n' || answer[0]=='N') flag = -1;
  77.     } while(!flag);
  78. if(flag==1)
  79.     {
  80.     printf("Enter the fields in the order you want to appear  in line 1.\n");
  81.     for(i=0;i<2;++i)
  82.         {
  83.         printf("    Entry %d is field: ",i+1);
  84.         scanf("%d",&ln[i]);
  85.         if(ln[i]>3)
  86.             {
  87.             printf("\nRange error.\n");
  88.             exit(1);
  89.             }
  90.         }
  91.     printf("    Entry 3 is field (quit=0): ");
  92.     scanf("%d",&ln[i]);
  93.     if(ln[i]>3)
  94.         {
  95.         printf("\nRange error.\n");
  96.         exit(1);
  97.         }
  98.     if(!ln[i]) fn = 2;
  99.     else fn = 3;
  100.  
  101.     for(i=0;i<fn;++i) --ln[i];
  102.     }
  103.  
  104. printf("\nWriting file %s\n\n",ut);
  105.  
  106. /* Allocate input buffer. */
  107. buffer = (unsigned char *) malloc(fields*t_width+2);
  108. if(!buffer)
  109.     {
  110.     printf("Not enough memory for file input buffer.\n");
  111.     exit(1);
  112.     }
  113. combo = (unsigned char *) malloc(fields*(wide[0]+wide[1]+wide[2])+2);
  114. if(!combo)
  115.     {
  116.     printf("Not enough memory for file input buffer.\n");
  117.     exit(1);
  118.     }
  119.  
  120. j = 0;
  121. for(;;)
  122.     {
  123.     ptr = fgets(buffer,t_width+2,in);
  124.     if(!ptr) break;
  125.     if(flag==1)
  126.         {
  127.         switch (ln[0])
  128.             {
  129.             case 0: i = 0; break;
  130.             case 1: i = wide[0]; break;
  131.             case 2: i = wide[0]+wide[1]; break;
  132.             }
  133.         strncpy(combo,&buffer[i],wide[ln[0]]);
  134.         combo[wide[ln[0]]] = 0;
  135.  
  136.         switch (ln[1])
  137.             {
  138.             case 0: i = 0; break;
  139.             case 1: i = wide[0]; break;
  140.             case 2: i = wide[0]+wide[1]; break;
  141.             }
  142.         strncat(combo,&buffer[i],wide[ln[1]]);
  143.         j = wide[ln[0]] + wide[ln[1]];
  144.         if(fn==3)
  145.             {
  146.             switch (ln[2])
  147.                 {
  148.                 case 0: i = 0; break;
  149.                 case 1: i = wide[0]; break;
  150.                 case 2: i = wide[0]+wide[1]; break;
  151.                 }
  152.             strncat(combo,&buffer[i],wide[ln[2]]);
  153.             j += wide[ln[2]];
  154.             }
  155.         fprintf(out,"%s\n",combo);
  156.         printf("%s\n",combo);
  157.         k = fn;
  158.         }
  159.     else
  160.         {
  161.         k = 0;
  162.         j = 0;
  163.         }
  164.     for(i=k;i<fields;++i)
  165.         {
  166.         strncpy(combo,&buffer[j],wide[i]);
  167.         combo[wide[i]] = 0;
  168.         fprintf(out,"%s\n",combo);
  169.         printf("%s\n",combo);
  170.         j += wide[i];
  171.         }
  172.     }
  173.  
  174. fclose(out);
  175. free(buffer);
  176. free(combo);
  177. }
  178.  
  179.