Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <netdb.h>
  10. #include <assert.h>
  11.  
  12. //TOOLS
  13. void remove_line_return(char * new_line, char * line) {
  14. int i = 0;
  15. int n = strlen(line);
  16. while ( (i < n) && (line[i] != '\n') ) {
  17. new_line[i] = line[i];
  18. i++;
  19. }
  20. new_line[i] = '\0';
  21. }
  22.  
  23. void what_is_left_and_right(char* star_url, char* left, char* right){
  24. char c = star_url[0];
  25. int i=0;
  26. int star_indice;
  27. while (c!='*'){
  28. left[i] = c;
  29. i++;
  30. c = star_url[i];
  31. }
  32. star_indice = i;
  33. while (c!='\0'){
  34. i++;
  35. c = star_url[i];
  36. right[i-star_indice-1] = c;
  37. }
  38. }
  39.  
  40. //ADD FUNCTION
  41. void add_to_file(char* newElement, char* fileName){
  42. FILE* fichier = NULL;
  43. fichier = fopen(fileName,"a");
  44. fprintf(fichier,"%s\n",newElement);
  45. fclose(fichier);
  46. }
  47.  
  48. //CREATION
  49. void create_files(char* blacklist){
  50. FILE* fichier = NULL;
  51. fichier = fopen(blacklist, "r");
  52. if (fichier != NULL) {
  53. size_t len = 0;
  54. char * line = NULL;
  55. int read = getline(&line, &len, fichier);
  56.  
  57. while (read != -1){
  58. remove_line_return(line, line);
  59. char * found_star = strstr(line, "*");
  60. char * found_http = strstr(line, "@@||");
  61. char * found_pipes = strstr(line, "||");
  62. char * found_hashtags = strstr(line, "#");
  63. char * found_arobases = strstr(line, "@@");
  64. if (found_star &&
  65. !found_http &&
  66. !found_pipes &&
  67. !found_hashtags &&
  68. !found_arobases) {
  69. add_to_file(line,"starFile.txt");
  70. }
  71. read = getline(&line, &len, fichier);
  72. }
  73. free(line);
  74. fclose(fichier);
  75. }
  76. else {
  77. printf("Impossible to open %s file\n", blacklist);
  78. }
  79. }
  80.  
  81. //PARSING
  82. char* search_in_star_file(char* url){
  83. FILE* fichier = NULL;
  84. fichier = fopen("starFile.txt", "r");
  85. if (fichier != NULL) {
  86. char* left = malloc(strlen(url) * sizeof(char));
  87. char* right = malloc(strlen(url) * sizeof(char));
  88. what_is_left_and_right(url,left,right);
  89. printf("left : %s\n", left);
  90. printf("right : %s\n", right);
  91. size_t len = 0;
  92. char * line = NULL;
  93. int read = getline(&line, &len, fichier);
  94.  
  95. while (read != -1){
  96. remove_line_return(line, line);
  97. char* line_left = malloc(strlen(line) * sizeof(char));
  98. char* line_right = malloc(strlen(line) * sizeof(char));
  99. what_is_left_and_right(line,line_left,line_right);
  100. //printf("left line : %s\n", line_left);
  101. //printf("right line : %s\n", line_right);
  102. char* found_left = strstr(left,line_left);
  103. char* found_right = strstr(right,line_right);
  104. printf("left found: %s\n", found_left);
  105. printf("right found: %s\n", found_right);
  106. if (found_left && found_right) {
  107. free(line_left);
  108. free(line_right);
  109. free(left);
  110. free(right);
  111. fclose(fichier);
  112. return line;
  113. }
  114. free(line_left);
  115. free(line_right);
  116. read = getline(&line, &len, fichier);
  117. }
  118. free(line);
  119. free(left);
  120. free(right);
  121. fclose(fichier);
  122. return "none";
  123. }
  124. else {
  125. return "Impossible to open starFile.txt\n";
  126. }
  127. }
  128.  
  129. char* blacklist(char* url){
  130. return search_in_star_file(url);
  131. }
  132.  
  133. int main(int argc, char* argv[]){
  134. //create_files("../Library/easylist.txt");
  135. //blacklist("");
  136. char* star_url = "http://&prvtof=*&poru=x";
  137. printf("res: %s\n", blacklist(star_url));
  138. //char* res = strstr(star_url,"&poru=");
  139. //printf("str: %s\n", res);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement