source: abuse/trunk/src/property.cpp

Last change on this file was 555, checked in by Sam Hocevar, 8 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 3.5 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, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <stdio.h>
16#include <string.h>
17
18#include "common.h"
19
20#include "property.h"
21#include "dprint.h"
22#include "game.h"
23
24class property
25{
26  public :
27  char *name;
28  char *def_str;
29  int def_num;
30  property(char const *Name, int Def)
31  { name = strdup(Name);
32    def_num = Def;
33    def_str = NULL;
34    next = NULL;
35  }
36
37  property(char const *Name, char const *Def)
38  { name = strdup(Name);
39    def_str = strdup(Def);
40    next = NULL;
41  }
42
43  void set(int x)
44  { if (def_str)
45    {
46      free(def_str);
47      def_str=NULL;
48    }
49    def_num=x;
50  }
51
52  void set(char const *x)
53  {
54    if (def_str)
55    {
56      free(def_str);
57      def_str=NULL;
58    }
59    def_str = strdup(x);
60  }
61
62  ~property()
63  {
64    if (def_str)
65      free(def_str);
66    free(name);
67  }
68  property *next;
69} ;
70
71property *property_manager::find(char const *name)
72{
73  for (property *i=first; i; i=i->next)
74    if (!strcmp(i->name,name))
75      return i;
76  return NULL;
77}
78
79
80property_manager::~property_manager()
81{
82  while (first)
83  {
84    property *i=first;
85    first=first->next;
86    delete i;
87  }
88}
89
90int property_manager::get(char const *name, int def)
91{
92  property *f=find(name);
93  if (!f || f->def_str)
94    return def;
95  else return f->def_num;
96}
97
98
99char const *property_manager::get(char const *name,char const *def)
100{
101  property *f=find(name);
102  if (!f || !f->def_str)
103    return def;
104  else return f->def_str;
105}
106
107
108void property_manager::set(char const *name, double def)
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;
118  }
119}
120
121void property_manager::set(char const *name, char const *def)
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;
131  }
132}
133
134
135void property_manager::save(char const *filename)
136{
137  FILE *fp=open_FILE(filename,"wb");
138  if (!fp)
139    dprintf("Error opening %s to save properties\n",filename);
140  else
141  {
142    for (property *i=first; i; i=i->next)
143    {
144      fprintf(fp,"%s = ",i->name);
145      if (i->def_str)
146        fprintf(fp,"\"%s\"\n",i->def_str);
147      else
148        fprintf(fp,"%d\n",i->def_num);
149    }
150    fclose(fp);
151  }
152}
153
154
155void property_manager::load(char const *filename)
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      {
165    for (c1=buf,c2=name; *c1 && *c1!='='; c1++,c2++)
166      *c2=*c1;
167    if (*c1==0) { fprintf(stderr,"Missing = for property line %s in file %s\n",buf,filename);
168              exit(1); }
169    *c2=' ';
170    while (*c2==' ') { *c2=0; c2--; }
171    c1++; while (*c1==' ') c1++;
172    if (*c1=='"')
173    { c1++;
174      for (c2=str; *c1 && *c1!='"'; c1++,c2++)
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);
189      }
190    }
191      }
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.