home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / FILECAT.ZIP / Filecat.C next >
Encoding:
C/C++ Source or Header  |  1998-01-22  |  5.5 KB  |  235 lines

  1. /* +++Date last modified: 17-Nov-1996 */
  2.  
  3.  
  4.  
  5. /*
  6.  
  7. ** FILECAT.C - Adds one file onto another vertically, as with a column
  8.  
  9. ** block in QEdit.
  10.  
  11. **
  12.  
  13. ** Must be compiled in Compact or Large memory models to use larger
  14.  
  15. ** files.
  16.  
  17. **
  18.  
  19. ** Public Domain by Chad Wallace, 1996
  20.  
  21. */
  22.  
  23.  
  24.  
  25. #include <stdio.h>
  26.  
  27. #include <alloc.h>
  28.  
  29.  
  30.  
  31. #ifndef FALSE
  32.  
  33. #define FALSE 0
  34.  
  35. #define TRUE 1
  36.  
  37. #endif
  38.  
  39.  
  40.  
  41. /*
  42.  
  43. ** filecat() error codes
  44.  
  45. */
  46.  
  47.  
  48.  
  49. enum {
  50.  
  51.       FC_SUCCESS = 0,         /* Success */
  52.  
  53.       FC_RDEST,               /* Error reading dest file */
  54.  
  55.       FC_WDEST,               /* Error writing dest file */
  56.  
  57.       FC_SRC,                 /* Error with src file */
  58.  
  59.       FC_MEM,                 /* Out of memory */
  60.  
  61.       FC_LINES,               /* Too many lines (increase max_lines) */
  62.  
  63.       FC_LINE                 /* Line too long (increase line_max) */
  64.  
  65. };
  66.  
  67.  
  68.  
  69. /*
  70.  
  71. ** These are global to save overhead when calling cleanup function
  72.  
  73. */
  74.  
  75.  
  76.  
  77. static char ** str_arr;
  78.  
  79. static char * temp_str;
  80.  
  81.  
  82.  
  83. /*
  84.  
  85. ** Frees all allocated memory and closes specified file
  86.  
  87. */
  88.  
  89.  
  90.  
  91. static void cleanup(int tot_lines, FILE * fp)
  92.  
  93. {
  94.  
  95.       if (str_arr)
  96.  
  97.       {
  98.  
  99.             for ( ; tot_lines >= 0; tot_lines--)
  100.  
  101.                   free(str_arr[tot_lines]);
  102.  
  103.             free(str_arr);
  104.  
  105.       }
  106.  
  107.       if (temp_str)
  108.  
  109.             free(temp_str);
  110.  
  111.       if (fp)
  112.  
  113.             fclose(fp);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. int filecat(char * dest_file, char * src_file, int line_max, int max_lines)
  120.  
  121. {
  122.  
  123.       int i, tot_lines;
  124.  
  125.       unsigned int lines_len;
  126.  
  127.       FILE * fp;
  128.  
  129.  
  130.  
  131.       if ((temp_str = malloc(line_max + 1)) == NULL)
  132.  
  133.             return FC_MEM;
  134.  
  135.  
  136.  
  137.       /* Allocate memory for pointers to line strings */
  138.  
  139.  
  140.  
  141.       if ((str_arr = calloc(max_lines + 1, sizeof(char *))) == NULL)
  142.  
  143.       {
  144.  
  145.             free(temp_str);
  146.  
  147.             return FC_MEM;
  148.  
  149.       }
  150.  
  151.  
  152.  
  153.       /* Open destination file */
  154.  
  155.  
  156.  
  157.       if ((fp = fopen(dest_file, "rt")) == NULL)
  158.  
  159.       {
  160.  
  161.             free(temp_str);
  162.  
  163.             free(str_arr);
  164.  
  165.             return FC_RDEST;
  166.  
  167.       }
  168.  
  169.  
  170.  
  171.       /* Read destination file into string array line-by-line */
  172.  
  173.  
  174.  
  175.       for (i = 0;
  176.  
  177.            (fgets(temp_str, line_max + 1, fp) != NULL) && (i <= max_lines);
  178.  
  179.            i++)
  180.  
  181.       {
  182.  
  183.             /* Strip trailing newline from line read */
  184.  
  185.  
  186.  
  187.             if (temp_str[strlen(temp_str) - 1] == '\n')
  188.  
  189.                   temp_str[strlen(temp_str) - 1] = '\0';
  190.  
  191.  
  192.  
  193.             /* Allocate memory */
  194.  
  195.  
  196.  
  197.             if ((str_arr[i] = malloc(line_max + 1)) == NULL)
  198.  
  199.             {
  200.  
  201.                   /* Clean up and return memory error */
  202.  
  203.  
  204.  
  205.                   cleanup(i, fp);
  206.  
  207.  
  208.  
  209.                   return FC_MEM;
  210.  
  211.             }
  212.  
  213.  
  214.  
  215.             /* Copy the string to its new home */
  216.  
  217.  
  218.  
  219.             strcpy(str_arr[i], temp_str);
  220.  
  221.       }
  222.  
  223.  
  224.  
  225.       fclose(fp);
  226.  
  227.  
  228.  
  229.       if (i > max_lines)
  230.  
  231.       {
  232.  
  233.             /* Clean up and return too many lines error */
  234.  
  235.  
  236.  
  237.             cleanup(i, NULL);
  238.  
  239.  
  240.  
  241.             return FC_LINES;
  242.  
  243.       }
  244.  
  245.  
  246.  
  247.       /* Get length of longest line */
  248.  
  249.  
  250.  
  251.       lines_len = max_line(str_arr);
  252.  
  253.  
  254.  
  255.       /* Open source file */
  256.  
  257.  
  258.  
  259.       if ((fp = fopen(src_file, "rt")) == NULL)
  260.  
  261.       {
  262.  
  263.             /* Clean up and return source file error */
  264.  
  265.  
  266.  
  267.             cleanup(i, NULL);
  268.  
  269.  
  270.  
  271.             return FC_SRC;
  272.  
  273.       }
  274.  
  275.  
  276.  
  277.       tot_lines = i;
  278.  
  279.  
  280.  
  281.       /*
  282.  
  283.       ** Get each line from src file and append to corresponding line from
  284.  
  285.       ** dest file
  286.  
  287.       */
  288.  
  289.  
  290.  
  291.       for (i = 0;
  292.  
  293.            (fgets(temp_str, line_max + 1, fp) != NULL) && (i < max_lines);
  294.  
  295.            i++)
  296.  
  297.       {
  298.  
  299.             int j;
  300.  
  301.  
  302.  
  303.             /* Has this line been allocated yet? */
  304.  
  305.  
  306.  
  307.             if (str_arr[i] == NULL)
  308.  
  309.             {
  310.  
  311.                   /* Allocate memory */
  312.  
  313.  
  314.  
  315.                   if ((str_arr[i] = malloc(line_max + 1)) == NULL)
  316.  
  317.                   {
  318.  
  319.                         /* Clean up and return memory error */
  320.  
  321.  
  322.  
  323.                         cleanup(tot_lines, fp);
  324.  
  325.  
  326.  
  327.                         return FC_MEM;
  328.  
  329.                   }
  330.  
  331.  
  332.  
  333.                   /* Initialize string */
  334.  
  335.  
  336.  
  337.                   str_arr[i][0] = '\0';
  338.  
  339.  
  340.  
  341.                   tot_lines++;
  342.  
  343.             }
  344.  
  345.  
  346.  
  347.             /* Pad with spaces to make this line as long as longest */
  348.  
  349.  
  350.  
  351.             for (j = strlen(str_arr[i]); j < lines_len; j++)
  352.  
  353.                   str_arr[i][j] = ' ';
  354.  
  355.             str_arr[i][j] = '\0';
  356.  
  357.  
  358.  
  359.             /* Check size of resulting line when strcat'd */
  360.  
  361.  
  362.  
  363.             if (lines_len + strlen(temp_str) > line_max)
  364.  
  365.             {
  366.  
  367.                   cleanup(tot_lines, fp);
  368.  
  369.  
  370.  
  371.                   return FC_LINE;
  372.  
  373.             }
  374.  
  375.  
  376.  
  377.             strcat(str_arr[i], temp_str);
  378.  
  379.       }
  380.  
  381.  
  382.  
  383.       fclose(fp);
  384.  
  385.  
  386.  
  387.       if (i > max_lines)
  388.  
  389.       {
  390.  
  391.             /* Clean up and return too many lines error */
  392.  
  393.  
  394.  
  395.             cleanup(i, NULL);
  396.  
  397.  
  398.  
  399.             return FC_LINES;
  400.  
  401.       }
  402.  
  403.  
  404.  
  405.       /* Open dest file again, to write this time */
  406.  
  407.  
  408.  
  409.       if ((fp = fopen(dest_file, "wt")) == NULL)
  410.  
  411.       {
  412.  
  413.             /* Clean up and return source file error */
  414.  
  415.  
  416.  
  417.             cleanup(i, NULL);
  418.  
  419.  
  420.  
  421.             return FC_WDEST;
  422.  
  423.       }
  424.  
  425.  
  426.  
  427.       for (i = 0; i < tot_lines; i++)
  428.  
  429.       {
  430.  
  431.             if ((fputs(str_arr[i], fp) == EOF) /*|| (fputc('\n', fp) == EOF)*/)
  432.  
  433.             {
  434.  
  435.                   cleanup(i, fp);
  436.  
  437.  
  438.  
  439.                   return FC_WDEST;
  440.  
  441.             }
  442.  
  443.       }
  444.  
  445.  
  446.  
  447.       cleanup(i, fp);
  448.  
  449.  
  450.  
  451.       return FC_SUCCESS;
  452.  
  453. }
  454.  
  455.  
  456.  
  457. int main(int argc, char ** argv)
  458.  
  459. {
  460.  
  461.       printf("Filecat returned %d\n", filecat(argv[1], argv[2], 2048, 4096));
  462.  
  463.  
  464.  
  465.       return 0;
  466.  
  467. }
  468.  
  469.