1 | #include "property.hpp" |
---|
2 | #include "jmalloc.hpp" |
---|
3 | #include <stdio.h> |
---|
4 | #include "dprint.hpp" |
---|
5 | #include <string.h> |
---|
6 | |
---|
7 | class property |
---|
8 | { |
---|
9 | public : |
---|
10 | char *name; |
---|
11 | char *def_str; |
---|
12 | int def_num; |
---|
13 | property(char *Name, int Def) |
---|
14 | { name=strcpy((char *)jmalloc(strlen(Name)+1,"Property Name"),Name); |
---|
15 | def_num=Def; |
---|
16 | def_str=NULL; |
---|
17 | next=NULL; |
---|
18 | } |
---|
19 | |
---|
20 | property(char *Name, char *Def) |
---|
21 | { name=strcpy((char *)jmalloc(strlen(Name)+1,"Property Name"),Name); |
---|
22 | def_str=strcpy((char *)jmalloc(strlen(Def)+1,"Property text"),Def); |
---|
23 | next=NULL; |
---|
24 | } |
---|
25 | |
---|
26 | void set(int x) |
---|
27 | { if (def_str) |
---|
28 | { |
---|
29 | jfree(def_str); |
---|
30 | def_str=NULL; |
---|
31 | } |
---|
32 | def_num=x; |
---|
33 | } |
---|
34 | |
---|
35 | void set(char *x) |
---|
36 | { |
---|
37 | if (def_str) |
---|
38 | { |
---|
39 | jfree(def_str); |
---|
40 | def_str=NULL; |
---|
41 | } |
---|
42 | def_str=strcpy((char *)jmalloc(strlen(x)+1,"Property text"),x); |
---|
43 | } |
---|
44 | |
---|
45 | ~property() |
---|
46 | { |
---|
47 | if (def_str) |
---|
48 | jfree(def_str); |
---|
49 | jfree(name); |
---|
50 | } |
---|
51 | property *next; |
---|
52 | } ; |
---|
53 | |
---|
54 | property *property_manager::find(char *name) |
---|
55 | { |
---|
56 | for (property *i=first;i;i=i->next) |
---|
57 | if (!strcmp(i->name,name)) |
---|
58 | return i; |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | property_manager::~property_manager() |
---|
64 | { |
---|
65 | while (first) |
---|
66 | { |
---|
67 | property *i=first; |
---|
68 | first=first->next; |
---|
69 | delete i; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | int property_manager::get(char *name, int def) |
---|
74 | { |
---|
75 | property *f=find(name); |
---|
76 | if (!f || f->def_str) |
---|
77 | return def; |
---|
78 | else return f->def_num; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | char *property_manager::get(char *name,char *def) |
---|
83 | { |
---|
84 | property *f=find(name); |
---|
85 | if (!f || !f->def_str) |
---|
86 | return def; |
---|
87 | else return f->def_str; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void property_manager::set(char *name, double def) |
---|
92 | { |
---|
93 | property *f=find(name); |
---|
94 | if (f) |
---|
95 | f->set((int)def); |
---|
96 | else |
---|
97 | { |
---|
98 | f=new property(name,(int)def); |
---|
99 | f->next=first; |
---|
100 | first=f; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | void property_manager::set(char *name, char *def) |
---|
105 | { |
---|
106 | property *f=find(name); |
---|
107 | if (f) |
---|
108 | f->set(def); |
---|
109 | else |
---|
110 | { |
---|
111 | f=new property(name,def); |
---|
112 | f->next=first; |
---|
113 | first=f; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | FILE *open_FILE(char *filename, char *mode); |
---|
119 | |
---|
120 | void property_manager::save(char *filename) |
---|
121 | { |
---|
122 | FILE *fp=open_FILE(filename,"wb"); |
---|
123 | if (!fp) |
---|
124 | dprintf("Error opening %s to save properties\n",filename); |
---|
125 | else |
---|
126 | { |
---|
127 | for (property *i=first;i;i=i->next) |
---|
128 | { |
---|
129 | fprintf(fp,"%s = ",i->name); |
---|
130 | if (i->def_str) |
---|
131 | fprintf(fp,"\"%s\"\n",i->def_str); |
---|
132 | else |
---|
133 | fprintf(fp,"%g\n",i->def_num); |
---|
134 | } |
---|
135 | fclose(fp); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void property_manager::load(char *filename) |
---|
141 | { |
---|
142 | char buf[100],*c1,*c2,name[100],str[100]; |
---|
143 | FILE *fp=open_FILE(filename,"rb"); |
---|
144 | if (fp) |
---|
145 | { |
---|
146 | while (!feof(fp)) |
---|
147 | { |
---|
148 | if (fgets(buf,100,fp)) |
---|
149 | { |
---|
150 | for (c1=buf,c2=name;*c1 && *c1!='=';c1++,c2++) |
---|
151 | *c2=*c1; |
---|
152 | if (*c1==0) { fprintf(stderr,"Missing = for property line %s in file %s\n",buf,filename); |
---|
153 | exit(1);} |
---|
154 | *c2=' '; |
---|
155 | while (*c2==' ') { *c2=0; c2--; } |
---|
156 | c1++; while (*c1==' ') c1++; |
---|
157 | if (*c1=='"') |
---|
158 | { c1++; |
---|
159 | for (c2=str;*c1 && *c1!='"';c1++,c2++) |
---|
160 | *c2=*c1; |
---|
161 | *c2=0; |
---|
162 | if (*c1!='"') { fprintf(stderr,"Missing \" for property name %s in file %s\n",name,filename); |
---|
163 | exit(1); } |
---|
164 | set(name,str); |
---|
165 | } else |
---|
166 | { |
---|
167 | double x; |
---|
168 | if (sscanf(c1,"%lg",&x)) |
---|
169 | set(name,x); |
---|
170 | else |
---|
171 | { |
---|
172 | fprintf(stderr,"Bad number/string for property name %s in file %s\n",name,filename); |
---|
173 | exit(1); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|