station_list.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <string.h>
  2. #include <dfs_posix.h>
  3. #include "station_list.h"
  4. static rt_uint32_t read_line(int fd, char* line, rt_uint32_t line_size)
  5. {
  6. char *pos, *next;
  7. rt_uint32_t length;
  8. length = read(fd, line, line_size);
  9. if (length > 0)
  10. {
  11. pos = strstr(line, "\r\n");
  12. if (pos == RT_NULL)
  13. {
  14. pos = strstr(line, "\n");
  15. next = pos ++;
  16. }
  17. else next = pos + 2;
  18. if (pos != RT_NULL)
  19. {
  20. *pos = '\0';
  21. /* move back */
  22. lseek(fd, -(length - (next - line)), SEEK_CUR);
  23. length = pos - line;
  24. }
  25. else length = 0;
  26. }
  27. return length;
  28. }
  29. static char *strncasestr(const char *haystack, const char *needle)
  30. {
  31. size_t nl=strlen(needle);
  32. size_t hl=strlen(haystack);
  33. size_t i;
  34. if (!nl) goto found;
  35. if (nl>hl) return 0;
  36. for (i=hl-nl+1; i; --i)
  37. {
  38. if (*haystack==*needle && !strncasecmp(haystack,needle,nl))
  39. found:
  40. return (char*)haystack;
  41. ++haystack;
  42. }
  43. return 0;
  44. }
  45. /*
  46. station list file format:
  47. [playlist]
  48. numberofentries=1
  49. File1=http://67.159.5.47:8110
  50. Title1=(#1 - 7/500) The Dominican.net Radio
  51. */
  52. struct station_list* station_list_create(const char* fn)
  53. {
  54. int fd;
  55. rt_uint32_t length, index;
  56. struct station_list* list;
  57. char *line, *pos, prefix[8];
  58. list = (struct station_list*)rt_malloc(sizeof(struct station_list));
  59. if (list == RT_NULL) goto _return0; /* no memory */
  60. list->count = 0; list->items = RT_NULL;
  61. #define LINE_BUFFER_SIZE 128
  62. line = rt_malloc(LINE_BUFFER_SIZE);
  63. if (line == RT_NULL)
  64. goto _return1; /* no memory */
  65. fd = open(fn, O_RDONLY, 0);
  66. if (fd < 0) goto _return2; /* open file failed */
  67. length = read_line(fd, line, LINE_BUFFER_SIZE);
  68. pos = strncasestr(line, "[playlist]");
  69. if (pos == RT_NULL)
  70. {
  71. station_list_destroy(list);
  72. list = RT_NULL;
  73. goto _return2;
  74. }
  75. length = read_line(fd, line, LINE_BUFFER_SIZE);
  76. pos = strncasestr(line, "numberofentries=");
  77. if (pos != RT_NULL)
  78. {
  79. list->count = (int)strtol(pos + strlen("numberofentries="), RT_NULL, 10);
  80. if (list->count > 0)
  81. {
  82. list->items = (struct station_item*) rt_malloc (sizeof(struct station_item) * list->count);
  83. rt_memset(list->items, 0, sizeof(struct station_item) * list->count);
  84. }
  85. }
  86. else
  87. {
  88. station_list_destroy(list);
  89. list = RT_NULL;
  90. goto _return2;
  91. }
  92. if (list->items == RT_NULL)
  93. {
  94. station_list_destroy(list);
  95. list = RT_NULL;
  96. goto _return2;
  97. }
  98. index = 0;
  99. while (index < list->count)
  100. {
  101. length = read_line(fd, line, LINE_BUFFER_SIZE);
  102. if (length > 0)
  103. {
  104. rt_snprintf(prefix, sizeof(prefix), "File%d", index + 1);
  105. pos = strncasestr(line, prefix);
  106. if (pos != RT_NULL)
  107. strncpy(list->items[index].url, pos + strlen(prefix) + 1, 128);
  108. rt_snprintf(prefix, sizeof(prefix), "Title%d", index + 1);
  109. pos = strncasestr(line, prefix);
  110. if (pos != RT_NULL)
  111. {
  112. strncpy(list->items[index].title, pos + strlen(prefix) + 1, 40);
  113. index ++;
  114. }
  115. }
  116. else break;
  117. }
  118. _return2:
  119. close(fd);
  120. _return1:
  121. rt_free(line);
  122. _return0:
  123. return list;
  124. }
  125. void station_list_destroy(struct station_list* list)
  126. {
  127. /* release memory */
  128. rt_free(list->items);
  129. rt_free(list);
  130. }
  131. /* update station list file from network */
  132. void station_list_update(struct rtgui_workbench* workbench)
  133. {
  134. }
  135. static void station_list_selected(void* parameter)
  136. {
  137. rtgui_list_view_t *view;
  138. view = RTGUI_LIST_VIEW(parameter);
  139. if (view->current_item != 0)
  140. rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_OK);
  141. else
  142. rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_CANCEL);
  143. }
  144. /* select a station from list */
  145. struct station_item* station_list_select(struct station_list* list, struct rtgui_workbench* workbench)
  146. {
  147. rt_size_t index;
  148. rtgui_rect_t rect;
  149. rtgui_list_view_t *view;
  150. struct rtgui_list_item* items;
  151. struct station_item* station;
  152. char exit_str[] = "·µ»Ø..";
  153. RT_ASSERT(list != RT_NULL);
  154. RT_ASSERT(workbench != RT_NULL);
  155. station = RT_NULL;
  156. items = (struct rtgui_list_item*) rt_malloc (sizeof(struct rtgui_list_item) * (list->count + 1));
  157. if (items == RT_NULL) return RT_NULL; /* no memory */
  158. /* create view */
  159. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  160. view = rtgui_list_view_create(items, list->count + 1, &rect);
  161. items[0].image = RT_NULL;
  162. items[0].action = station_list_selected;
  163. items[0].name = exit_str;
  164. items[0].parameter = view;
  165. for (index = 1; index < list->count + 1; index ++)
  166. {
  167. items[index].image = RT_NULL;
  168. items[index].action = station_list_selected;
  169. items[index].name = list->items[index - 1].title;
  170. items[index].parameter = view;
  171. }
  172. /* add view to workbench */
  173. rtgui_workbench_add_view(workbench, RTGUI_VIEW(view));
  174. /* show view as modal */
  175. if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
  176. {
  177. station = &list->items[view->current_item - 1];
  178. }
  179. /* destroy view */
  180. rtgui_list_view_destroy(view);
  181. /* release items */
  182. rt_free(items);
  183. return station;
  184. }