source: abuse/branches/lol/src/property.cpp

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

build: sync with newer Lol Engine changes.

File size: 3.8 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 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 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 "imlib/dprint.h"
21
22#include "property.h"
23#include "game.h"
24
25class AProperty
26{
27public:
28    String m_name;
29    String m_string;
30    int m_num;
31
32    AProperty(char const *name, int def)
33      : m_name(name),
34        m_num(def)
35    {
36    }
37
38    AProperty(char const *name, char const *def)
39      : m_name(name),
40        m_string(def)
41    {
42    }
43
44    void Set(int x)
45    {
46        // FIXME: would empty() work here?
47        if (m_string.count())
48            m_string.resize(0);
49        m_num = x;
50    }
51
52    void Set(char const *x)
53    {
54        m_string = x;
55    }
56};
57
58
59APropertyManager::APropertyManager()
60{
61}
62
63APropertyManager::~APropertyManager()
64{
65    for (int i = 0; i < m_props.count(); ++i)
66        delete m_props[i];
67}
68
69AProperty *APropertyManager::find(char const *name)
70{
71    for (int i = 0; i < m_props.count(); ++i)
72        if (m_props[i]->m_name == name)
73            return m_props[i];
74    return nullptr;
75}
76
77int APropertyManager::get(char const *name, int def)
78{
79    AProperty *f = find(name);
80    if (!f || f->m_string.count())
81        return def;
82    return f->m_num;
83}
84
85char const *APropertyManager::get(char const *name, char const *def)
86{
87    AProperty *f = find(name);
88    if (!f || !f->m_string.count())
89        return def;
90    return f->m_string.C();
91}
92
93void APropertyManager::set(char const *name, double def)
94{
95    AProperty *f = find(name);
96    if (f)
97        f->Set((int)def);
98    else
99        m_props.push(new AProperty(name, (int)def));
100}
101
102void APropertyManager::set(char const *name, char const *def)
103{
104    AProperty *f = find(name);
105    if (f)
106        f->Set(def);
107    else
108        m_props.push(new AProperty(name, def));
109}
110
111void APropertyManager::save(char const *filename)
112{
113    FILE *fp = open_FILE(filename, "wb");
114    if (!fp)
115    {
116        dprintf("Error opening %s to save properties\n",filename);
117        return;
118    }
119
120    for (int i = 0; i < m_props.count(); ++i)
121    {
122        fprintf(fp, "%s = ", m_props[i]->m_name.C());
123        if (m_props[i]->m_string.count())
124            fprintf(fp, "\"%s\"\n", m_props[i]->m_string.C());
125        else
126            fprintf(fp, "%d\n", m_props[i]->m_num);
127    }
128    fclose(fp);
129}
130
131/* FIXME: this is putrid. */
132void APropertyManager::load(char const *filename)
133{
134    char buf[100], *c1, *c2, name[100], str[100];
135    FILE *fp = open_FILE(filename, "rb");
136
137    if (!fp)
138        return;
139
140    while (!feof(fp) && fgets(buf, 100, fp))
141    {
142        for (c1=buf,c2=name; *c1 && *c1!='='; c1++,c2++)
143            *c2=*c1;
144        if (*c1==0)
145        {
146            fprintf(stderr,"Missing = for property line %s in file %s\n",buf,filename);
147            exit(1);
148        }
149        *c2 = ' ';
150        while (*c2==' ') { *c2=0; c2--; }
151        c1++;
152        while (*c1==' ')
153            c1++;
154        if (*c1=='"')
155        {
156            c1++;
157            for (c2=str; *c1 && *c1!='"'; c1++,c2++)
158                *c2=*c1;
159            *c2=0;
160            if (*c1!='"')
161            {
162                fprintf(stderr,"Missing \" for property name %s in file %s\n",name,filename);
163                exit(1);
164            }
165            set(name,str);
166        }
167        else
168        {
169            double x;
170            if (sscanf(c1,"%lg",&x))
171                set(name, x);
172            else
173            {
174                fprintf(stderr, "Bad number/string for property name %s in file %s\n",name,filename);
175                exit(1);
176            }
177        }
178    }
179}
180
Note: See TracBrowser for help on using the repository browser.