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