Posted By

fabio914 on 10/30/12


Tagged

space tab


Versions (?)

SpaceTab


 / Published in: C
 

Convert any file to an "empty" text file (with spaces and tabs).

  1. /*
  2.  * SpaceTab
  3.  *
  4.  * Created by Fabio de Albuquerque Dela Antonio
  5.  *
  6.  * fabio914 at gmail.com
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. unsigned char * toSpaceTab(unsigned char *, long int);
  15. unsigned char * toText(unsigned char *, long int);
  16.  
  17. int main(int argc, char *argv[]) {
  18.  
  19. FILE * in, * out;
  20.  
  21. int option = 0;
  22.  
  23. if(argc < 4) {
  24. fprintf(stderr, "Use: %s <input file> <output file> <option>\n", argv[0]);
  25. fprintf(stderr, "option: \"-1\" to convert from text to space-tab or \"-2\" to convert from space-tab to text\n");
  26. return 1;
  27. }
  28.  
  29. if(!strcmp(argv[3], "-1"))
  30. option = 1;
  31. else if(!strcmp(argv[3], "-2"))
  32. option = 2;
  33. else {
  34. fprintf(stderr, "Invalid option.\n");
  35. return 1;
  36. }
  37.  
  38. if((in = fopen(argv[1], "r")) == NULL) {
  39. fprintf(stderr, "Could not open input file.\n");
  40. return 1;
  41. }
  42.  
  43. fseek(in, 0, SEEK_END);
  44. long int fileSize = ftell(in);
  45. fseek(in, 0, SEEK_SET);
  46.  
  47. unsigned char * inb, * outb;
  48.  
  49. if((inb = (unsigned char *)malloc(fileSize*sizeof(unsigned char))) == NULL) {
  50. fprintf(stderr, "Could not allocate enough memory.\n");
  51. return 1;
  52. }
  53.  
  54. if(fread(inb, sizeof(unsigned char), fileSize, in) < fileSize) {
  55. fprintf(stderr, "Could not read input file.\n");
  56. return 1;
  57. }
  58.  
  59. outb = (option == 1) ? toSpaceTab(inb, fileSize):toText(inb, fileSize);
  60.  
  61. fclose(in);
  62.  
  63. if((out = fopen(argv[2], "w")) == NULL) {
  64. fprintf(stderr, "Could not open/create output file.\n");
  65. return 1;
  66. }
  67.  
  68. if(fwrite(outb, sizeof(unsigned char), (option == 1) ? fileSize*8:fileSize/8, out) < ((option == 1) ? fileSize*8:fileSize/8)) {
  69. fprintf(stderr, "Could not write on output file.\n");
  70. return 1;
  71. }
  72.  
  73. fclose(out);
  74. free(inb);
  75. free(outb);
  76.  
  77. return 0;
  78. }
  79.  
  80. unsigned char * toSpaceTab(unsigned char * text, long int fileSize) {
  81.  
  82. unsigned char * outb;
  83. unsigned char current;
  84.  
  85. if((outb = (unsigned char *)malloc(fileSize*sizeof(unsigned char)*8)) == NULL) {
  86. fprintf(stderr, "Could not allocate enough memory.\n");
  87. exit(1);
  88. }
  89.  
  90. register long int i, k = 0;
  91. register unsigned char j;
  92.  
  93. for(i = 0; i < fileSize; i++) {
  94. current = text[i];
  95.  
  96. for(j = 0; j < 8; j++) {
  97.  
  98. if((current & (1 << j)))
  99. outb[k] = ' ';
  100. else outb[k] = '\t';
  101.  
  102. k++;
  103. }
  104. }
  105.  
  106. return outb;
  107. }
  108.  
  109. unsigned char * toText(unsigned char * spacetab, long int fileSize) {
  110.  
  111. unsigned char * outb;
  112. unsigned char current;
  113.  
  114. if((outb = (unsigned char *)malloc(fileSize*sizeof(unsigned char)/8)) == NULL) {
  115. fprintf(stderr, "Could not allocate enough memory.\n");
  116. exit(1);
  117. }
  118.  
  119. register long int i, k = 0;
  120. register unsigned char j;
  121.  
  122. for(i = 0; i < fileSize/8; i++) {
  123. current = 0;
  124.  
  125. for(j = 0; j < 8; j++) {
  126.  
  127. if(spacetab[k] == ' ')
  128. current |= (1 << j);
  129.  
  130. k++;
  131. }
  132.  
  133. outb[i] = current;
  134. }
  135.  
  136. return outb;
  137. }

Report this snippet  

You need to login to post a comment.