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

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

build: sync with newer Lol Engine changes.

File size: 3.8 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[724]4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[732]11#if HAVE_CONFIG_H
[555]12#   include "config.h"
13#endif
[56]14
[39]15#include <stdio.h>
16#include <string.h>
17
[512]18#include "common.h"
19
[724]20#include "imlib/dprint.h"
21
[481]22#include "property.h"
23#include "game.h"
[2]24
[714]25class AProperty
[2]26{
[714]27public:
28    String m_name;
29    String m_string;
30    int m_num;
[2]31
[714]32    AProperty(char const *name, int def)
33      : m_name(name),
34        m_num(def)
35    {
36    }
[2]37
[714]38    AProperty(char const *name, char const *def)
39      : m_name(name),
40        m_string(def)
[2]41    {
42    }
43
[714]44    void Set(int x)
[124]45    {
[737]46        // FIXME: would empty() work here?
47        if (m_string.count())
48            m_string.resize(0);
[714]49        m_num = x;
[2]50    }
51
[714]52    void Set(char const *x)
53    {
54        m_string = x;
55    }
56};
[2]57
[714]58
59APropertyManager::APropertyManager()
[2]60{
61}
62
[714]63APropertyManager::~APropertyManager()
64{
[737]65    for (int i = 0; i < m_props.count(); ++i)
[714]66        delete m_props[i];
67}
[2]68
[714]69AProperty *APropertyManager::find(char const *name)
[2]70{
[737]71    for (int i = 0; i < m_props.count(); ++i)
[714]72        if (m_props[i]->m_name == name)
73            return m_props[i];
74    return nullptr;
[2]75}
76
[714]77int APropertyManager::get(char const *name, int def)
[2]78{
[714]79    AProperty *f = find(name);
[737]80    if (!f || f->m_string.count())
[714]81        return def;
82    return f->m_num;
[2]83}
84
[714]85char const *APropertyManager::get(char const *name, char const *def)
[2]86{
[714]87    AProperty *f = find(name);
[737]88    if (!f || !f->m_string.count())
[714]89        return def;
90    return f->m_string.C();
[2]91}
92
[714]93void APropertyManager::set(char const *name, double def)
[2]94{
[714]95    AProperty *f = find(name);
96    if (f)
97        f->Set((int)def);
98    else
[737]99        m_props.push(new AProperty(name, (int)def));
[2]100}
101
[714]102void APropertyManager::set(char const *name, char const *def)
[2]103{
[714]104    AProperty *f = find(name);
105    if (f)
106        f->Set(def);
107    else
[737]108        m_props.push(new AProperty(name, def));
[2]109}
110
[714]111void APropertyManager::save(char const *filename)
[2]112{
[714]113    FILE *fp = open_FILE(filename, "wb");
114    if (!fp)
[2]115    {
[714]116        dprintf("Error opening %s to save properties\n",filename);
117        return;
[2]118    }
[714]119
[737]120    for (int i = 0; i < m_props.count(); ++i)
[714]121    {
122        fprintf(fp, "%s = ", m_props[i]->m_name.C());
[737]123        if (m_props[i]->m_string.count())
[714]124            fprintf(fp, "\"%s\"\n", m_props[i]->m_string.C());
125        else
126            fprintf(fp, "%d\n", m_props[i]->m_num);
127    }
[2]128    fclose(fp);
129}
130
[714]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");
[2]136
[714]137    if (!fp)
138        return;
139
140    while (!feof(fp) && fgets(buf, 100, fp))
[2]141    {
[714]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        }
[2]178    }
179}
[714]180
Note: See TracBrowser for help on using the repository browser.