source: abuse/trunk/src/property.cpp @ 512

Last change on this file since 512 was 512, checked in by Sam Hocevar, 7 years ago

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

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