-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
144 lines (116 loc) · 2.48 KB
/
util.c
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
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "util.h"
struct _Movie
{
char name[MAX_LINE_LEN];
char filename[MAX_LINE_LEN];
char path[2*MAX_LINE_LEN];
char valid; // used as gboolean
};
struct _Movie movies[MAX_MOVIES];
char *long2string(long number)
{
static char string[22];
sprintf(string, "%li", number);
return string;
}
long string2long(char *string)
{
return strtol(string, NULL, 10);
}
FILE *open_file(char *filename, long long *filesize)
{
struct stat filestats;
stat(filename, &filestats);
*filesize = filestats.st_size;
static FILE *stream;
stream = fopen(filename, "rb");
if (!stream)
fprintf(stderr, "ERROR: Video File not found!\n");
return stream;
}
char *getl(FILE *stream, char *line) // without \n at the end
{
char *ret;
ret = fgets(line, MAX_LINE_LEN, stream);
line[strlen(line)-1] = 0x00;
return ret;
}
char readmovies()
{
int mnum;
for (mnum = 0; mnum < MAX_MOVIES; mnum++)
movies[mnum].valid = FALSE;
FILE *stream;
stream = fopen(CONFIG_FILE, "rb");
if (!stream) return FALSE;
char line[MAX_LINE_LEN];
char basedir[MAX_LINE_LEN];
char basedir_string[MAX_LINE_LEN];
// Parse config file
mnum = 0;
while(getl(stream, line))
{
if (strings_equal(line, "__BASEDIR"))
{
assert(getl(stream, basedir_string));
if (!strstr(basedir, "__CWD__"))
{
strcpy(basedir, getcwd(NULL, 0));
strcat(basedir, basedir_string+7);
}
else
{
strcpy(basedir, basedir_string);
}
}
if (strings_equal(line, "__MOVIE"))
{
assert(getl(stream, movies[mnum].name));
assert(getl(stream, movies[mnum].filename));
movies[mnum].valid = TRUE;
++mnum;
}
}
// Establish Paths to Files
mnum = 0;
while(movies[mnum].valid)
{
strcpy(movies[mnum].path, basedir);
strcat(movies[mnum].path, movies[mnum].filename);
++mnum;
}
// Dump Paths
mnum = 0;
while(movies[mnum].valid) fprintf(stderr, "Adding File: %s\n", movies[mnum++].path);
fclose(stream);
return TRUE;
}
// Helper for on_movlist_selection_changed to retrieve the filename / path from the list name
char *movie_get_path_by_name(char *name)
{
int mnum = 0;
while(movies[mnum].valid)
{
if (strings_equal(movies[mnum].name, name))
return movies[mnum].path;
++mnum;
}
return NULL;
}
char **get_movie_names()
{
static char *movienames[MAX_LINE_LEN];
int mnum = 0;
while(movies[mnum].valid)
{
movienames[mnum] = movies[mnum].name;
++mnum;
}
return movienames;
}