SHARE
TWEET

strrot.c (I got carried away)

B1KMusic Sep 23rd, 2016 (edited) 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Refactor of: http://codereview.stackexchange.com/questions/142101/did-you-twist-my-words
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7.  
  8. bool streq(char *s1, char *s2);
  9. bool streq_circular(char *s1, char *s1_base, char *s2);
  10. bool strstr_circular(char *haystack, char *needle);
  11. char *tokenize(char *buf, char *delim);
  12. char *next_token(char *buf);
  13. char *jump_token(char *buf, int n);
  14. void parse_buffer(char *buf);
  15. void die(char *err);
  16. FILE *get_file_from_args(char **args);
  17. size_t get_file_length(FILE *file);
  18. char *copy_file(FILE *file);
  19.  
  20. bool
  21. streq(char *s1, char *s2)
  22. {
  23.     for(; *s1 && *s2; s1++, s2++)
  24.         if(*s1 != *s2)
  25.             return false;
  26.  
  27.     return true;
  28. }
  29.  
  30. bool
  31. streq_circular(char *s1, char *s1_base, char *s2)
  32. {
  33.     char *s2_offset = s2 + strlen(s1);
  34.  
  35.     return streq(s1, s2) && streq(s1_base, s2_offset);
  36. }
  37.  
  38. bool
  39. strstr_circular(char *haystack, char *needle)
  40. {
  41.     if(!needle || !haystack || strlen(needle) != strlen(haystack))
  42.         return false;
  43.  
  44.     for(char *hs_base = haystack; haystack && haystack[0]; haystack = strchr(haystack + 1, *needle))
  45.         if(streq_circular(haystack, hs_base, needle))
  46.             return true;
  47.  
  48.     return false;
  49. }
  50.  
  51. char *
  52. tokenize(char *buf, char *delim)
  53. {
  54.     char *base = buf;
  55.  
  56.     for(; *buf; buf++)
  57.         if(strchr(delim, *buf))
  58.             *buf = '\0';
  59.  
  60.     return base;
  61. }
  62.  
  63. char *
  64. next_token(char *buf)
  65. {
  66.     if(buf == NULL)
  67.         return NULL;
  68.  
  69.     while(*(buf++));
  70.  
  71.     return *buf ? buf : NULL;
  72. }
  73.  
  74. char *
  75. jump_token(char *buf, int n)
  76. {
  77.     while(n--)
  78.         buf = next_token(buf);
  79.  
  80.     return buf;
  81. }
  82.  
  83. void
  84. parse_buffer(char *buf)
  85. {
  86.     bool is_rotated;
  87.  
  88.     for(buf = tokenize(buf, ",\n\r"); buf != NULL; buf = jump_token(buf, 2)){
  89.         is_rotated = strstr_circular(buf, next_token(buf));
  90.         puts(is_rotated ? "True" : "False");
  91.     }
  92. }
  93.  
  94. void
  95. die(char *err)
  96. {
  97.     fprintf(stderr, "Error: %s\n", err);
  98.     exit(1);
  99. }
  100.  
  101. FILE *
  102. get_file_from_args(char **args)
  103. {
  104.     FILE *out;
  105.  
  106.     if(!args[1])
  107.         die("No file was provided");
  108.  
  109.     out = fopen(args[1], "r");
  110.  
  111.     if(out == NULL)
  112.         die("Could not open file");
  113.  
  114.     return out;
  115. }
  116.  
  117. size_t
  118. get_file_length(FILE *file)
  119. {
  120.     size_t length;
  121.  
  122.     fseek(file, 0, SEEK_END);
  123.     length = ftell(file);
  124.     fseek(file, 0, SEEK_SET);
  125.  
  126.     return length;
  127. }
  128.  
  129. char *
  130. copy_file(FILE *file)
  131. {
  132.     size_t file_length = get_file_length(file);
  133.     char *out = calloc(1, file_length + 2);
  134.  
  135.     if(out == NULL)
  136.         die("Could not allocate memory");
  137.  
  138.     fread(out, 1, file_length, file);
  139.  
  140.     return out;
  141. }
  142.  
  143. int
  144. main(int argc, char **args)
  145. {
  146.     FILE *file = get_file_from_args(args);
  147.     char *fbuf = copy_file(file);
  148.  
  149.     fclose(file);
  150.     parse_buffer(fbuf);
  151.     free(fbuf);
  152.  
  153.     return 0;
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top