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