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

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

build: SDL2 compilation fixes.

File size: 3.7 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    {
[714]46        if (m_string.Count())
47            m_string.Resize(0);
48        m_num = x;
[2]49    }
50
[714]51    void Set(char const *x)
52    {
53        m_string = x;
54    }
55};
[2]56
[714]57
58APropertyManager::APropertyManager()
[2]59{
60}
61
[714]62APropertyManager::~APropertyManager()
63{
64    for (int i = 0; i < m_props.Count(); ++i)
65        delete m_props[i];
66}
[2]67
[714]68AProperty *APropertyManager::find(char const *name)
[2]69{
[714]70    for (int i = 0; i < m_props.Count(); ++i)
71        if (m_props[i]->m_name == name)
72            return m_props[i];
73    return nullptr;
[2]74}
75
[714]76int APropertyManager::get(char const *name, int def)
[2]77{
[714]78    AProperty *f = find(name);
79    if (!f || f->m_string.Count())
80        return def;
81    return f->m_num;
[2]82}
83
[714]84char const *APropertyManager::get(char const *name, char const *def)
[2]85{
[714]86    AProperty *f = find(name);
87    if (!f || !f->m_string.Count())
88        return def;
89    return f->m_string.C();
[2]90}
91
[714]92void APropertyManager::set(char const *name, double def)
[2]93{
[714]94    AProperty *f = find(name);
95    if (f)
96        f->Set((int)def);
97    else
98        m_props.Push(new AProperty(name, (int)def));
[2]99}
100
[714]101void APropertyManager::set(char const *name, char const *def)
[2]102{
[714]103    AProperty *f = find(name);
104    if (f)
105        f->Set(def);
106    else
107        m_props.Push(new AProperty(name, def));
[2]108}
109
[714]110void APropertyManager::save(char const *filename)
[2]111{
[714]112    FILE *fp = open_FILE(filename, "wb");
113    if (!fp)
[2]114    {
[714]115        dprintf("Error opening %s to save properties\n",filename);
116        return;
[2]117    }
[714]118
119    for (int i = 0; i < m_props.Count(); ++i)
120    {
121        fprintf(fp, "%s = ", m_props[i]->m_name.C());
122        if (m_props[i]->m_string.Count())
123            fprintf(fp, "\"%s\"\n", m_props[i]->m_string.C());
124        else
125            fprintf(fp, "%d\n", m_props[i]->m_num);
126    }
[2]127    fclose(fp);
128}
129
[714]130/* FIXME: this is putrid. */
131void APropertyManager::load(char const *filename)
132{
133    char buf[100], *c1, *c2, name[100], str[100];
134    FILE *fp = open_FILE(filename, "rb");
[2]135
[714]136    if (!fp)
137        return;
138
139    while (!feof(fp) && fgets(buf, 100, fp))
[2]140    {
[714]141        for (c1=buf,c2=name; *c1 && *c1!='='; c1++,c2++)
142            *c2=*c1;
143        if (*c1==0)
144        {
145            fprintf(stderr,"Missing = for property line %s in file %s\n",buf,filename);
146            exit(1);
147        }
148        *c2 = ' ';
149        while (*c2==' ') { *c2=0; c2--; }
150        c1++;
151        while (*c1==' ')
152            c1++;
153        if (*c1=='"')
154        {
155            c1++;
156            for (c2=str; *c1 && *c1!='"'; c1++,c2++)
157                *c2=*c1;
158            *c2=0;
159            if (*c1!='"')
160            {
161                fprintf(stderr,"Missing \" for property name %s in file %s\n",name,filename);
162                exit(1);
163            }
164            set(name,str);
165        }
166        else
167        {
168            double x;
169            if (sscanf(c1,"%lg",&x))
170                set(name, x);
171            else
172            {
173                fprintf(stderr, "Bad number/string for property name %s in file %s\n",name,filename);
174                exit(1);
175            }
176        }
[2]177    }
178}
[714]179
Note: See TracBrowser for help on using the repository browser.