html_furi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <html/html_furi.h>
  5. /*
  6. * Checks if the substring of the FuriString starting at index `pos`
  7. * matches the given C-string `needle`.
  8. */
  9. static bool furi_string_sub_equals(FuriString *str, int pos, const char *needle)
  10. {
  11. size_t needle_len = strlen(needle);
  12. if ((size_t)pos + needle_len > furi_string_size(str))
  13. {
  14. return false;
  15. }
  16. for (size_t i = 0; i < needle_len; i++)
  17. {
  18. if (furi_string_get_char(str, pos + i) != needle[i])
  19. {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. /*
  26. * Parse the content for a given HTML tag <tag> in `html`, handling nested tags.
  27. * Returns a newly allocated FuriString or NULL on error.
  28. *
  29. * @param tag e.g. "<p>"
  30. * @param html The HTML string to parse.
  31. * @param index The position in `html` from where to start searching.
  32. */
  33. FuriString *html_furi_find_tag(const char *tag, FuriString *html, size_t index)
  34. {
  35. int tag_len = strlen(tag);
  36. if (tag_len < 3)
  37. {
  38. FURI_LOG_E("html_furi_parse", "Invalid tag length");
  39. return NULL;
  40. }
  41. // Extract the tag name from <p> => "p"
  42. int inner_len = tag_len - 2; // exclude '<' and '>'
  43. char inner_tag[inner_len + 1];
  44. for (int i = 0; i < inner_len; i++)
  45. {
  46. inner_tag[i] = tag[i + 1];
  47. }
  48. inner_tag[inner_len] = '\0';
  49. // Build closing tag => "</p>"
  50. char closing_tag[inner_len + 4];
  51. snprintf(closing_tag, sizeof(closing_tag), "</%s>", inner_tag);
  52. int html_len = furi_string_size(html);
  53. // Find the first occurrence of the opening tag
  54. int open_tag_index = -1;
  55. for (int i = index; i <= html_len - tag_len; i++)
  56. {
  57. if (furi_string_sub_equals(html, i, tag))
  58. {
  59. open_tag_index = i;
  60. break;
  61. }
  62. }
  63. if (open_tag_index == -1)
  64. {
  65. // Tag not found
  66. return NULL;
  67. }
  68. // Content starts after the opening tag
  69. int content_start = open_tag_index + tag_len;
  70. // Skip leading whitespace
  71. while (content_start < html_len && furi_string_get_char(html, content_start) == ' ')
  72. {
  73. content_start++;
  74. }
  75. // Find matching closing tag, accounting for nested tags
  76. int depth = 1;
  77. int i = content_start;
  78. int matching_close_index = -1;
  79. while (i <= html_len - 1)
  80. {
  81. if (furi_string_sub_equals(html, i, tag))
  82. {
  83. depth++;
  84. i += tag_len;
  85. continue;
  86. }
  87. if (furi_string_sub_equals(html, i, closing_tag))
  88. {
  89. depth--;
  90. if (depth == 0)
  91. {
  92. matching_close_index = i;
  93. break;
  94. }
  95. i += strlen(closing_tag);
  96. continue;
  97. }
  98. i++;
  99. }
  100. if (matching_close_index == -1)
  101. {
  102. // No matching close => return NULL or partial content as you choose
  103. return NULL;
  104. }
  105. // Copy the content between <tag>...</tag>
  106. size_t content_length = matching_close_index - content_start;
  107. if (memmgr_get_free_heap() < (content_length + 1 + 1024))
  108. {
  109. FURI_LOG_E("html_furi_parse", "Not enough heap to allocate result");
  110. return NULL;
  111. }
  112. // Allocate and copy
  113. FuriString *result = furi_string_alloc();
  114. furi_string_reserve(result, content_length + 1);
  115. furi_string_set_n(result, html, content_start, content_length);
  116. furi_string_trim(result);
  117. return result;
  118. }
  119. static FuriString *_html_furi_find_tag(const char *tag, FuriString *html, size_t index, int *out_next_index)
  120. {
  121. // Clear next index in case of early return
  122. *out_next_index = -1;
  123. int tag_len = strlen(tag);
  124. if (tag_len < 3)
  125. {
  126. FURI_LOG_E("html_furi_parse", "Invalid tag length");
  127. return NULL;
  128. }
  129. // Extract "p" from "<p>"
  130. int inner_len = tag_len - 2;
  131. char inner_tag[inner_len + 1];
  132. for (int i = 0; i < inner_len; i++)
  133. {
  134. inner_tag[i] = tag[i + 1];
  135. }
  136. inner_tag[inner_len] = '\0';
  137. // Create closing tag => "</p>"
  138. char closing_tag[inner_len + 4];
  139. snprintf(closing_tag, sizeof(closing_tag), "</%s>", inner_tag);
  140. int html_len = furi_string_size(html);
  141. // 1) Find opening tag from `index`.
  142. int open_tag_index = -1;
  143. for (int i = index; i <= html_len - tag_len; i++)
  144. {
  145. if (furi_string_sub_equals(html, i, tag))
  146. {
  147. open_tag_index = i;
  148. break;
  149. }
  150. }
  151. if (open_tag_index == -1)
  152. {
  153. return NULL; // no more occurrences
  154. }
  155. // The content begins after the opening tag.
  156. int content_start = open_tag_index + tag_len;
  157. // skip leading spaces
  158. while (content_start < html_len && furi_string_get_char(html, content_start) == ' ')
  159. {
  160. content_start++;
  161. }
  162. int depth = 1;
  163. int i = content_start;
  164. int matching_close_index = -1;
  165. while (i < html_len)
  166. {
  167. if (furi_string_sub_equals(html, i, tag))
  168. {
  169. depth++;
  170. i += tag_len;
  171. }
  172. else if (furi_string_sub_equals(html, i, closing_tag))
  173. {
  174. depth--;
  175. i += strlen(closing_tag);
  176. if (depth == 0)
  177. {
  178. matching_close_index = i - strlen(closing_tag);
  179. // i now points just after "</p>"
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. i++;
  186. }
  187. }
  188. if (matching_close_index == -1)
  189. {
  190. // No matching close tag found
  191. return NULL;
  192. }
  193. size_t content_length = matching_close_index - content_start;
  194. // Allocate the result
  195. FuriString *result = furi_string_alloc();
  196. furi_string_reserve(result, content_length + 1); // +1 for safety
  197. furi_string_set_n(result, html, content_start, content_length);
  198. furi_string_trim(result);
  199. *out_next_index = i;
  200. return result;
  201. }
  202. /*
  203. * Parse *all* occurrences of <tag> in `html`, handling nested tags.
  204. * Returns a FuriString concatenating all parsed contents.
  205. */
  206. FuriString *html_furi_find_tags(const char *tag, FuriString *html)
  207. {
  208. FuriString *result = furi_string_alloc();
  209. size_t index = 0;
  210. while (true)
  211. {
  212. int next_index;
  213. FuriString *parsed = _html_furi_find_tag(tag, html, index, &next_index);
  214. if (parsed == NULL)
  215. {
  216. // No more tags from 'index' onward
  217. break;
  218. }
  219. // Append the found content
  220. furi_string_cat(result, parsed);
  221. furi_string_cat_str(result, "\n");
  222. furi_string_free(parsed);
  223. // Resume searching at `next_index` (just after `</tag>`).
  224. index = next_index;
  225. }
  226. return result;
  227. }
  228. /*
  229. * @brief Check if an HTML tag exists in the provided HTML string.
  230. * @param tag The HTML tag to search for (including the angle brackets).
  231. * @param html The HTML string to search (as a FuriString).
  232. * @param index The starting index to search from.
  233. * @return True if the tag exists in the HTML string, false otherwise.
  234. */
  235. bool html_furi_tag_exists(const char *tag, FuriString *html, size_t index)
  236. {
  237. int tag_len = strlen(tag);
  238. if (tag_len < 3)
  239. {
  240. FURI_LOG_E("html_furi_parse", "Invalid tag length");
  241. return false;
  242. }
  243. int html_len = furi_string_size(html);
  244. for (int i = index; i <= html_len - tag_len; i++)
  245. {
  246. if (furi_string_sub_equals(html, i, tag))
  247. {
  248. return true;
  249. }
  250. }
  251. return false;
  252. }