station_list.c 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#include <string.h>
#include <dfs_posix.h>
#include "station_list.h"

static rt_uint32_t read_line(int fd, char* line, rt_uint32_t line_size)
{
    char *pos, *next;
    rt_uint32_t length;

    length = read(fd, line, line_size);
    if (length > 0)
    {
        pos = strstr(line, "\r\n");
		if (pos == RT_NULL)
		{
			pos = strstr(line, "\n");
			next = pos ++;
		}
		else next = pos + 2;

        if (pos != RT_NULL)
        {
            *pos = '\0';

            /* move back */
            lseek(fd, -(length - (next - line)), SEEK_CUR);

            length = pos - line;
        }
        else length = 0;
    }

    return length;
}

static char *strncasestr(const char *haystack, const char *needle)
{
  size_t nl=strlen(needle);
  size_t hl=strlen(haystack);
  size_t i;

  if (!nl) goto found;
  if (nl>hl) return 0;
  for (i=hl-nl+1; i; --i)
  {
    if (*haystack==*needle && !strncasecmp(haystack,needle,nl))
found:
      return (char*)haystack;
    ++haystack;
  }
  return 0;
}

/*
 station list file format:

 [playlist]
 numberofentries=1
 File1=http://67.159.5.47:8110
 Title1=(#1 - 7/500) The Dominican.net Radio
 */
struct station_list* station_list_create(const char* fn)
{
	int fd;
	rt_uint32_t length, index;
	struct station_list* list;
	char  *line, *pos, prefix[8];

	list = (struct station_list*)rt_malloc(sizeof(struct station_list));
	if (list == RT_NULL) goto _return0; /* no memory */
	list->count = 0; list->items = RT_NULL;
	
#define LINE_BUFFER_SIZE	128
	line = rt_malloc(LINE_BUFFER_SIZE);
	if (line == RT_NULL)
		goto _return1; /* no memory */

	fd = open(fn, O_RDONLY, 0); 
	if (fd < 0) goto _return2; /* open file failed */

	length = read_line(fd, line, LINE_BUFFER_SIZE);
	pos = strncasestr(line, "[playlist]");
	if (pos == RT_NULL) 
	{
		station_list_destroy(list);
		list = RT_NULL;
		goto _return2;
	}

	length = read_line(fd, line, LINE_BUFFER_SIZE);
	pos = strncasestr(line, "numberofentries=");
	if (pos != RT_NULL)
	{
		list->count = (int)strtol(pos + strlen("numberofentries="), RT_NULL, 10);
		if (list->count > 0)
		{
			list->items = (struct station_item*) rt_malloc (sizeof(struct station_item) * list->count);
			rt_memset(list->items, 0, sizeof(struct station_item) * list->count);
		}
	}
	else
	{
		station_list_destroy(list);
		list = RT_NULL;
		goto _return2;
	}

	if (list->items == RT_NULL)
	{
		station_list_destroy(list);
		list = RT_NULL;
		goto _return2;
	}

	index = 0;
	while (index < list->count)
	{
		length = read_line(fd, line, LINE_BUFFER_SIZE);
		if (length > 0)
		{
			rt_snprintf(prefix, sizeof(prefix), "File%d", index + 1);
			pos = strncasestr(line, prefix);
			if (pos != RT_NULL)
				strncpy(list->items[index].url, pos + strlen(prefix) + 1, 128);

			rt_snprintf(prefix, sizeof(prefix), "Title%d", index + 1);
			pos = strncasestr(line, prefix);
			if (pos != RT_NULL)
			{
				strncpy(list->items[index].title, pos + strlen(prefix) + 1, 40);
				index ++;
			}
		}
		else break;
	}

_return2:
	close(fd);
_return1:
	rt_free(line);

_return0:
	return list;
}

void station_list_destroy(struct station_list* list)
{
	/* release memory */
	rt_free(list->items);
	rt_free(list);
}

/* update station list file from network */
void station_list_update(struct rtgui_workbench* workbench)
{
}

static void station_list_selected(void* parameter)
{
	rtgui_list_view_t *view;

	view = RTGUI_LIST_VIEW(parameter);

	if (view->current_item != 0)
		rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_OK);
	else
		rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_CANCEL);
}

/* select a station from list */
struct station_item* station_list_select(struct station_list* list, struct rtgui_workbench* workbench)
{
	rt_size_t index;
	rtgui_rect_t rect;
	rtgui_list_view_t *view;
	struct rtgui_list_item* items;
	struct station_item* station;
	char exit_str[] = "..";

	RT_ASSERT(list != RT_NULL);
	RT_ASSERT(workbench != RT_NULL);

	station = RT_NULL;

	items = (struct rtgui_list_item*) rt_malloc (sizeof(struct rtgui_list_item) * (list->count + 1));
	if (items == RT_NULL) return RT_NULL; /* no memory */

	/* create view */
	rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
	view = rtgui_list_view_create(items, list->count + 1, &rect);

	items[0].image = RT_NULL;
	items[0].action = station_list_selected;
	items[0].name = exit_str;
	items[0].parameter = view;

	for (index = 1; index < list->count + 1; index ++)
	{
		items[index].image = RT_NULL;
		items[index].action = station_list_selected;
		items[index].name = list->items[index - 1].title;
		items[index].parameter = view;
	}

	/* add view to workbench */
	rtgui_workbench_add_view(workbench, RTGUI_VIEW(view));

	/* show view as modal */
	if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
	{
		station = &list->items[view->current_item - 1];
	}

	/* destroy view */
	rtgui_list_view_destroy(view);

	return station;
}