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

Last change on this file since 40 was 40, checked in by Sam Hocevar, 15 years ago
  • Fix a few more warnings.
  • Remove -Wshadow from the warnings because it is too verbose for now.
  • Remove warning flags that have no meaning in C++.
File size: 3.2 KB
Line 
1#include <stdio.h>
2#include <string.h>
3
4#include "property.hpp"
5#include "jmalloc.hpp"
6#include "dprint.hpp"
7#include "game.hpp"
8
9class property
10{
11  public :
12  char *name;
13  char *def_str;
14  int def_num;
15  property(char const *Name, int Def)
16  { name=strcpy((char *)jmalloc(strlen(Name)+1,"Property Name"),Name);
17    def_num=Def;
18    def_str=NULL;
19    next=NULL;
20  }
21
22  property(char const *Name, char const *Def)
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
37  void set(char const *x)
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
56property *property_manager::find(char const *name)
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
65property_manager::~property_manager()
66{
67  while (first)
68  {
69    property *i=first;
70    first=first->next;
71    delete i;
72  }
73}
74
75int property_manager::get(char const *name, int def)
76{
77  property *f=find(name);
78  if (!f || f->def_str)
79    return def;
80  else return f->def_num;
81}
82
83
84char const *property_manager::get(char const *name,char const *def)
85{
86  property *f=find(name);
87  if (!f || !f->def_str)
88    return def;
89  else return f->def_str;
90}
91
92
93void property_manager::set(char const *name, double def)
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
106void property_manager::set(char const *name, char const *def)
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
120void property_manager::save(char const *filename)
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
133        fprintf(fp,"%d\n",i->def_num);     
134    }
135    fclose(fp);
136  }
137}
138
139
140void property_manager::load(char const *filename)
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}
Note: See TracBrowser for help on using the repository browser.