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