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

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