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 |
|
---|
8 | void Geno::init(const SString& genstring,int genformat,const SString& genname,const SString& comment)
|
---|
9 | {
|
---|
10 | owner=0;
|
---|
11 | f0gen=0;
|
---|
12 | mapinshift=0;
|
---|
13 | mapoutshift=0;
|
---|
14 | isvalid=-1;
|
---|
15 | SString gencopy(genstring);
|
---|
16 | if (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 |
|
---|
68 | gen=gencopy;
|
---|
69 | format=genformat;
|
---|
70 | if (!name.len()) name=genname;
|
---|
71 | if (!txt.len()) txt=comment;
|
---|
72 | multiline=(strchr((const char*)gen,'\n')!=0);
|
---|
73 | // mapoutshift...?
|
---|
74 | }
|
---|
75 |
|
---|
76 | void Geno::freeF0()
|
---|
77 | {
|
---|
78 | if (f0gen) {delete f0gen; f0gen=0;}
|
---|
79 | }
|
---|
80 |
|
---|
81 | Geno::Geno(const char *genstring,int genformat,const char *genname,const char *comment)
|
---|
82 | {
|
---|
83 | init(SString(genstring),genformat,SString(genname),SString(comment));
|
---|
84 | }
|
---|
85 |
|
---|
86 | Geno::Geno(const SString& genstring,int genformat,const SString& genname,const SString& comment)
|
---|
87 | {
|
---|
88 | init(genstring,genformat,genname,comment);
|
---|
89 | }
|
---|
90 |
|
---|
91 | Geno::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 |
|
---|
97 | void Geno::operator=(const Geno& src)
|
---|
98 | {
|
---|
99 | freeF0();
|
---|
100 | gen=src.gen;
|
---|
101 | name=src.name;
|
---|
102 | format=src.format;
|
---|
103 | txt=src.txt;
|
---|
104 | isvalid=src.isvalid;
|
---|
105 | mapinshift=src.mapinshift;
|
---|
106 | mapoutshift=src.mapinshift;
|
---|
107 | multiline=src.multiline;
|
---|
108 | f0gen=src.f0gen?new Geno(*src.f0gen):0;
|
---|
109 | owner=0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | Geno::Geno(const SString& src)
|
---|
113 | {
|
---|
114 | init(src,-1,SString::empty,SString::empty);
|
---|
115 | }
|
---|
116 |
|
---|
117 | void Geno::setGene(const SString& g,int newformat)
|
---|
118 | {
|
---|
119 | gen=g;
|
---|
120 | isvalid=-1;
|
---|
121 | freeF0();
|
---|
122 | if (newformat>=0) format=newformat;
|
---|
123 | }
|
---|
124 |
|
---|
125 | void Geno::setString(const SString& g)
|
---|
126 | {
|
---|
127 | freeF0();
|
---|
128 | init(g,-1,SString::empty,SString::empty);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void Geno::setName(const SString& n)
|
---|
132 | {
|
---|
133 | name=n;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void Geno::setComment(const SString& c)
|
---|
137 | {
|
---|
138 | txt=c;
|
---|
139 | }
|
---|
140 |
|
---|
141 | SString Geno::toString(void)
|
---|
142 | {
|
---|
143 | SString out;
|
---|
144 | int comment=0;
|
---|
145 | if ((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 | }
|
---|
162 | out+=gen;
|
---|
163 | return out;
|
---|
164 | }
|
---|
165 |
|
---|
166 | SString Geno::shortString(void)
|
---|
167 | {
|
---|
168 | SString out;
|
---|
169 | if (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 | }
|
---|
184 | out+=gen;
|
---|
185 | return out;
|
---|
186 | }
|
---|
187 |
|
---|
188 | int Geno::mapGenToString(int genpos) const
|
---|
189 | {
|
---|
190 | if (genpos>gen.len()) return -2;
|
---|
191 | if (genpos<0) return -1;
|
---|
192 | return mapinshift+genpos;
|
---|
193 | }
|
---|
194 |
|
---|
195 | int Geno::mapStringToGen(int stringpos) const
|
---|
196 | {
|
---|
197 | stringpos-=mapinshift;
|
---|
198 | if (stringpos>gen.len()) return -2;
|
---|
199 | if (stringpos<0) return -1;
|
---|
200 | return stringpos;
|
---|
201 | }
|
---|
202 |
|
---|
203 | SString Geno::getGene(void) const {return gen;}
|
---|
204 | SString Geno::getName(void) const {return name;}
|
---|
205 | int Geno::getFormat(void) const {return format;}
|
---|
206 | SString Geno::getComment(void) const {return txt;}
|
---|
207 |
|
---|
208 | void Geno::validate()
|
---|
209 | {
|
---|
210 | if (isvalid>=0) return;
|
---|
211 | if (gen.len()==0) { isvalid=0; return; }
|
---|
212 | if (getFormat()=='0')
|
---|
213 | {
|
---|
214 | Model mod(*this);
|
---|
215 | isvalid=mod.isValid();
|
---|
216 | }
|
---|
217 | else
|
---|
218 | {
|
---|
219 | Geno f0geno=getConverted('0');
|
---|
220 | f0geno.validate();
|
---|
221 | isvalid=f0geno.isvalid;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | int Geno::isValid(void)
|
---|
226 | {
|
---|
227 | if (isvalid<0) validate();
|
---|
228 | return isvalid;
|
---|
229 | }
|
---|
230 |
|
---|
231 | Geno Geno::getConverted(int otherformat,MultiMap *m)
|
---|
232 | {
|
---|
233 | if (otherformat==getFormat()) return *this;
|
---|
234 | #ifndef NO_GENOCONVMANAGER
|
---|
235 | if ((otherformat=='0')&&(!m))
|
---|
236 | {
|
---|
237 | if (!f0gen)
|
---|
238 | f0gen=new Geno(GenoConvManager::globalConvert(*this,otherformat));
|
---|
239 | return *f0gen;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | return GenoConvManager::globalConvert(*this,otherformat,m);
|
---|
243 | #else
|
---|
244 | return (otherformat==getFormat())?*this:Geno(0,0,0,"GenConvManager not available");
|
---|
245 | #endif
|
---|
246 | }
|
---|
247 |
|
---|
248 | Geno::~Geno()
|
---|
249 | {
|
---|
250 | if (f0gen) delete f0gen;
|
---|
251 | }
|
---|