From b15d3350d00dd8e9539460109370dbf07ce075b2 Mon Sep 17 00:00:00 2001 From: mehmetmertguduz Date: Sat, 8 Jun 2024 16:28:24 +0300 Subject: [PATCH] clean code for search.c --- docs/html/dc/dec/search_8c_source.html | 175 +++++++++++++------------ src/search/search.c | 109 +++++++-------- 2 files changed, 143 insertions(+), 141 deletions(-) diff --git a/docs/html/dc/dec/search_8c_source.html b/docs/html/dc/dec/search_8c_source.html index 2af7e28..568fad9 100644 --- a/docs/html/dc/dec/search_8c_source.html +++ b/docs/html/dc/dec/search_8c_source.html @@ -152,7 +152,7 @@
53 /* Read the posts.json file. */
54 FILE *posts_json_file = fopen("posts.json", "r");
55
-
56 if (posts_json_file == NULL)
+
56 if (posts_json_file == NULL)
57 {
58 no_json_error_msg();
59 goto END;
@@ -166,7 +166,7 @@
67 /* Parse the JSON. */
68 cJSON *json = cJSON_Parse(json_string);
69
-
70 if (json == NULL)
+
70 if (json == NULL)
71 {
72 json_parse_error_msg();
73 goto END;
@@ -175,7 +175,7 @@
76 /* Get the posts array. */
77 cJSON *posts = cJSON_GetObjectItem(json, "posts");
78
-
79 if (!cJSON_IsArray(posts))
+
79 if (!cJSON_IsArray(posts))
80 {
81 posts_not_array_error_msg();
82 goto END;
@@ -185,12 +185,12 @@
86 size_t post_i = 0;
87
88 /* Write the posts and tags to windows. */
-
89 for (size_t i = 0; i < cJSON_GetArraySize(posts); i++)
+
89 for (size_t i = 0; i < cJSON_GetArraySize(posts); i++)
90 {
91 /* Take the post with specified index. */
92 cJSON *post = cJSON_GetArrayItem(posts, i);
93
-
94 if (post == NULL)
+
94 if (post == NULL)
95 {
96 post_error_msg();
97 goto END;
@@ -199,7 +199,7 @@
100 /* Take the file object of the specified post. */
101 cJSON *file_object = cJSON_GetObjectItem(post, "file");
102
-
103 if (file_object == NULL)
+
103 if (file_object == NULL)
104 {
105 fprintf(stderr, "File object not found in post %d\n", i);
106 goto END;
@@ -208,94 +208,95 @@
109 /* Take the url of the of the specified post by parsing the data from file object. */
110 cJSON *url = cJSON_GetObjectItem(file_object, "url");
111
-
112 if (post_i == 0)
-
113 {
-
114 /* The tags object parsing. */
-
115 cJSON *tags_object = cJSON_GetObjectItem(post, "tags");
+
112 if (post_i == 0)
+
113 {
+
114 /* The tags object parsing. */
+
115 cJSON *tags_object = cJSON_GetObjectItem(post, "tags");
116
-
117 if (tags_object == NULL)
-
118 {
-
119 fprintf(stderr, "Tags object not found in post %d\n", i);
-
120 goto END;
-
121 }
+
117 if (tags_object == NULL)
+
118 {
+
119 fprintf(stderr, "Tags object not found in post %d\n", i);
+
120 goto END;
+
121 }
122
-
123 /* The general tags array parsing. */
-
124 cJSON *general_tags = cJSON_GetObjectItem(tags_object, "general");
-
125
-
126 wmove(post_tags_window, 1, 1);
+
123 /* The general tags array parsing. */
+
124 cJSON *general_tags = cJSON_GetObjectItem(tags_object, "general");
+
125
+
126 wmove(post_tags_window, 1, 1);
127
-
128 int line_counter = 0;
-
129 int max_width = getmaxx(post_tags_window) - 2; // Get the window width, minus 2 for borders
-
130 int max_height = 6; // Fixed height for the window
-
131 int line_to_write = 1;
+
128 int line_counter = 0;
+
129 int max_width = getmaxx(post_tags_window) - 2; // Get the window width, minus 2 for borders
+
130 int max_height = 6; // Fixed height for the window
+
131 int line_to_write = 1;
132
-
133 if (cJSON_IsArray(general_tags))
-
134 {
-
135 for (size_t j = 0; j < cJSON_GetArraySize(general_tags); j++)
-
136 {
-
137 cJSON *tag = cJSON_GetArrayItem(general_tags, j);
+
133 if (cJSON_IsArray(general_tags))
+
134 {
+
135 for (size_t j = 0; j < cJSON_GetArraySize(general_tags); j++)
+
136 {
+
137 cJSON *tag = cJSON_GetArrayItem(general_tags, j);
138
-
139 for (size_t k = 0; k < strlen(tag->valuestring); k++)
-
140 {
-
141 if (line_counter >= max_width)
-
142 {
-
143 line_to_write++;
-
144 if (line_to_write >= max_height) // Check for vertical overflow
-
145 {
-
146 break;
-
147 }
-
148 wmove(post_tags_window, line_to_write, 1);
-
149 line_counter = 0;
-
150 }
+
139 for (size_t k = 0; k < strlen(tag->valuestring); k++)
+
140 {
+
141 if (line_counter >= max_width)
+
142 {
+
143 line_to_write++;
+
144 if (line_to_write >= max_height) // Check for vertical overflow
+
145 {
+
146 break;
+
147 }
+
148 wmove(post_tags_window, line_to_write, 1);
+
149 line_counter = 0;
+
150 }
151
-
152 wprintw(post_tags_window, "%c", tag->valuestring[k]);
-
153 wrefresh(post_tags_window);
-
154 line_counter++;
-
155 }
+
152 wprintw(post_tags_window, "%c", tag->valuestring[k]);
+
153 wrefresh(post_tags_window);
+
154 line_counter++;
+
155 }
156
-
157 if (line_counter + 2 >= max_width) // Check if ", " will overflow
-
158 {
-
159 line_to_write++;
-
160 if (line_to_write >= max_height) // Check for vertical overflow
-
161 {
-
162 break;
-
163 }
-
164 wmove(post_tags_window, line_to_write, 1);
-
165 line_counter = 0;
-
166 }
-
167 else
-
168 {
-
169 wprintw(post_tags_window, ", ");
-
170 line_counter += 2;
-
171 }
-
172
-
173 if (line_to_write >= max_height) // Check for vertical overflow
-
174 {
-
175 break;
-
176 }
-
177 }
-
178 }
-
179 }
-
180
-
181
-
182 if (url != NULL && cJSON_IsString(url) && post_i != posts_panel_height - 2)
-
183 {
-
184 write_post(posts_window, post_i, url);
-
185 wrefresh(posts_window);
-
186
-
187 /* Increase post_i. */
-
188 post_i++;
-
189 }
-
190 }
-
191
-
192 wrefresh(posts_window);
-
193 refresh();
-
194
-
195 END:
-
196 cJSON_Delete(json);
-
197 getch();
-
198 endwin();
-
199}
+
157 if (line_counter + 2 >= max_width) // Check if ", " will overflow
+
158 {
+
159 line_to_write++;
+
160
+
161 if (line_to_write >= max_height) // Check for vertical overflow
+
162 {
+
163 break;
+
164 }
+
165
+
166 wmove(post_tags_window, line_to_write, 1);
+
167 line_counter = 0;
+
168 }
+
169 else
+
170 {
+
171 wprintw(post_tags_window, ", ");
+
172 line_counter += 2;
+
173 }
+
174
+
175 if (line_to_write >= max_height) // Check for vertical overflow
+
176 {
+
177 break;
+
178 }
+
179 }
+
180 }
+
181 }
+
182
+
183 if (url != NULL && cJSON_IsString(url) && post_i != posts_panel_height - 2)
+
184 {
+
185 write_post(posts_window, post_i, url);
+
186 wrefresh(posts_window);
+
187
+
188 /* Increase post_i. */
+
189 post_i++;
+
190 }
+
191 }
+
192
+
193 wrefresh(posts_window);
+
194 refresh();
+
195
+
196 END:
+
197 cJSON_Delete(json);
+
198 getch();
+
199 endwin();
+
200}
void aria2_download(char *tags, int page, int lim)
downloads the API response by using aria2c, works with a system call
void create_controls_window(WINDOW *window, int *posts_panel_height, control *controls, int total_controls)
Creates a new window to display the controls and adds it to the specified position.
void create_post_tags_window(WINDOW **window, int *posts_panel_height)
Creates a new window to display post tags and adds it to the specified position.
diff --git a/src/search/search.c b/src/search/search.c index 2dc4c71..39e46ed 100644 --- a/src/search/search.c +++ b/src/search/search.c @@ -6,7 +6,7 @@ * @author Mehmet Mert Gunduz (merttgg@gmail.com) * * @date 31/10/2023 -*/ + */ #include @@ -53,7 +53,7 @@ void search(char *tags) /* Read the posts.json file. */ FILE *posts_json_file = fopen("posts.json", "r"); - if (posts_json_file == NULL) + if (posts_json_file == NULL) { no_json_error_msg(); goto END; @@ -67,7 +67,7 @@ void search(char *tags) /* Parse the JSON. */ cJSON *json = cJSON_Parse(json_string); - if (json == NULL) + if (json == NULL) { json_parse_error_msg(); goto END; @@ -76,7 +76,7 @@ void search(char *tags) /* Get the posts array. */ cJSON *posts = cJSON_GetObjectItem(json, "posts"); - if (!cJSON_IsArray(posts)) + if (!cJSON_IsArray(posts)) { posts_not_array_error_msg(); goto END; @@ -86,12 +86,12 @@ void search(char *tags) size_t post_i = 0; /* Write the posts and tags to windows. */ - for (size_t i = 0; i < cJSON_GetArraySize(posts); i++) + for (size_t i = 0; i < cJSON_GetArraySize(posts); i++) { /* Take the post with specified index. */ cJSON *post = cJSON_GetArrayItem(posts, i); - if (post == NULL) + if (post == NULL) { post_error_msg(); goto END; @@ -100,7 +100,7 @@ void search(char *tags) /* Take the file object of the specified post. */ cJSON *file_object = cJSON_GetObjectItem(post, "file"); - if (file_object == NULL) + if (file_object == NULL) { fprintf(stderr, "File object not found in post %d\n", i); goto END; @@ -109,77 +109,78 @@ void search(char *tags) /* Take the url of the of the specified post by parsing the data from file object. */ cJSON *url = cJSON_GetObjectItem(file_object, "url"); - if (post_i == 0) - { - /* The tags object parsing. */ - cJSON *tags_object = cJSON_GetObjectItem(post, "tags"); - - if (tags_object == NULL) + if (post_i == 0) { - fprintf(stderr, "Tags object not found in post %d\n", i); - goto END; - } + /* The tags object parsing. */ + cJSON *tags_object = cJSON_GetObjectItem(post, "tags"); - /* The general tags array parsing. */ - cJSON *general_tags = cJSON_GetObjectItem(tags_object, "general"); - - wmove(post_tags_window, 1, 1); + if (tags_object == NULL) + { + fprintf(stderr, "Tags object not found in post %d\n", i); + goto END; + } - int line_counter = 0; - int max_width = getmaxx(post_tags_window) - 2; // Get the window width, minus 2 for borders - int max_height = 6; // Fixed height for the window - int line_to_write = 1; + /* The general tags array parsing. */ + cJSON *general_tags = cJSON_GetObjectItem(tags_object, "general"); - if (cJSON_IsArray(general_tags)) - { - for (size_t j = 0; j < cJSON_GetArraySize(general_tags); j++) - { - cJSON *tag = cJSON_GetArrayItem(general_tags, j); + wmove(post_tags_window, 1, 1); + + int line_counter = 0; + int max_width = getmaxx(post_tags_window) - 2; // Get the window width, minus 2 for borders + int max_height = 6; // Fixed height for the window + int line_to_write = 1; - for (size_t k = 0; k < strlen(tag->valuestring); k++) + if (cJSON_IsArray(general_tags)) + { + for (size_t j = 0; j < cJSON_GetArraySize(general_tags); j++) { - if (line_counter >= max_width) + cJSON *tag = cJSON_GetArrayItem(general_tags, j); + + for (size_t k = 0; k < strlen(tag->valuestring); k++) + { + if (line_counter >= max_width) + { + line_to_write++; + if (line_to_write >= max_height) // Check for vertical overflow + { + break; + } + wmove(post_tags_window, line_to_write, 1); + line_counter = 0; + } + + wprintw(post_tags_window, "%c", tag->valuestring[k]); + wrefresh(post_tags_window); + line_counter++; + } + + if (line_counter + 2 >= max_width) // Check if ", " will overflow { line_to_write++; + if (line_to_write >= max_height) // Check for vertical overflow { break; } + wmove(post_tags_window, line_to_write, 1); line_counter = 0; + } + else + { + wprintw(post_tags_window, ", "); + line_counter += 2; } - wprintw(post_tags_window, "%c", tag->valuestring[k]); - wrefresh(post_tags_window); - line_counter++; - } - - if (line_counter + 2 >= max_width) // Check if ", " will overflow - { - line_to_write++; if (line_to_write >= max_height) // Check for vertical overflow { break; } - wmove(post_tags_window, line_to_write, 1); - line_counter = 0; - } - else - { - wprintw(post_tags_window, ", "); - line_counter += 2; - } - - if (line_to_write >= max_height) // Check for vertical overflow - { - break; } } } - } - - if (url != NULL && cJSON_IsString(url) && post_i != posts_panel_height - 2) + if (url != NULL && cJSON_IsString(url) && post_i != posts_panel_height - 2) { write_post(posts_window, post_i, url); wrefresh(posts_window);