[5] | 1 | // This file is a part of Framsticks GDK library. |
---|
| 2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.frams.alife.pl/ for further information. |
---|
| 4 | |
---|
| 5 | #include "nonstd.h" |
---|
| 6 | |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | #include <math.h> |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <string.h> |
---|
| 11 | #include <ctype.h> |
---|
| 12 | #include <time.h> |
---|
| 13 | #include <errno.h> |
---|
| 14 | |
---|
| 15 | #include "genoconv.h" |
---|
| 16 | #include "multimap.h" |
---|
| 17 | |
---|
| 18 | /////////////////////////////////////////////////////////////////////////// |
---|
| 19 | |
---|
| 20 | GenoConvParam::GenoConvParam(GenoConvManager *g):Param(0),gcm(g) |
---|
| 21 | { |
---|
| 22 | updatetab(); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | void GenoConvParam::freetab() |
---|
| 26 | { |
---|
| 27 | if (tab) free(tab); |
---|
| 28 | tab=0; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | const char *GenoConvParam::id(int i) |
---|
| 32 | { |
---|
| 33 | if (i>=gcm->converters.size()) return 0; |
---|
| 34 | static char t[20]; |
---|
| 35 | sprintf(t,"genkonw%d",i); |
---|
| 36 | return t; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void GenoConvParam::updatetab() |
---|
| 40 | { |
---|
| 41 | int i; |
---|
| 42 | GenoConverter *gk; |
---|
| 43 | ParamEntry *pe; |
---|
| 44 | int ile=gcm->converters.size(); |
---|
| 45 | freetab(); |
---|
| 46 | tab=(ParamEntry*)calloc(2+ile,sizeof(ParamEntry)); |
---|
| 47 | tab[0].id="Genetics: Conversions"; |
---|
| 48 | tab[0].group=1; |
---|
| 49 | tab[0].flags=ile; |
---|
| 50 | tab[0].name="gkparam:"; |
---|
| 51 | for (i=0,pe=tab+1;gk=(GenoConverter *)gcm->converters(i);pe++,i++) |
---|
| 52 | { |
---|
| 53 | pe->id="?"; |
---|
| 54 | pe->group=0; |
---|
| 55 | pe->flags=0; |
---|
| 56 | pe->name=gk->name; |
---|
| 57 | pe->type="d 0 1"; |
---|
| 58 | pe->help=gk->info; |
---|
| 59 | } |
---|
| 60 | pe->id=0; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | GenoConvParam::~GenoConvParam() |
---|
| 64 | { |
---|
| 65 | freetab(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void *GenoConvParam::getTarget(int i) |
---|
| 69 | { |
---|
| 70 | GenoConverter *gk=(GenoConverter *)gcm->converters(i); |
---|
| 71 | return &gk->enabled; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | GenoConvManager::GenoConvManager() |
---|
| 75 | :param(this) |
---|
| 76 | { |
---|
| 77 | if (!globalobject) globalobject=this; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | GenoConvManager::~GenoConvManager() |
---|
| 81 | { |
---|
| 82 | GenoConverter *gc; |
---|
| 83 | for (converters.start();gc=(GenoConverter*)converters();) delete gc; |
---|
| 84 | if (globalobject==this) globalobject=0; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | GenoConvManager *GenoConvManager::globalobject=0; |
---|
| 88 | |
---|
| 89 | void GenoConvManager::addConverter(GenoConverter *gc) |
---|
| 90 | { |
---|
| 91 | converters+=gc; |
---|
| 92 | param.updatetab(); |
---|
| 93 | } |
---|
| 94 | void GenoConvManager::removeConverter(GenoConverter *gc) |
---|
| 95 | { |
---|
| 96 | converters-=gc; |
---|
| 97 | param.updatetab(); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /// write path into 'path' |
---|
| 101 | /// return the last path element (return >= path) |
---|
| 102 | /// null -> path not found |
---|
| 103 | /// @param mapavailable will receive 1 if conversion map is supported by all converters in path |
---|
| 104 | /// (can be NULL if you don't need this information) |
---|
| 105 | |
---|
| 106 | char *GenoConvManager::getPath(char in,char out,char *path,int maxlen,int *mapavailable) |
---|
| 107 | { |
---|
| 108 | if (!maxlen) return 0; |
---|
| 109 | GenoConverter *gk; |
---|
| 110 | int i=0; |
---|
| 111 | for (;gk=(GenoConverter*)converters(i);i++) |
---|
| 112 | { |
---|
| 113 | if ((gk->enabled)&&(gk->in_format == in)) |
---|
| 114 | { |
---|
| 115 | *path=i; |
---|
| 116 | if (gk->out_format == out) |
---|
| 117 | { |
---|
| 118 | if (mapavailable) |
---|
| 119 | *mapavailable=gk->mapsupport; |
---|
| 120 | return path; |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | int mapavail; |
---|
| 125 | char *ret=getPath(gk->out_format,out,path+1,maxlen-1,&mapavail); |
---|
| 126 | if (ret) |
---|
| 127 | { |
---|
| 128 | if (mapavailable) |
---|
| 129 | *mapavailable=gk->mapsupport && mapavail; |
---|
| 130 | return ret; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | return 0; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | char *GenoConvManager::getFormatPath(char in,char out,char *path,int maxlen,int *mapavailable) |
---|
| 139 | { |
---|
| 140 | char *ret=getPath(in,out,path,maxlen,mapavailable); |
---|
| 141 | if (ret) |
---|
| 142 | { |
---|
| 143 | for (char*t=path;t<=ret;t++) |
---|
| 144 | *t=((GenoConverter*)converters(*t))->out_format; |
---|
| 145 | } |
---|
| 146 | return ret; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | Geno GenoConvManager::convert(Geno &in,char format,MultiMap *map) |
---|
| 150 | { |
---|
| 151 | if (in.getFormat()==format) return in; |
---|
| 152 | static char path[10]; |
---|
| 153 | int dep; |
---|
| 154 | char *ret; |
---|
| 155 | if (in.isInvalid()) { return Geno("",0,"","invalid genotype cannot be converted"); } |
---|
| 156 | int mapavail; |
---|
| 157 | for (dep=1;dep<sizeof(path);dep++) // sogenannte iteracyjne poglebianie... |
---|
| 158 | if (ret=getPath(in.getFormat(),format,path,dep,&mapavail)) break; |
---|
| 159 | if (!ret) { return Geno("",0,"","converter not found"); } |
---|
| 160 | if (!map) mapavail=0; |
---|
| 161 | char *t=path; |
---|
| 162 | SString tmp; |
---|
| 163 | tmp=in.getGene(); |
---|
| 164 | MultiMap lastmap,tmpmap; |
---|
| 165 | int firstmap=1; |
---|
| 166 | for (;t<=ret;t++) |
---|
| 167 | { |
---|
| 168 | GenoConverter *gk=(GenoConverter*)converters(*t); |
---|
| 169 | tmp=gk->convert(tmp,mapavail?&tmpmap:0); |
---|
| 170 | if (!tmp.len()) |
---|
| 171 | { |
---|
| 172 | char t[100]; |
---|
| 173 | sprintf(t,"f%c->f%c conversion failed (%s)",gk->in_format,gk->out_format,gk->name); |
---|
| 174 | return Geno(0,0,0,t); |
---|
| 175 | } |
---|
| 176 | if (mapavail) |
---|
| 177 | { |
---|
| 178 | if (firstmap) |
---|
| 179 | { |
---|
| 180 | lastmap=tmpmap; |
---|
| 181 | firstmap=0; |
---|
| 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | MultiMap m; |
---|
| 186 | m.addCombined(lastmap,tmpmap); |
---|
| 187 | lastmap=m; |
---|
| 188 | } |
---|
| 189 | tmpmap.clear(); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | if (map) |
---|
| 193 | *map=lastmap; |
---|
| 194 | return Geno(tmp, format, in.getName(), in.getComment()); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | Geno GenoConvManager::globalConvert(Geno &in,char format,MultiMap *map) |
---|
| 198 | { |
---|
| 199 | if (globalobject) return globalobject->convert(in,format,map); |
---|
| 200 | if (format==in.getFormat()) return in; |
---|
| 201 | return Geno(0,0,0,"GenConvManager not initialized"); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | ///////////////////////////////// |
---|