· 9 years ago · Nov 06, 2016, 06:42 PM
1//
2// main.c
3// databasebuilder
4//
5// Created by Matan Bareket on 6/3/15.
6//
7//
8
9#include <stdio.h>
10#include <libxml/xmlreader.h>
11#include <time.h>
12#include <sqlite3.h>
13
14int main(int argc, const char * argv[]) {
15
16 // insert code here...
17 printf("starting program");
18 sqlite3 *db;
19 int rc;
20 rc = sqlite3_open("/Users/matanbareket/Dropbox/Projects/Old/IV-Play2/ivplay.db", &db);
21 if (rc) {
22 sqlite3_close(db);
23 printf("Error opening db");
24 return 0;
25 }
26 sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS machine (name TEXT PRIMARY KEY, description TEXT, year INTEGER, cloneof TEXT, ismechanical INTEGER)", NULL, NULL, NULL);
27 sqlite3_exec(db, "PRAGMA synchronous = OFF; PRAGMA journal_mode = MEMORY; BEGIN TRANSACTION;", NULL, NULL, NULL);
28 printf("Parsing");
29 time_t start_time = time(NULL);
30 int stream = streamFile("/Users/matanbareket/Dropbox/Projects/Old/IV-Play2/mame/mame.xml", db);
31 time_t end_time = time(NULL);
32
33 sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
34 fprintf(stdout, "Elapsed %f seconds\n", difftime(end_time, start_time));
35 return 0;
36}
37
38
39void processMachine(xmlNodePtr xmlNode, sqlite3* db) {
40 /* handling of a node in the tree */
41 xmlChar* name;
42 xmlChar* cloneof;
43 xmlChar* description;
44 xmlChar* year;
45 xmlChar* isbios;
46 xmlChar* runnable;
47 xmlChar* ismechanical;
48
49 name = xmlGetProp(xmlNode, (const xmlChar*)"name");
50 cloneof = xmlGetProp(xmlNode, (const xmlChar*)"cloneof");
51
52 isbios = xmlGetProp(xmlNode, (const xmlChar*)"isbios");
53 runnable = xmlGetProp(xmlNode, (const xmlChar*)"runnable");
54 ismechanical = xmlGetProp(xmlNode, (const xmlChar*)"ismechanical");
55
56 if (ismechanical != NULL && !xmlStrcmp(ismechanical, (const xmlChar*)"yes")) {
57 ismechanical = (xmlChar*) "1";
58 } else {
59 ismechanical = (xmlChar*) "0";
60 }
61 if ( (isbios != NULL && !xmlStrcmp(isbios, (const xmlChar*)"yes")) ||
62 (runnable != NULL && !xmlStrcmp(runnable, (const xmlChar*)"no"))
63 ) {
64// printf("bios or not runnable");
65 return;
66 }
67 xmlNode = xmlNode->xmlChildrenNode;
68 while (xmlNode != NULL) {
69 if (!xmlStrcmp(xmlNode->name, (const xmlChar*)"description")) {
70 description = xmlNodeGetContent(xmlNode);
71 }
72 if (!xmlStrcmp(xmlNode->name, (const xmlChar*)"year")) {
73 year = xmlNodeGetContent(xmlNode);
74 }
75
76 xmlNode = xmlNode->next;
77 }
78 char* query;
79
80 if (cloneof != NULL) {
81 asprintf(&query, "INSERT INTO machine VALUES(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\")", name, description, year, cloneof, ismechanical);
82 } else {
83 asprintf(&query, "INSERT INTO machine VALUES(\"%s\", \"%s\", \"%s\", NULL, \"%s\")", name, description, year, ismechanical);
84 }
85
86// xmlFree(year);
87// xmlFree(description);
88// xmlFree(name);
89// xmlFree(cloneof);
90// xmlFree(xmlNode);
91 sqlite3_exec(db, query, NULL, NULL, NULL);
92 return;
93
94 }
95
96int streamFile(char *filename, sqlite3* db) {
97 xmlTextReaderPtr reader;
98 int ret;
99
100 reader = xmlNewTextReaderFilename(filename);
101 if (reader != NULL) {
102 ret = xmlTextReaderRead(reader);
103 while (ret == 1) {
104 if (xmlStrcmp(xmlTextReaderName(reader), (const xmlChar *) "machine") == 0 && xmlTextReaderNodeType(reader) == 1) {
105 processMachine(xmlTextReaderExpand(reader), db);
106 }
107 ret = xmlTextReaderRead(reader);
108 }
109 xmlFreeTextReader(reader);
110 if (ret != 0) {
111 printf("%s : failed to parse\n", filename);
112 }
113 } else {
114 printf("Unable to open %s\n", filename);
115 }
116 return 0;
117}