[39] | 1 | #include <stdio.h> |
---|
| 2 | #include <string.h> |
---|
| 3 | |
---|
[2] | 4 | #include "property.hpp" |
---|
| 5 | #include "jmalloc.hpp" |
---|
| 6 | #include "dprint.hpp" |
---|
[39] | 7 | #include "game.hpp" |
---|
[2] | 8 | |
---|
| 9 | class property |
---|
| 10 | { |
---|
| 11 | public : |
---|
| 12 | char *name; |
---|
| 13 | char *def_str; |
---|
| 14 | int def_num; |
---|
[39] | 15 | property(char const *Name, int Def) |
---|
[2] | 16 | { name=strcpy((char *)jmalloc(strlen(Name)+1,"Property Name"),Name); |
---|
| 17 | def_num=Def; |
---|
| 18 | def_str=NULL; |
---|
| 19 | next=NULL; |
---|
| 20 | } |
---|
| 21 | |
---|
[39] | 22 | property(char const *Name, char const *Def) |
---|
[2] | 23 | { name=strcpy((char *)jmalloc(strlen(Name)+1,"Property Name"),Name); |
---|
| 24 | def_str=strcpy((char *)jmalloc(strlen(Def)+1,"Property text"),Def); |
---|
| 25 | next=NULL; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | void set(int x) |
---|
| 29 | { if (def_str) |
---|
| 30 | { |
---|
| 31 | jfree(def_str); |
---|
| 32 | def_str=NULL; |
---|
| 33 | } |
---|
| 34 | def_num=x; |
---|
| 35 | } |
---|
| 36 | |
---|
[39] | 37 | void set(char const *x) |
---|
[2] | 38 | { |
---|
| 39 | if (def_str) |
---|
| 40 | { |
---|
| 41 | jfree(def_str); |
---|
| 42 | def_str=NULL; |
---|
| 43 | } |
---|
| 44 | def_str=strcpy((char *)jmalloc(strlen(x)+1,"Property text"),x); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | ~property() |
---|
| 48 | { |
---|
| 49 | if (def_str) |
---|
| 50 | jfree(def_str); |
---|
| 51 | jfree(name); |
---|
| 52 | } |
---|
| 53 | property *next; |
---|
| 54 | } ; |
---|
| 55 | |
---|
[39] | 56 | property *property_manager::find(char const *name) |
---|
[2] | 57 | { |
---|
| 58 | for (property *i=first;i;i=i->next) |
---|
| 59 | if (!strcmp(i->name,name)) |
---|
| 60 | return i; |
---|
| 61 | return NULL; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | property_manager::~property_manager() |
---|
| 66 | { |
---|
| 67 | while (first) |
---|
| 68 | { |
---|
| 69 | property *i=first; |
---|
| 70 | first=first->next; |
---|
| 71 | delete i; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
[39] | 75 | int property_manager::get(char const *name, int def) |
---|
[2] | 76 | { |
---|
| 77 | property *f=find(name); |
---|
| 78 | if (!f || f->def_str) |
---|
| 79 | return def; |
---|
| 80 | else return f->def_num; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
[39] | 84 | char const *property_manager::get(char const *name,char const *def) |
---|
[2] | 85 | { |
---|
| 86 | property *f=find(name); |
---|
| 87 | if (!f || !f->def_str) |
---|
| 88 | return def; |
---|
| 89 | else return f->def_str; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
[39] | 93 | void property_manager::set(char const *name, double def) |
---|
[2] | 94 | { |
---|
| 95 | property *f=find(name); |
---|
| 96 | if (f) |
---|
| 97 | f->set((int)def); |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | f=new property(name,(int)def); |
---|
| 101 | f->next=first; |
---|
| 102 | first=f; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
[39] | 106 | void property_manager::set(char const *name, char const *def) |
---|
[2] | 107 | { |
---|
| 108 | property *f=find(name); |
---|
| 109 | if (f) |
---|
| 110 | f->set(def); |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | f=new property(name,def); |
---|
| 114 | f->next=first; |
---|
| 115 | first=f; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
[39] | 120 | void property_manager::save(char const *filename) |
---|
[2] | 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 |
---|
[40] | 133 | fprintf(fp,"%d\n",i->def_num); |
---|
[2] | 134 | } |
---|
| 135 | fclose(fp); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[39] | 140 | void property_manager::load(char const *filename) |
---|
[2] | 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 | } |
---|