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