summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--channel.c50
-rw-r--r--channel.h7
-rw-r--r--config.c6
-rw-r--r--main.c22
4 files changed, 51 insertions, 34 deletions
diff --git a/channel.c b/channel.c
index cab4612..065e606 100644
--- a/channel.c
+++ b/channel.c
@@ -22,45 +22,43 @@ struct channel_t *channel_add(const char *name) {
}
struct channel_t *channel = &channels[channel_count-1];
channel->name = strdup(name);
- channel->file_count = 0;
channel->files = NULL;
return channel;
}
-int channel_set_file_count(struct channel_t *channel, int count) {
- channel->files = malloc(count * sizeof(struct channel_file_t));
- if(!channel->files) {
- char *error = strerror(errno);
- fprintf(stderr, "Could not allocate memory for channel files (%s): %s\n", channel->name, error);
- return 0;
- }
- channel->file_count = count;
- return 1;
-}
-
-int channel_set_file(struct channel_t *channel, int index, const char *path, int rs_index) {
- struct channel_file_t *file;
- /* Make sure index is in range. */
- if(!(index < channel->file_count))
- return 0;
- file = &channel->files[index];
+struct channel_file_t *channel_file_add(struct channel_t *channel, const char *path, int rs_index) {
+ struct channel_file_t *file = malloc(sizeof(struct channel_file_t));
+ struct channel_file_t *last = channel->files;
+ if(last) {
+ while(last->next) last = last->next;
+ last->next = file;
+ } else
+ channel->files = file;
file->path = strdup(path);
file->rs = rs_get(rs_index);
- /* Fail if we don't get a regex set. */
+ file->next = NULL;
if(!file->rs)
- return 0;
- return 1;
+ return NULL;
+ return file;
+}
+
+int channel_get_count() {
+ return channel_count;
+}
+
+struct channel_t *channel_get(int index) {
+ return (index < channel_count ? &channels[index] : NULL);
}
void channel_free() {
for(int i = 0; i < channel_count; i++) {
free(channels[i].name);
- /* Free all file path strings. */
- for(int j = 0; j < channels[i].file_count; j++) {
- free(channels[i].files[j].path);
+ struct channel_file_t *file = channels[i].files;
+ while(file) {
+ struct channel_file_t *next = file->next;
+ free(file);
+ file = next;
}
- /* 'files' is a dynamically allocated array, must be freed. */
- free(channels[i].files);
}
free(channels);
channels = NULL;
diff --git a/channel.h b/channel.h
index cda0df5..73f2dde 100644
--- a/channel.h
+++ b/channel.h
@@ -6,18 +6,19 @@
struct channel_file_t {
char *path;
struct regexset_t *rs;
+ struct channel_file_t *next;
};
struct channel_t {
char *name;
- int file_count;
struct channel_file_t *files;
};
void channel_init();
struct channel_t *channel_add(const char *name);
-int channel_set_file_count(struct channel_t *channel, int count);
-int channel_set_file(struct channel_t *channel, int index, const char *path, int rs_index);
+struct channel_file_t *channel_file_add(struct channel_t *channel, const char *path, int rs_index);
+int channel_get_count();
+struct channel_t *channel_get(int index);
void channel_free();
#endif
diff --git a/config.c b/config.c
index 3d683c2..d50c90b 100644
--- a/config.c
+++ b/config.c
@@ -59,7 +59,6 @@ int cfg_init() {
sprintf(sname, "channel #%d", i+1);
name = sname;
}
- printf("Channel %s\n", name);
struct channel_t *channel;
if(!(channel = channel_add(name))) {
return 0;
@@ -70,9 +69,6 @@ int cfg_init() {
continue;
}
int file_count = config_setting_length(files);
- /* Break if we can't set file count, most likely any following channels will also fail. */
- if(!channel_set_file_count(channel, file_count))
- break;
/* Iterate through files. */
for(int j = 0; j < file_count; j++) {
const char *filepath;
@@ -96,7 +92,7 @@ int cfg_init() {
filepath = config_setting_get_string(file);
rs_index = 0;
}
- if(!channel_set_file(channel, j, filepath, rs_index)) {
+ if(!channel_file_add(channel, filepath, rs_index)) {
fprintf(stderr, "Failed to set file #%d (%s) for channel %s.\n", j+1, filepath, name);
}
}
diff --git a/main.c b/main.c
index 9f822ed..a3b3c7e 100644
--- a/main.c
+++ b/main.c
@@ -17,6 +17,28 @@ int main(int argc, char **argv) {
}
/* Parsing stuff goes here. */
+ for(int chan_i = 0; chan_i < channel_get_count(); chan_i++) {
+ struct channel_t *channel = channel_get(chan_i);
+ printf("Channel %s\n", channel->name);
+ struct channel_file_t *file = channel->files;
+ while(file) {
+ struct regexset_t *rs = file->rs;
+ FILE *f = fopen(file->path, "r");
+ if(!f) {
+ fprintf(stderr, "\tFailed to open %s\n", file->path);
+ file = file->next;
+ continue;
+ } else
+ printf("\tParsing %s\n", file->path);
+
+ char line[0x2ff];
+ while(fgets(line, 0x2ff, f)) {
+ // TODO: Magic.
+ }
+
+ file = file->next;
+ }
+ }
cfg_free();
channel_free();