source: cpp/gdk/geno.cpp @ 64

Last change on this file since 64 was 64, checked in by Maciej Komosinski, 13 years ago

a lot of minor fixes

File size: 4.9 KB
Line 
1// This file is a part of the Framsticks GDK library.
2// Copyright (C) 2002-2011  Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include "geno.h"
6#include "model.h"
7
8void Geno::init(const SString& genstring,int genformat,const SString& genname,const SString& comment)
9{
10owner=0;
11f0gen=0;
12mapinshift=0;
13mapoutshift=0;
14isvalid=-1;
15SString gencopy(genstring);
16if (genformat==-1)
17        { // unknown format
18        genformat='1';
19        if (genstring.charAt(0)=='/')
20                {
21                int end;
22                SString newcomment;
23                switch(genstring.charAt(1))
24                        {
25                        case '/':
26                                genformat=genstring.charAt(2);
27                                if ((end=genstring.indexOf('\n'))>=0)
28                                        {
29                                        newcomment=genstring.substr(2,end-2);
30                                        gencopy=genstring.substr(end+1);
31                                        mapinshift=end+1;
32                                        }
33                                else
34                                        {
35                                        gencopy=0;
36                                        mapinshift=genstring.len();
37                                        }
38                                break;
39                        case '*':
40                                genformat=genstring.charAt(2);
41                                if ((end=genstring.indexOf("*/"))>=0)
42                                        {
43                                        newcomment=genstring.substr(2,end-2);
44                                        gencopy=genstring.substr(end+2);
45                                        mapinshift=end+2;
46                                        }
47                                else
48                                        {
49                                        gencopy=0;
50                                        mapinshift=genstring.len();
51                                        }
52                                break;
53                        }
54                if (newcomment.len()>0)
55                        {
56                        SString token; int pos=0;
57                        if (newcomment.getNextToken(pos,token,';'))
58                        if (newcomment.getNextToken(pos,token,';'))
59                                {
60                                if (token.len()) txt=token;
61                        if (newcomment.getNextToken(pos,token,';'))
62                                if (token.len()) name=token;
63                                }
64                        }
65                }
66        }
67
68gen=gencopy;
69format=genformat;
70if (!name.len()) name=genname;
71if (!txt.len()) txt=comment;
72multiline=(strchr((const char*)gen,'\n')!=0);
73// mapoutshift...?
74}
75
76void Geno::freeF0()
77{
78if (f0gen) {delete f0gen; f0gen=0;}
79}
80
81Geno::Geno(const char *genstring,int genformat,const char *genname,const char *comment)
82{
83init(SString(genstring),genformat,SString(genname),SString(comment));
84}
85
86Geno::Geno(const SString& genstring,int genformat,const SString& genname,const SString& comment)
87{
88init(genstring,genformat,genname,comment);
89}
90
91Geno::Geno(const Geno& src)
92        :gen(src.gen),name(src.name),format(src.format),txt(src.txt),isvalid(src.isvalid),
93         mapinshift(src.mapinshift),mapoutshift(src.mapinshift),multiline(src.multiline),
94         f0gen(0),owner(0)
95{f0gen=src.f0gen?new Geno(*src.f0gen):0;}
96
97void Geno::operator=(const Geno& src)
98{
99freeF0();
100gen=src.gen;
101name=src.name;
102format=src.format;
103txt=src.txt;
104isvalid=src.isvalid;
105mapinshift=src.mapinshift;
106mapoutshift=src.mapinshift;
107multiline=src.multiline;
108f0gen=src.f0gen?new Geno(*src.f0gen):0;
109owner=0;
110}
111
112Geno::Geno(const SString& src)
113{
114init(src,-1,SString::empty,SString::empty);
115}
116
117void Geno::setGene(const SString& g,int newformat)
118{
119gen=g;
120isvalid=-1;
121freeF0();
122if (newformat>=0) format=newformat;
123}
124
125void Geno::setString(const SString& g)
126{
127freeF0();
128init(g,-1,SString::empty,SString::empty);
129}
130
131void Geno::setName(const SString& n)
132{
133name=n;
134}
135
136void Geno::setComment(const SString& c)
137{
138txt=c;
139}
140
141SString Geno::toString(void)
142{
143SString out;
144int comment=0;
145if ((format!='1')||(comment=(txt.len()||name.len())))
146        {
147        if (multiline)
148                out+="//";
149        else
150                out+="/*";
151        out+=format;
152        if (comment)
153                {
154                if (txt.len()) {out+=";";out+=txt;}
155                if (name.len()){out+=";";out+=name;}
156                }
157        if (multiline)
158                out+="\n";
159        else
160                out+="*/";
161        }
162out+=gen;
163return out;
164}
165
166SString Geno::shortString(void)
167{
168SString out;
169if (format!='1')
170        {
171        if (multiline)
172                out+="//";
173        else
174                out+="/*";
175        if (format==0)
176                out+="invalid";
177        else
178                out+=format;
179        if (multiline)
180                out+="\n";
181        else
182                out+="*/";
183        }
184out+=gen;
185return out;
186}
187
188int Geno::mapGenToString(int genpos) const
189{
190if (genpos>gen.len()) return -2;
191if (genpos<0) return -1;
192return mapinshift+genpos;
193}
194
195int Geno::mapStringToGen(int stringpos) const
196{
197stringpos-=mapinshift;
198if (stringpos>gen.len()) return -2;
199if (stringpos<0) return -1;
200return stringpos;
201}
202
203SString Geno::getGene(void) const {return gen;}
204SString Geno::getName(void) const {return name;}
205int Geno::getFormat(void) const {return format;}
206SString Geno::getComment(void) const {return txt;}
207
208void Geno::validate()
209{
210if (isvalid>=0) return;
211if (gen.len()==0) { isvalid=0; return; }
212if (getFormat()=='0')
213        {
214        Model mod(*this);
215        isvalid=mod.isValid();
216        }
217else
218        {
219        Geno f0geno=getConverted('0');
220        f0geno.validate();
221        isvalid=f0geno.isvalid;
222        }
223}
224
225int Geno::isValid(void)
226{
227if (isvalid<0) validate();
228return isvalid;
229}
230
231Geno Geno::getConverted(int otherformat,MultiMap *m)
232{
233if (otherformat==getFormat()) return *this;
234#ifndef NO_GENOCONVMANAGER
235if ((otherformat=='0')&&(!m))
236        {
237        if (!f0gen)
238                f0gen=new Geno(GenoConvManager::globalConvert(*this,otherformat));
239        return *f0gen;
240        }
241else
242        return GenoConvManager::globalConvert(*this,otherformat,m);
243#else
244return (otherformat==getFormat())?*this:Geno(0,0,0,"GenConvManager not available");
245#endif
246}
247
248Geno::~Geno()
249{
250if (f0gen) delete f0gen;
251}
Note: See TracBrowser for help on using the repository browser.