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