[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 "multiparamload.h" |
---|
| 6 | #include "framsg.h" |
---|
| 7 | #include <ctype.h> |
---|
| 8 | |
---|
| 9 | void MultiParamLoader::init() |
---|
| 10 | { |
---|
| 11 | file=0; ownfile=0; |
---|
| 12 | status=0; |
---|
| 13 | reset(); |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | void MultiParamLoader::reset() |
---|
| 17 | { |
---|
| 18 | status=0; |
---|
| 19 | breakcond=OnError; |
---|
| 20 | emptyparam.setParamTab(empty_paramtab); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | void MultiParamLoader::load() |
---|
| 24 | { |
---|
| 25 | clearstack(); |
---|
| 26 | if (!file) |
---|
| 27 | { |
---|
| 28 | lasterror="can't open file"; |
---|
| 29 | status=OnError; |
---|
| 30 | return; |
---|
| 31 | } |
---|
| 32 | status=Loading; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void MultiParamLoader::abort() |
---|
| 36 | { |
---|
| 37 | if (file && ownfile) |
---|
| 38 | { |
---|
| 39 | fclose(file); |
---|
| 40 | file=0; |
---|
| 41 | } |
---|
| 42 | clearstack(); |
---|
| 43 | status=Finished; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void MultiParamLoader::load(VirtFILE *f) |
---|
| 47 | { |
---|
| 48 | abort(); |
---|
| 49 | ownfile=0; |
---|
| 50 | file=f; |
---|
| 51 | load(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void MultiParamLoader::load(const char* filename) |
---|
| 55 | { |
---|
| 56 | abort(); |
---|
| 57 | ownfile=1; |
---|
| 58 | file=Vfopen(filename,FOPEN_READ); |
---|
| 59 | load(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | static int isEOL(char ch) |
---|
| 63 | { |
---|
| 64 | return (ch=='\n')||(ch=='\r'); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | static char* fgets0(char*t,int d,VirtFILE *f,int &linlen) |
---|
| 68 | { |
---|
| 69 | char *r=fgets(t,d,f); |
---|
| 70 | if (r) |
---|
| 71 | { |
---|
| 72 | int d=strlen(r); |
---|
| 73 | if (d && isEOL(r[d-1])) { d--; if (d && isEOL(r[d-1])) d--; } |
---|
| 74 | linlen=d; |
---|
| 75 | r[d]=0; |
---|
| 76 | } |
---|
| 77 | else |
---|
| 78 | linlen=0; |
---|
| 79 | return r; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | static char* trim(char*t,int &linlen) |
---|
| 83 | { |
---|
| 84 | char *x=t; |
---|
| 85 | while(isspace(*x)) x++; |
---|
| 86 | linlen-=(x-t); |
---|
| 87 | t=x; |
---|
| 88 | if (!linlen) return t; |
---|
| 89 | x=t+linlen-1; |
---|
| 90 | while(isspace(*x)) x--; |
---|
| 91 | x[1]=0; |
---|
| 92 | linlen=x-t+1; |
---|
| 93 | return t; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | int MultiParamLoader::go() |
---|
| 97 | { |
---|
| 98 | char buf[100]; |
---|
| 99 | char *t; |
---|
| 100 | int linlen; |
---|
| 101 | if (status==OnError) return status; |
---|
| 102 | while (!finished()) |
---|
| 103 | { |
---|
| 104 | if ((status==BeforeObject) || ((status==BeforeUnknown)&&lastclass)) |
---|
| 105 | { |
---|
| 106 | lastclass->load(file); |
---|
| 107 | if (maybeBreak(AfterObject)) |
---|
| 108 | break; |
---|
| 109 | continue; |
---|
| 110 | } |
---|
| 111 | else if (status==BeforeUnknown) |
---|
| 112 | { |
---|
| 113 | loadObjectNow(&emptyparam); |
---|
| 114 | continue; |
---|
| 115 | } |
---|
| 116 | t=fgets0(buf,100,file,linlen); |
---|
| 117 | if (!t) |
---|
| 118 | { |
---|
| 119 | if (!returnFromIncluded()) |
---|
| 120 | { |
---|
| 121 | abort(); |
---|
| 122 | break; |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | continue; |
---|
| 126 | } |
---|
| 127 | if (buf[0]=='#') |
---|
| 128 | { |
---|
| 129 | if (!strncmp(buf+1,"include",7)) |
---|
| 130 | { |
---|
| 131 | const char* t=strchr(buf,'\"'),*t2=0; |
---|
| 132 | if (t) t2=strchr(t+1,'\"'); |
---|
| 133 | if (t2) |
---|
| 134 | { |
---|
| 135 | SString filename(t+1,t2-t-1); |
---|
| 136 | include(filename); |
---|
| 137 | } |
---|
| 138 | else |
---|
| 139 | { |
---|
| 140 | const char* thisfilename=file->VgetPath(); |
---|
| 141 | FMprintf("MultiParamLoader","go",FMLV_WARN,"invalid \"%s\"%s%s",buf, |
---|
| 142 | (thisfilename?" in ":""),(thisfilename?thisfilename:"")); |
---|
| 143 | } |
---|
| 144 | continue; |
---|
| 145 | } |
---|
| 146 | else if (maybeBreak(OnComment)) |
---|
| 147 | { |
---|
| 148 | lastcomment=t+1; |
---|
| 149 | break; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | t=trim(buf,linlen); |
---|
| 153 | if ((linlen>1)&&(t[linlen-1]==':')) |
---|
| 154 | { |
---|
| 155 | lastunknown=0; |
---|
| 156 | lastunknown.append(t,linlen-1); |
---|
| 157 | lastclass=0; |
---|
| 158 | FOREACH(ParamInterface*,pi,params) |
---|
| 159 | { |
---|
| 160 | if (!strcmp(pi->getName(),lastunknown)) { lastclass=pi; break; } |
---|
| 161 | } |
---|
| 162 | if (lastclass) |
---|
| 163 | { |
---|
| 164 | if (maybeBreak(BeforeObject)) |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | if (maybeBreak(BeforeUnknown)) |
---|
| 170 | break; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | return status; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | bool MultiParamLoader::alreadyIncluded(const char* filename) |
---|
| 179 | { |
---|
| 180 | int i; |
---|
| 181 | const char* t; |
---|
| 182 | for(i=0;i<filestack.size();i++) |
---|
| 183 | { |
---|
| 184 | t=filestack(i)->VgetPath(); |
---|
| 185 | if (!t) continue; |
---|
| 186 | if (!strcmp(filename,t)) return true; |
---|
| 187 | } |
---|
| 188 | return false; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | void MultiParamLoader::include(SString& filename) |
---|
| 192 | { |
---|
| 193 | const char* thisfilename=file->VgetPath(); |
---|
| 194 | SString newfilename; |
---|
| 195 | const char* t=thisfilename?strrchr(thisfilename,PATHSEPARATORCHAR):0; |
---|
| 196 | |
---|
| 197 | if (thisfilename && t) |
---|
| 198 | { |
---|
| 199 | newfilename.append(thisfilename,t-thisfilename+1); |
---|
| 200 | newfilename+=filename; |
---|
| 201 | } |
---|
| 202 | else |
---|
| 203 | newfilename=filename; |
---|
| 204 | |
---|
| 205 | if (alreadyIncluded(newfilename)) |
---|
| 206 | { |
---|
| 207 | FMprintf("MultiParamLoader","include",FMLV_WARN,"circular reference ignored (\"%s\")", |
---|
| 208 | (const char*)filename); |
---|
| 209 | return; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | VirtFILE *f=Vfopen(newfilename,FOPEN_READ); |
---|
| 213 | if (!f) |
---|
| 214 | { |
---|
| 215 | FMprintf("MultiParamLoader","include",FMLV_WARN,"\"%s\" not found",(const char*)newfilename); |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | filestack+=file; |
---|
| 220 | file=f; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | VirtFILE* MultiParamLoader::popstack() |
---|
| 225 | { |
---|
| 226 | if (!filestack.size()) return 0; |
---|
| 227 | VirtFILE* f=filestack(filestack.size()-1); |
---|
| 228 | filestack.remove(filestack.size()-1); |
---|
| 229 | return f; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void MultiParamLoader::clearstack() |
---|
| 233 | { |
---|
| 234 | VirtFILE *f; |
---|
| 235 | while(f=popstack()) fclose(f); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | bool MultiParamLoader::returnFromIncluded() |
---|
| 239 | { |
---|
| 240 | if (!filestack.size()) return false; |
---|
| 241 | if (file) fclose(file); |
---|
| 242 | file=popstack(); |
---|
| 243 | return true; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | int MultiParamLoader::loadObjectNow(ParamInterface *pi) |
---|
| 247 | { |
---|
| 248 | pi->load(file); |
---|
| 249 | status=AfterObject; |
---|
| 250 | return 0; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | int MultiParamLoader::run() |
---|
| 254 | { |
---|
| 255 | int stat; |
---|
| 256 | breakOn(OnError); |
---|
| 257 | while(stat=go()) |
---|
| 258 | if (stat==OnError) |
---|
| 259 | { |
---|
| 260 | abort(); |
---|
| 261 | return 0; |
---|
| 262 | } |
---|
| 263 | return 1; |
---|
| 264 | } |
---|