change id,title order. playlist load implement
[melted_gui] / src / playlist.c
index 7abcc47..6d03b08 100644 (file)
@@ -681,101 +681,95 @@ void playlist_item_swap(instance_t* app, int dir)
     pthread_mutex_unlock(&app->playlist.lock);
 };
 
-#if 0
-static int playlist_load_file_ply(instance_t* app, char* filename)
+static int playlist_load_plt(instance_t* app, char* filename, char* err_buf, int err_len)
 {
     FILE* f;
-    char *ID, *CH, *B, *IN, *OUT, *DUR, *REST, *l;
-    int count = 0, i;
+    char *line;
+    int count = 0, i = 0;
     playlist_item_t* items;
 
+    f = fopen(filename, "rt");
+
+    if(!f)
+    {
+        i = errno;
+        snprintf(err_buf, err_len, "Failed to open file [%s], error: %s",
+            filename, strerror(i));
+        return -i;
+    };
+
     /* allocate space for strings and items */
     items = malloc(sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
     memset(items, 0, sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
-    ID = malloc(PATH_MAX);
-    CH = malloc(PATH_MAX);
-    B = malloc(PATH_MAX);
-    IN = malloc(PATH_MAX);
-    OUT = malloc(PATH_MAX);
-    DUR = malloc(PATH_MAX);
-    REST = malloc(PATH_MAX);
-    l = malloc(PATH_MAX);
-
-    /* open and process file */
-    f = fopen(filename, "rt");
-    if(f)
+    line = malloc(PATH_MAX);
+
+    while(1)
     {
-        while( !feof(f) )
-        {
-            char* s;
+        char* s;
 
-            /* load string */
-            memset(l, 0, PATH_MAX);
-            fgets(l, PATH_MAX, f);
+        if(feof(f))
+            break;
 
-            /* remove newlines */
-            if( (s = strchr(l, '\n')) ) *s = 0;
-            if( (s = strchr(l, '\r')) ) *s = 0;
-            if( (s = strchr(l, '\t')) ) *s = 0;
+        /* load string */
+        memset(line, 0, PATH_MAX);
+        fgets(line, PATH_MAX, f);
 
-            /* check for empty line */
-            if(l[0] && l[0] != '#')
-            {
-                if (6 != sscanf(l, "%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%s",
-                    ID, CH, B, IN, OUT, DUR, REST))
-                {
-                    int b = atol(B);
-                    /* setup item */
-                    tc2frames(IN, 25.0, &items[count].in);
-                    tc2frames(DUR, 25.0, &items[count].dur);
-                    strncpy(items[count].id, ID, PATH_MAX);
-                    items[count].player = atol(CH) - 1;
-                    switch(b)
-                    {
-                        case 1: items[count].type = PLAYLIST_ITEM_BLOCK_SINGLE; break;
-                        case 2: items[count].type = PLAYLIST_ITEM_LOOP_BEGIN; break;
-                        case 3: items[count].type = PLAYLIST_ITEM_LOOP_BODY; break;
-                        case 4: items[count].type = PLAYLIST_ITEM_LOOP_END; break;
-                        case 6: items[count].type = PLAYLIST_ITEM_BLOCK_END; break;
-                        case 0:
-                            if(!count)
-                                items[count].type = PLAYLIST_ITEM_BLOCK_BEGIN;
-                            else if(items[count - 1].type == PLAYLIST_ITEM_BLOCK_BEGIN ||
-                                    items[count - 1].type == PLAYLIST_ITEM_BLOCK_BODY)
-                                items[count].type = PLAYLIST_ITEM_BLOCK_BODY;
-                            else
-                                items[count].type = PLAYLIST_ITEM_BLOCK_BEGIN;
-                            break;
-                        default:
-                            if(b >= 1024)
-                                items[count].type = b - 1024;
-                    };
-#if 0
-                    {
-                        char* n;
-                        switch(items[count].type)
-                        {
-                            case PLAYLIST_ITEM_BLOCK_BEGIN:       n = "BLOCK_BEGIN"; break;
-                            case PLAYLIST_ITEM_BLOCK_BODY:        n = "BLOCK_BODY"; break;
-                            case PLAYLIST_ITEM_BLOCK_END:         n = "BLOCK_END"; break;
-                            case PLAYLIST_ITEM_BLOCK_SINGLE:      n = "BLOCK_SINGLE"; break;
-                            case PLAYLIST_ITEM_LOOP_BEGIN:        n = "LOOP_BEGIN"; break;
-                            case PLAYLIST_ITEM_LOOP_BODY:         n = "LOOP_BODY"; break;
-                            case PLAYLIST_ITEM_LOOP_END:          n = "LOOP_END"; break;
-                            case PLAYLIST_ITEM_LOOP_SINGLE:       n = "LOOP_SINGLE"; break;
-                        };
-                        fprintf(stderr, "src=[%s]\ndst=[idx=%d,block=%s,block_id=%d,in=%d,out=%d]\n",
-                            l, count, n, items[count].type, items[count].in, items[count].dur);
-                    };
-#endif
+        /* remove newlines */
+        if( (s = strchr(line, '\n')) ) *s = 0;
+        if( (s = strchr(line, '\r')) ) *s = 0;
+
+        /* check for empty line */
+        if(!line[0]  || line[0] == '#')
+            continue;
 
+        /* check item contine */
+        if(line[0] == '\t')
+        {
+            /* line without header */
+            if(!i)
+                continue;
+
+            /* load playlist item */
+            switch(i)
+            {
+                case 1:         // title
+                    strncpy(items[count].title, line + 1, PATH_MAX);
+                    i++;
+                    break;
+
+                case 2:         // in
+                    tc2frames(line + 1, 25.0, &items[count].in);
+                    i++;
+                    break;
+
+                case 3:         // dur
+                    tc2frames(line + 1, 25.0, &items[count].dur);
+                    i++;
+                    break;
+
+                case 4:         // player
+                    items[count].player = atol(line + 1) - 1;
+                    i++;
+                    break;
+
+                case 5:         // type
+                    items[count].type = atol(line + 1) - 1024;
+
+                    /* last item - break a chain */
+                    i = 0;
                     count++;
-                }
+                    break;
             };
         }
+        else
+        {
+            strcpy(items[count].id, line);
+            i = 1;
+        };
 
-        fclose(f);
-    }
+    };
+
+    fclose(f);
 
     /* add loaded items to playlist */
     if(count)
@@ -790,26 +784,10 @@ static int playlist_load_file_ply(instance_t* app, char* filename)
         pthread_mutex_unlock(&app->playlist.lock);
     }
 
-    /* free data */
+    /* free items */
+    free(line);
     free(items);
-    free(ID);
-    free(CH);
-    free(IN);
-    free(OUT);
-    free(DUR);
-    free(REST);
-    free(l);
-
-    return count;
-};
 
-static int playlist_save_file_ply(instance_t* app, char* filename)
-{
-};
-#endif
-
-static int playlist_load_plt(instance_t* app, char* filename, char* err_buf, int err_len)
-{
     return 0;
 };