summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2011-08-30 18:40:25 -0700
committerGravatar Linus Torvalds <torvalds@linux-foundation.org>2011-08-30 18:40:25 -0700
commit5c4cc39c56470b0a01274e69be703f0d4fc3080b (patch)
treedced6401e2eeec88b659f8781df424b5227af4ee
parentf3a338a9af53f31f9a72de021950cbe725a6782f (diff)
downloadsubsurface-5c4cc39c56470b0a01274e69be703f0d4fc3080b.tar.gz
Start moving some of the non-parsing stuff out of 'parse.c'
Create a 'main.c' with the main routine and argument "parsing" etc. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--Makefile10
-rw-r--r--dive.h12
-rw-r--r--main.c89
-rw-r--r--parse.c106
4 files changed, 127 insertions, 90 deletions
diff --git a/Makefile b/Makefile
index 648912bd6..675fb50b6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,11 @@
CC=gcc
CFLAGS=-Wall -Wno-pointer-sign -g
-parse: parse.c dive.h
- $(CC) $(CFLAGS) -o parse `xml2-config --cflags` parse.c `xml2-config --libs`
+parse: main.o parse.o
+ $(CC) $(LDLAGS) -o parse main.o parse.o `xml2-config --libs`
+
+parse.o: parse.c dive.h
+ $(CC) $(CFLAGS) -c `xml2-config --cflags` parse.c
+
+main.o: main.c dive.h
+ $(CC) $(CFLAGS) -c main.c
diff --git a/dive.h b/dive.h
index 9fc390867..53ecdfe00 100644
--- a/dive.h
+++ b/dive.h
@@ -111,4 +111,16 @@ struct dive {
struct sample sample[];
};
+extern int verbose;
+
+struct dive_table {
+ int nr, allocated;
+ struct dive **dives;
+};
+
+extern struct dive_table dive_table;
+
+void parse_xml_init(void);
+void parse_xml_file(const char *filename);
+
#endif /* DIVE_H */
diff --git a/main.c b/main.c
new file mode 100644
index 000000000..6da386ccd
--- /dev/null
+++ b/main.c
@@ -0,0 +1,89 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "dive.h"
+
+static void show_dive(int nr, struct dive *dive)
+{
+ int i;
+ struct tm *tm;
+
+ tm = gmtime(&dive->when);
+
+ printf("At %02d:%02d:%02d %04d-%02d-%02d (%d ft max, %d minutes)\n",
+ tm->tm_hour, tm->tm_min, tm->tm_sec,
+ tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
+ to_feet(dive->maxdepth), dive->duration.seconds / 60);
+
+ if (!verbose)
+ return;
+
+ for (i = 0; i < dive->samples; i++) {
+ struct sample *s = dive->sample + i;
+
+ printf("%4d:%02d: %3d ft, %2d C, %4d PSI\n",
+ s->time.seconds / 60,
+ s->time.seconds % 60,
+ to_feet(s->depth),
+ to_C(s->temperature),
+ to_PSI(s->tankpressure));
+ }
+}
+
+static int sortfn(const void *_a, const void *_b)
+{
+ const struct dive *a = *(void **)_a;
+ const struct dive *b = *(void **)_b;
+
+ if (a->when < b->when)
+ return -1;
+ if (a->when > b->when)
+ return 1;
+ return 0;
+}
+
+static void report_dives(void)
+{
+ int i;
+
+ qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
+ for (i = 0; i < dive_table.nr; i++)
+ show_dive(i+1, dive_table.dives[i]);
+}
+
+static void parse_argument(const char *arg)
+{
+ const char *p = arg+1;
+
+ do {
+ switch (*p) {
+ case 'v':
+ verbose++;
+ continue;
+ default:
+ fprintf(stderr, "Bad argument '%s'\n", arg);
+ exit(1);
+ }
+ } while (*++p);
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+
+ parse_xml_init();
+
+ for (i = 1; i < argc; i++) {
+ const char *a = argv[i];
+
+ if (a[0] == '-') {
+ parse_argument(a);
+ continue;
+ }
+ parse_xml_file(a);
+ }
+ report_dives();
+ return 0;
+}
+
diff --git a/parse.c b/parse.c
index e3b07c6db..1a8e3ba61 100644
--- a/parse.c
+++ b/parse.c
@@ -9,68 +9,28 @@
#include "dive.h"
-static int verbose;
+int verbose;
-static struct dive **dive_table;
-static int nr_dives, nr_allocated;
+struct dive_table dive_table;
+/*
+ * Add a dive into the dive_table array
+ */
static void record_dive(struct dive *dive)
{
- if (nr_dives >= nr_allocated) {
- nr_allocated = (nr_dives + 32) * 3 / 2;
- dive_table = realloc(dive_table, nr_allocated * sizeof(struct dive *));
- if (!dive_table)
- exit(1);
- }
- dive_table[nr_dives++] = dive;
-}
-
-static void show_dive(int nr, struct dive *dive)
-{
- int i;
- struct tm *tm;
-
- tm = gmtime(&dive->when);
-
- printf("At %02d:%02d:%02d %04d-%02d-%02d (%d ft max, %d minutes)\n",
- tm->tm_hour, tm->tm_min, tm->tm_sec,
- tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
- to_feet(dive->maxdepth), dive->duration.seconds / 60);
+ int nr = dive_table.nr, allocated = dive_table.allocated;
+ struct dive **dives = dive_table.dives;
- if (!verbose)
- return;
-
- for (i = 0; i < dive->samples; i++) {
- struct sample *s = dive->sample + i;
-
- printf("%4d:%02d: %3d ft, %2d C, %4d PSI\n",
- s->time.seconds / 60,
- s->time.seconds % 60,
- to_feet(s->depth),
- to_C(s->temperature),
- to_PSI(s->tankpressure));
+ if (nr >= allocated) {
+ allocated = (nr + 32) * 3 / 2;
+ dives = realloc(dives, allocated * sizeof(struct dive *));
+ if (!dives)
+ exit(1);
+ dive_table.dives = dives;
+ dive_table.allocated = allocated;
}
-}
-
-static int sortfn(const void *_a, const void *_b)
-{
- const struct dive *a = *(void **)_a;
- const struct dive *b = *(void **)_b;
-
- if (a->when < b->when)
- return -1;
- if (a->when > b->when)
- return 1;
- return 0;
-}
-
-static void report_dives(void)
-{
- int i;
- qsort(dive_table, nr_dives, sizeof(struct dive *), sortfn);
-
- for (i = 0; i < nr_dives; i++)
- show_dive(i+1, dive_table[i]);
+ dives[nr] = dive;
+ dive_table.nr = nr+1;
}
static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
@@ -578,7 +538,7 @@ static void traverse(xmlNode *node)
}
}
-static void parse_xml_file(const char *filename)
+void parse_xml_file(const char *filename)
{
xmlDoc *doc;
@@ -595,37 +555,7 @@ static void parse_xml_file(const char *filename)
xmlCleanupParser();
}
-static void parse_argument(const char *arg)
+void parse_xml_init(void)
{
- const char *p = arg+1;
-
- do {
- switch (*p) {
- case 'v':
- verbose++;
- continue;
- default:
- fprintf(stderr, "Bad argument '%s'\n", arg);
- exit(1);
- }
- } while (*++p);
-}
-
-int main(int argc, char **argv)
-{
- int i;
-
LIBXML_TEST_VERSION
-
- for (i = 1; i < argc; i++) {
- const char *a = argv[i];
-
- if (a[0] == '-') {
- parse_argument(a);
- continue;
- }
- parse_xml_file(a);
- }
- report_dives();
- return 0;
}