Start parsing a DEC file.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Sat, 8 Jun 2013 12:40:21 +0000 (09:40 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Sat, 8 Jun 2013 12:40:21 +0000 (09:40 -0300)
Read lines from a DEC file and store them in an array for later parsing.

Makefile.am
decfile.c [new file with mode: 0644]
decfile.h [new file with mode: 0644]
rnetclient.c

index 6ecf16d..a9d3f7e 100644 (file)
@@ -1,2 +1,2 @@
 bin_PROGRAMS = rnetclient
-rnetclient_SOURCES = rnetclient.c
+rnetclient_SOURCES = rnetclient.c decfile.c decfile.h
diff --git a/decfile.c b/decfile.c
new file mode 100644 (file)
index 0000000..ebffc9f
--- /dev/null
+++ b/decfile.c
@@ -0,0 +1,118 @@
+/*
+ *  Copyright (C) 2012-2013  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define _GNU_SOURCE
+#include "decfile.h"
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+
+struct rnet_decfile {
+       char *filename;
+       FILE *file;
+       char **lines;
+       int lines_len;
+};
+
+/*
+ * line should be an allocated buffer given to append_line
+ * this means, free(line) will be called when decfile is released
+ */
+static int append_line(struct rnet_decfile *decfile, char *line)
+{
+       size_t len;
+       char **old_lines;
+       decfile->lines_len += 1;
+       len = sizeof(*decfile->lines) * decfile->lines_len;
+       old_lines = decfile->lines;
+       decfile->lines = realloc(decfile->lines, len);
+       if (!decfile->lines) {
+               decfile->lines = old_lines;
+               goto out;
+       }
+       decfile->lines[decfile->lines_len - 1] = line;
+       return 0;
+out:
+       decfile->lines_len -= 1;
+       return -1;
+}
+
+static void decfile_release_lines(struct rnet_decfile *decfile)
+{
+       int i;
+       for (i = 0; i < decfile->lines_len; i++)
+               free(decfile->lines[i]);
+       free(decfile->lines);
+       decfile->lines = NULL;
+}
+
+static int decfile_parse(struct rnet_decfile *decfile)
+{
+       char *buffer = NULL;
+       size_t len = 0;
+       int r;
+       while ((r = getline(&buffer, &len, decfile->file)) > 0) {
+               r = append_line(decfile, buffer);
+               if (r) {
+                       free(buffer);
+                       goto out;
+               }
+               buffer = NULL;
+               len = 0;
+       }
+       return 0;
+out:
+       decfile_release_lines(decfile);
+       return -1;
+}
+
+struct rnet_decfile * rnet_decfile_open(char *filename)
+{
+       struct rnet_decfile *decfile;
+       decfile = malloc(sizeof(*decfile));
+       if (!decfile)
+               return NULL;
+       decfile->filename = strdup(filename);
+       if (!decfile->filename)
+               goto out_filename;
+       decfile->file = fopen(filename, "r");
+       if (!decfile->file)
+               goto out_file;
+       decfile->lines_len = 0;
+       decfile->lines = NULL;
+       if (decfile_parse(decfile))
+               goto out_parse;
+       return decfile;
+out_parse:
+       fclose(decfile->file);
+out_file:
+       free(decfile->filename);
+out_filename:
+       free(decfile);
+       return NULL;
+}
+
+void rnet_decfile_close(struct rnet_decfile *decfile)
+{
+       decfile_release_lines(decfile);
+       fclose(decfile->file);
+       free(decfile->filename);
+       free(decfile);
+}
diff --git a/decfile.h b/decfile.h
new file mode 100644 (file)
index 0000000..0fcf6dd
--- /dev/null
+++ b/decfile.h
@@ -0,0 +1,26 @@
+/*
+ *  Copyright (C) 2012-2013  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _RNET_DECFILE_H
+#define _RNET_DECFILE_H
+
+struct rnet_decfile;
+struct rnet_decfile * rnet_decfile_open(char *filename);
+void rnet_decfile_close(struct rnet_decfile *decfile);
+
+#endif
index b5a8052..e109e5f 100644 (file)
@@ -27,6 +27,7 @@
 #include <netdb.h>
 #include <gnutls/gnutls.h>
 #include <zlib.h>
+#include "decfile.h"
 
 static void * get_creds(char *certfile)
 {
@@ -165,6 +166,12 @@ static int handshake(int c)
        return 0;
 }
 
+static void usage(void)
+{
+       fprintf(stderr, "rnetclient [filename]\n");
+       exit(1);
+}
+
 int main(int argc, char **argv)
 {
        int c;
@@ -172,8 +179,21 @@ int main(int argc, char **argv)
        char buffer[2048];
        char *out;
        size_t olen;
+       struct rnet_decfile *decfile;
        gnutls_session_t session;
+       
+       if (argc < 2) {
+               usage();
+       }
+
+       decfile = rnet_decfile_open(argv[1]);
+       if (!decfile) {
+               fprintf(stderr, "could not parse %s: %s\n", argv[1], strerror(errno));
+               exit(1);
+       }
+
        gnutls_global_init();
+
        session_new(&session);
        r = connect_rnet(&c);
        if (r) {
@@ -198,6 +218,10 @@ int main(int argc, char **argv)
        while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
                write(1, buffer, r);
        close(c);
+
+       rnet_decfile_close(decfile);
+
        gnutls_global_deinit();
+
        return 0;
 }