html_furi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. return result;
  117. }
  118. static FuriString *_html_furi_find_tag(const char *tag, FuriString *html, size_t index, int *out_next_index)
  119. {
  120. // Clear next index in case of early return
  121. *out_next_index = -1;
  122. int tag_len = strlen(tag);
  123. if (tag_len < 3)
  124. {
  125. FURI_LOG_E("html_furi_parse", "Invalid tag length");
  126. return NULL;
  127. }
  128. // Extract "p" from "<p>"
  129. int inner_len = tag_len - 2;
  130. char inner_tag[inner_len + 1];
  131. for (int i = 0; i < inner_len; i++)
  132. {
  133. inner_tag[i] = tag[i + 1];
  134. }
  135. inner_tag[inner_len] = '\0';
  136. // Create closing tag => "</p>"
  137. char closing_tag[inner_len + 4];
  138. snprintf(closing_tag, sizeof(closing_tag), "</%s>", inner_tag);
  139. int html_len = furi_string_size(html);
  140. // 1) Find opening tag from `index`.
  141. int open_tag_index = -1;
  142. for (int i = index; i <= html_len - tag_len; i++)
  143. {
  144. if (furi_string_sub_equals(html, i, tag))
  145. {
  146. open_tag_index = i;
  147. break;
  148. }
  149. }
  150. if (open_tag_index == -1)
  151. {
  152. return NULL; // no more occurrences
  153. }
  154. // The content begins after the opening tag.
  155. int content_start = open_tag_index + tag_len;
  156. // skip leading spaces
  157. while (content_start < html_len && furi_string_get_char(html, content_start) == ' ')
  158. {
  159. content_start++;
  160. }
  161. int depth = 1;
  162. int i = content_start;
  163. int matching_close_index = -1;
  164. while (i < html_len)
  165. {
  166. if (furi_string_sub_equals(html, i, tag))
  167. {
  168. depth++;
  169. i += tag_len;
  170. }
  171. else if (furi_string_sub_equals(html, i, closing_tag))
  172. {
  173. depth--;
  174. i += strlen(closing_tag);
  175. if (depth == 0)
  176. {
  177. matching_close_index = i - strlen(closing_tag);
  178. // i now points just after "</p>"
  179. break;
  180. }
  181. }
  182. else
  183. {
  184. i++;
  185. }
  186. }
  187. if (matching_close_index == -1)
  188. {
  189. // No matching close tag found
  190. return NULL;
  191. }
  192. size_t content_length = matching_close_index - content_start;
  193. // Allocate the result
  194. FuriString *result = furi_string_alloc();
  195. furi_string_reserve(result, content_length + 1); // +1 for safety
  196. furi_string_set_n(result, html, content_start, content_length);
  197. *out_next_index = i;
  198. return result;
  199. }
  200. /*
  201. * Parse *all* occurrences of <tag> in `html`, handling nested tags.
  202. * Returns a FuriString concatenating all parsed contents.
  203. */
  204. FuriString *html_furi_find_tags(const char *tag, FuriString *html)
  205. {
  206. FuriString *result = furi_string_alloc();
  207. size_t index = 0;
  208. while (true)
  209. {
  210. int next_index;
  211. FuriString *parsed = _html_furi_find_tag(tag, html, index, &next_index);
  212. if (parsed == NULL)
  213. {
  214. // No more tags from 'index' onward
  215. break;
  216. }
  217. // Append the found content
  218. furi_string_cat(result, parsed);
  219. furi_string_free(parsed);
  220. // Resume searching at `next_index` (just after `</tag>`).
  221. index = next_index;
  222. }
  223. return result;
  224. }
  225. /*
  226. * @brief Check if an HTML tag exists in the provided HTML string.
  227. * @param tag The HTML tag to search for (including the angle brackets).
  228. * @param html The HTML string to search (as a FuriString).
  229. * @param index The starting index to search from.
  230. * @return True if the tag exists in the HTML string, false otherwise.
  231. */
  232. bool html_furi_tag_exists(const char *tag, FuriString *html, size_t index)
  233. {
  234. int tag_len = strlen(tag);
  235. if (tag_len < 3)
  236. {
  237. FURI_LOG_E("html_furi_parse", "Invalid tag length");
  238. return false;
  239. }
  240. int html_len = furi_string_size(html);
  241. for (int i = index; i <= html_len - tag_len; i++)
  242. {
  243. if (furi_string_sub_equals(html, i, tag))
  244. {
  245. return true;
  246. }
  247. }
  248. return false;
  249. }