pa02.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Do not modify this file.
  3. */
  4. #include "pa02.h"
  5. #include <libgen.h>
  6. #define LINE_SIZE 1000
  7. void trim(char * s)
  8. {
  9. int pos = strlen(s) - 1;
  10. while (iscntrl(s[pos]))
  11. {
  12. s[pos--] = '\0';
  13. }
  14. }
  15. void printUsage(char * exec_path)
  16. {
  17. const char * exec = basename(exec_path);
  18. printf("\n"
  19. " Usage: %s <command> <inputfile>\n"
  20. "\n"
  21. " <command> should be one of:\n"
  22. "\n"
  23. " my_strlen\n"
  24. " my_countchar\n"
  25. " my_strupper\n"
  26. " my_strlower\n"
  27. " my_strcpy\n"
  28. " my_strncpy\n"
  29. " my_strcat\n"
  30. " my_strncat\n"
  31. " my_strstr\n"
  32. " my_strinsert\n"
  33. " my_strdelete\n"
  34. "\n",
  35. exec);
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. if (argc == 2 && (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0))
  40. {
  41. printUsage(argv[0]);
  42. return EXIT_SUCCESS;
  43. }
  44. if (argc != 3) {
  45. printUsage(argv[0]);
  46. return EXIT_FAILURE;
  47. }
  48. FILE * f;
  49. if ((f = fopen(argv[2], "r")) == NULL) {
  50. printf("Unable to open file '%s', aborting!\n", argv[2]);
  51. return EXIT_FAILURE;
  52. }
  53. FILE * fptrout = stdout;
  54. int line_count = 0;
  55. char line_buffer[LINE_SIZE];
  56. while (fgets(line_buffer, LINE_SIZE, f) != NULL)
  57. {
  58. line_count++;
  59. }
  60. int file_length = ftell(f);
  61. rewind(f);
  62. char * * src = malloc(sizeof(char *) * line_count);
  63. char * * copy = malloc(sizeof(char *) * line_count);
  64. int i;
  65. for (i = 0; i < line_count; i++)
  66. {
  67. if (feof(f))
  68. {
  69. printf("Not enough lines in file!\n");
  70. fclose(f);
  71. return EXIT_FAILURE;
  72. }
  73. copy[i] = malloc(sizeof(char) * LINE_SIZE);
  74. fgets(copy[i], LINE_SIZE, f);
  75. trim(copy[i]);
  76. src[i] = strdup(copy[i]);
  77. }
  78. fclose(f);
  79. char * buffer = malloc(sizeof(char) * file_length);
  80. buffer[0] = '\0';
  81. //Partitioning outputs
  82. const char * command = argv[1];
  83. if (strcmp(command, "my_strlen") == 0) {
  84. for (i = 0; i < line_count; i++) {
  85. fprintf(fptrout, "length: %d\n", my_strlen(copy[i]));
  86. }
  87. } else if (strcmp(command, "my_countchar") == 0) {
  88. for (i = 0; i < line_count; i++) {
  89. fprintf(fptrout, "count(%c): %d\n", copy[i][0], my_countchar(copy[i], copy[i][0]));
  90. }
  91. } else if (strcmp(command, "my_strupper") == 0) {
  92. for (i = 0; i < line_count; i++) {
  93. my_strupper(copy[i]);
  94. fprintf(fptrout, "uppercase: %s\n", copy[i]);
  95. }
  96. } else if (strcmp(command, "my_strlower") == 0) {
  97. for (i = 0; i < line_count; i++) {
  98. my_strlower(copy[i]);
  99. fprintf(fptrout, "lowercase: %s\n", copy[i]);
  100. }
  101. } else if (strcmp(command, "my_strcat") == 0) {
  102. for (i = 0; i < line_count; i++) {
  103. my_strcat(copy[i], " ");
  104. my_strcat(copy[i], src[i]);
  105. fprintf(fptrout, "%s\n", copy[i]);
  106. }
  107. } else if (strcmp(command, "my_strncat") == 0) {
  108. for (i = 0; i < line_count; i++) {
  109. my_strncat(copy[i], " ", 2);
  110. my_strncat(copy[i], "........", 1);
  111. fprintf(fptrout, "%s\n", copy[i]);
  112. }
  113. } else if (strcmp(command, "my_strcpy") == 0) {
  114. for (i = 0; i < line_count; i++) {
  115. my_strcpy(copy[i], "Copying this String.");
  116. fprintf(fptrout, "%s\n", copy[i]);
  117. }
  118. } else if (strcmp(command, "my_strncpy") == 0) {
  119. for (i = 0; i < line_count; i++) {
  120. my_strncpy(copy[i], src[i], 5);
  121. fprintf(fptrout, "%s\n", copy[i]);
  122. }
  123. } else if (strcmp(command, "my_strstr") == 0) {
  124. for (i = 0; i < line_count; i++) {
  125. fprintf(fptrout, "%d\n", my_strstr(copy[i], src[i]) != NULL);
  126. }
  127. } else if (strcmp(command, "my_strinsert") == 0) {
  128. for (i = 0; i < line_count; i++) {
  129. my_strinsert(buffer, "\n", 0);
  130. my_strinsert(buffer, src[i], 0);
  131. }
  132. fprintf(fptrout, "[%s]\n", buffer);
  133. } else if (strcmp(command, "my_strdelete") == 0) {
  134. for (i = 0; i < line_count; i++) {
  135. my_strinsert(buffer, "\n", 0);
  136. my_strinsert(buffer, src[i], 0);
  137. my_strdelete(buffer, 0, 10);
  138. }
  139. fprintf(fptrout, "[%s]\n", buffer);
  140. }
  141. for (i = 0; i < line_count; i++) {
  142. free(copy[i]);
  143. free(src[i]);
  144. }
  145. free(src);
  146. free(copy);
  147. free(buffer);
  148. fclose(fptrout);
  149. return EXIT_SUCCESS;
  150. }