1 /*--------------------------------------------------------------------------------------------------
2   DataDraw is a CASE tool for generating data structures in C from simple descriptions.
3 --------------------------------------------------------------------------------------------------*/
4 #include <stdlib.h>
5 #include <string.h>
6 #include "htdatabase.h"
7 
8 htGraph htTheGraph;
9 utSym htNameSym, htXSym, htYSym;
10 
11 /*--------------------------------------------------------------------------------------------------
12   Create a new node.
13 --------------------------------------------------------------------------------------------------*/
htNodeCreate(void)14 htNode htNodeCreate(void)
15 {
16     htNode node = htNodeAlloc();
17 
18     htGraphAppendNode(htTheGraph, node);
19     return node;
20 }
21 
22 /*--------------------------------------------------------------------------------------------------
23   Read in the nodes from the text file.
24 --------------------------------------------------------------------------------------------------*/
readNodes(char * filename)25 static void readNodes(
26     char *filename)
27 {
28     char buffer[1024];
29     FILE *file = fopen(filename, "r");
30     htNode node;
31     int x, y;
32 
33     if(file == NULL) {
34         printf("Unable to open file %s\n", filename);
35         exit(1);
36     }
37     while(fscanf(file, "%s %d %d", buffer, &x, &y) == 3) {
38         node = htNodeCreate();
39         htNodeSetStringAttribute(node, htNameSym, buffer);
40         htNodeSetInt64Attribute(node, htXSym, x);
41         htNodeSetInt64Attribute(node, htYSym, y);
42     }
43     fclose(file);
44 }
45 
46 /*--------------------------------------------------------------------------------------------------
47   Just write out the nodes.
48 --------------------------------------------------------------------------------------------------*/
writeNodes(void)49 static void writeNodes(void)
50 {
51     htNode node;
52 
53     htForeachGraphNode(htTheGraph, node) {
54         printf("%s %d %d\n", htNodeGetStringAttribute(node, htNameSym),
55             (int32)htNodeGetInt64Attribute(node, htXSym), (int32)htNodeGetInt64Attribute(node, htYSym));
56     } htEndGraphNode;
57 }
58 
59 /*--------------------------------------------------------------------------------------------------
60   This is the actual main routine.
61 --------------------------------------------------------------------------------------------------*/
main(int argc,char ** argv)62 int main(
63     int argc,
64     char **argv)
65 {
66     utStart();
67     utInitLogFile("hash.log");
68     htDatabaseStart();
69     htTheGraph = htGraphAlloc();
70     htNameSym = utSymCreate("Name");
71     htXSym = utSymCreate("X");
72     htYSym = utSymCreate("Y");
73     readNodes(argv[1]);
74     writeNodes();
75     htDatabaseStop();
76     utStop(false);
77     return 0;
78 }
79