aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2022-01-21 11:00:55 -0500
committerjvech <jmvalenciae@unal.edu.co>2022-01-21 11:00:55 -0500
commit9bf647a224ffe3f18a19fd8957406bc4f3e111d2 (patch)
treec6b232e67a8316b678465d189dd09e7991b59fcd /main.c
Text parser made
Diffstat (limited to 'main.c')
-rw-r--r--main.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..7d0370a
--- /dev/null
+++ b/main.c
@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "main.h"
+
+void error(char *c)
+{
+ fprintf(stderr, "%s: %s\n", c, strerror(errno));
+ exit(1);
+}
+
+list *list_create(char title[], char id[])
+{
+ list *x = malloc(sizeof(list));
+ sscanf(title, "%100[^\n]", x->name);
+ sscanf(id, "%11s", x->id);
+ x->next = NULL;
+ return x;
+}
+
+void list_free(list *start)
+{
+ list *i;
+ list *next = NULL;
+ for (i = start; i != NULL; i = next) {
+ next = i->next;
+ free(i);
+ }
+}
+
+void list_append(list *list_first, char title[], char id[])
+{
+ list *i;
+ for(i = list_first; i->next != NULL; i = i->next);
+ i->next = list_create(title, id);
+}
+
+void list_print_item(list *x, int i)
+{
+ list *p;
+ if (i == -1){
+ for (p = x; p->next != NULL; p = p->next);
+ } else {
+ for(p = x; i != 0; p = p->next, i--){
+ if (p == NULL) {
+ printf("List index out of range");
+ exit(1);
+ }
+ }
+ }
+ printf("title: %s\n", p->name);
+ printf("id: %s\n\n", p->id);
+}
+
+list *parse_stream(FILE *file)
+{
+ list *videos = list_create("", "");
+ return videos;
+}
+
+int main(int argc, char **argv)
+{
+ FILE *file;
+ list *videos = list_create("", "");
+ char buffer_id[ID_LENGTH], buffer_name[TITLE_LENGTH];
+ int i;
+
+ if (argc != 2){
+ printf("You must provide 1 argument not %i\n", argc - 1);
+ exit(1);
+ }
+
+ if (!strcmp(argv[1], "-")){
+ file = stdin;
+
+ } else {
+ file = fopen(argv[1], "r");
+ if (file == NULL){
+ error("Can't open file stream");
+ }
+ }
+
+ do {
+ i = fscanf(file, "\"videoRenderer\":{\"videoId\":\"%11s\"", buffer_id);
+ if (i == 1){
+ do{
+ i = fscanf(file, "\"title\":{\"runs\":[{\"text\":\"%100[^\"]\"", buffer_name);
+ if (i == 1){
+ list_append(videos, buffer_name, buffer_id);
+ break;
+ }
+ } while (fscanf(file, "%*c") != EOF);
+ }
+ } while (fscanf(file, "%*c") != EOF);
+
+ list *q;
+ for (q = videos; q != NULL; q = q->next){
+ printf("title: %s\n", q->name);
+ printf("vidId: %s\n", q->id);
+ printf("\n\n");
+ }
+
+ list_free(videos);
+ return 0;
+}
Feel free to download, copy and edit any repo