From 19bbd5822593c7198dd54eb0f5d38fa1ed8d8fb7 Mon Sep 17 00:00:00 2001 From: Ian C Date: Wed, 18 Aug 2010 14:38:34 +0000 Subject: Reformatted code and altered the attraction rule to something a bit more correct. Also added two tests to prove it works. --- config.cpp | 647 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 349 insertions(+), 298 deletions(-) (limited to 'config.cpp') diff --git a/config.cpp b/config.cpp index ebfd68b..5c74954 100644 --- a/config.cpp +++ b/config.cpp @@ -36,60 +36,66 @@ Config::Config() { int f; - m_error=""; + m_error = ""; for(f=0;f& mass) { static struct - { + { Command cmd; string token; int val; - } enum_list[]= - { - {enable_cmd,"solid",static_cast(solid)}, - {enable_cmd,"lighting",static_cast(lighting)}, - {enable_cmd,"texture",static_cast(texture)}, - {collide_cmd,"none",static_cast(none)}, - {collide_cmd,"merge",static_cast(merge)}, - {collide_cmd,"delta_merge",static_cast(delta_merge)}, - {collide_cmd,"shatter",static_cast(shatter)}, - {plane_cmd,"xy",static_cast(xy_plane)}, - {plane_cmd,"xz",static_cast(xz_plane)}, - {plane_cmd,"yz",static_cast(yz_plane)}, - {cmd_error,"",0} - }; + } enum_list[]= + { + {enable_cmd,"solid",static_cast(solid)}, + {enable_cmd,"lighting",static_cast(lighting)}, + {enable_cmd,"texture",static_cast(texture)}, + {collide_cmd,"none",static_cast(none)}, + {collide_cmd,"merge",static_cast(merge)}, + {collide_cmd,"delta_merge",static_cast(delta_merge)}, + {collide_cmd,"shatter",static_cast(shatter)}, + {plane_cmd,"xy",static_cast(xy_plane)}, + {plane_cmd,"xz",static_cast(xz_plane)}, + {plane_cmd,"yz",static_cast(yz_plane)}, + {cmd_error,"",0} + }; Mass parent; Mass *nm; @@ -128,37 +134,37 @@ bool Config::read(const string& fname,vector& mass) vector::size_type sidx; if (!file) - { + { m_error="File not found : " + fname; return false; - } + } try - { + { while(getCommand(file,cmd,arg)) - { + { switch(cmd) - { + { case enable_cmd: f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==enable_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_flag[static_cast(enum_list[f].val)]=true; break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to enable : " + arg[0]; throw m_error; - } + } break; @@ -166,22 +172,22 @@ bool Config::read(const string& fname,vector& mass) f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==enable_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_flag[static_cast(enum_list[f].val)]=false; break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to disable : " + arg[0]; throw m_error; - } + } break; @@ -189,22 +195,22 @@ bool Config::read(const string& fname,vector& mass) f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==collide_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_collide=static_cast(enum_list[f].val); break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to collide : " + arg[0]; throw m_error; - } + } break; @@ -222,14 +228,16 @@ bool Config::read(const string& fname,vector& mass) toGLfloat(arg[10]), toDouble(arg[11])); if (mass.size()& mass) toDouble(arg[9])); if (mass.size()& mass) int f; for(f=0;f& mass) spd+=spd_i; if (mass.size()& mass) case look_cmd: if (findMass(mass,arg[0],parent)) + { m_look.push_back(arg[0]); + } else - { + { m_error="Unknown object in look : " + arg[0]; throw m_error; - } + } break; case mlook_cmd: { - int num=toInt(arg[1]); - string name=arg[0]; - int f; + int num=toInt(arg[1]); + string name=arg[0]; + int f; - for(f=0;f& mass) case texture_cmd: if (!m_glid) - { + { m_error="texture used without preceeding num_texture"; throw m_error; - } + } if (findMass(mass,arg[0],idx)) - { + { if (m_glid_no==m_glid_idx) - { + { m_error="more textures than expected"; throw m_error; - } + } GLuint id=m_glid[m_glid_idx++]; if (bindTexture(arg[1],toInt(arg[2]),toInt(arg[3]),id)) + { mass[idx].setTexture(id); + } else - { + { m_error="Failed to bind texture : " + m_error; throw m_error; - } } + } else - { + { m_error="Unknown object in texture : " + arg[0]; throw m_error; - } + } break; case num_texture_cmd: if (m_glid) - { + { m_error="num_texture used more than once"; throw m_error; - } + } m_glid_no=toInt(arg[0]); m_glid=new GLuint[m_glid_no]; @@ -392,76 +408,80 @@ bool Config::read(const string& fname,vector& mass) case reuse_cmd: if (findMass(mass,arg[0],idx)) - { + { if (findMass(mass,arg[1],parent)) - { + { GLuint id; if (parent.getTexture(id) || parent.getRingTexture(id)) + { mass[idx].setTexture(id); + } else - { + { m_error="reuse object has no texture : " + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in reuse : " + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in reuse : " + arg[0]; throw m_error; - } + } break; case mreuse_cmd: if (findMass(mass,arg[2],parent)) - { + { int num=toInt(arg[1]); string name=arg[0]; int f; for(f=0;f& mass) toGLfloat(arg[6]), toDouble(arg[7]), toDouble(arg[8])); - } + } else - { + { m_error="Unknown object in ring : " + arg[0]; throw m_error; - } + } break; case mring_cmd: { - int num=toInt(arg[1]); - string name=arg[0]; - int f; + int num=toInt(arg[1]); + string name=arg[0]; + int f; - for(f=0;f& mass) case ring_texture_cmd: if (!m_glid) - { + { m_error="ring used without preceeding num_texture"; throw m_error; - } + } if (findMass(mass,arg[0],idx)) - { + { if (m_glid_no==m_glid_idx) - { + { m_error="more textures than expected"; throw m_error; - } + } GLuint id=m_glid[m_glid_idx++]; if (bindTexture(arg[1],toInt(arg[2]),toInt(arg[3]),id)) mass[idx].setRingTexture(id); else - { + { m_error="Failed to bind texture : " + m_error; throw m_error; - } } + } else - { + { m_error="Unknown object in ring : " + arg[0]; throw m_error; - } + } break; case ring_reuse_cmd: if (findMass(mass,arg[0],idx)) - { + { if (findMass(mass,arg[1],parent)) - { + { GLuint id; if (parent.getRingTexture(id) || - parent.getTexture(id)) + parent.getTexture(id)) mass[idx].setRingTexture(id); else - { + { m_error="ring_reuse object has no texture : " + - arg[1]; + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in ring_reuse : " + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in ring_reuse : " + arg[0]; throw m_error; - } + } break; case ring_mreuse_cmd: if (findMass(mass,arg[2],parent)) - { + { int num=toInt(arg[1]); string name=arg[0]; int f; for(f=0;f& mass) f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==plane_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_plane=static_cast(enum_list[f].val); break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to plane : " + arg[0]; throw m_error; - } + } break; @@ -653,34 +674,36 @@ bool Config::read(const string& fname,vector& mass) found=false; for(sidx=0;sidx& mass) default: break; - } } } + } catch(...) - { + { file.close(); return false; - } + } if (cmd==cmd_eof) - { + { file.close(); return true; - } + } else + { return false; + } } void Config::getCamera(GLfloat& x, GLfloat& y, GLfloat& z, GLfloat& yaw) const @@ -858,15 +883,19 @@ bool Config::CompareNoCase(const string& s1, const string& s2) p2=s2.c_str(); while((*p1)&&(tolower(*p1)==tolower(*p2))) - { + { p1++; p2++; - } + } if (tolower(*p1)-tolower(*p2)) + { return false; + } else + { return true; + } } @@ -880,64 +909,84 @@ Config::GetWordStatus Config::getWord(ifstream& file, string& word, char c; if (term==";") + { not_allowed=","; + } else if (term==",") + { not_allowed=";"; + } else + { not_allowed=",;"; + } word=""; while(!done) - { + { file.get(c); if (file.eof()) + { return GWEof; + } if (in_comment) - { + { if (c=='\n') - in_comment=false; + { + in_comment=false; } + } else - { + { if (c=='#') - in_comment=true; - else - if (in_word) - { - bool at_term=(term.find(c)!=string::npos); - bool in_error=(not_allowed.find(c)!=string::npos); - - if (in_error) - { - if (term==",") - return GWTooFewArgs; - else if (term==";") - return GWTooManyArgs; - else - return GWSyntax; - } + { + in_comment=true; + } + else if (in_word) + { + bool at_term=(term.find(c)!=string::npos); + bool in_error=(not_allowed.find(c)!=string::npos); - if (at_term) - done=true; - else - if (c!='\n') - word+=c; + if (in_error) + { + if (term==",") + { + return GWTooFewArgs; } - else + else if (term==";") { - bool in_ignore=(ignore.find(c)!=string::npos); - - if (!in_ignore) - { - word+=c; - in_word=true; - } + return GWTooManyArgs; } + else + { + return GWSyntax; + } + } + + if (at_term) + { + done=true; + } + else if (c!='\n') + { + word+=c; + } + } + else + { + bool in_ignore=(ignore.find(c)!=string::npos); + + if (!in_ignore) + { + word+=c; + in_word=true; + } } } + } return GWOK; } @@ -947,44 +996,44 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, vector& arg) { static struct - { - string cmd; - Command token; - int args; - } cmd_list[]= - { - {"enable",enable_cmd,1}, - {"disable",disable_cmd,1}, - {"collide",collide_cmd,1}, - {"const",const_cmd,1}, - {"body",body_cmd,12}, - {"radial",radial_cmd,10}, - {"light",light_cmd,4}, - {"camera",camera_cmd,4}, - {"look",look_cmd,1}, - {"texture",texture_cmd,4}, - {"num_texture",num_texture_cmd,1}, - {"reuse",reuse_cmd,2}, - {"particles",particles_cmd,3}, - {"shatter",shatter_cmd,5}, - {"multiple",multiple_cmd,15}, - {"mreuse",mreuse_cmd,3}, - {"plane",plane_cmd,1}, - {"scale",scale_cmd,1}, - {"look_at",look_at_cmd,1}, - {"travel_as",travel_as_cmd,1}, - {"window_size",window_size_cmd,2}, - {"sparks",sparks_cmd,5}, - {"max_mass",max_mass_cmd,1}, - {"mlook",mlook_cmd,2}, - {"ring",ring_cmd,9}, - {"mring",mring_cmd,10}, - {"ring_texture",ring_texture_cmd,4}, - {"ring_reuse",ring_reuse_cmd,2}, - {"ring_mreuse",ring_mreuse_cmd,3}, - {"fog",fog_cmd,5}, - {"",cmd_error,1} - }; + { + string cmd; + Command token; + int args; + } cmd_list[]= + { + {"enable",enable_cmd,1}, + {"disable",disable_cmd,1}, + {"collide",collide_cmd,1}, + {"const",const_cmd,1}, + {"body",body_cmd,12}, + {"radial",radial_cmd,10}, + {"light",light_cmd,4}, + {"camera",camera_cmd,4}, + {"look",look_cmd,1}, + {"texture",texture_cmd,4}, + {"num_texture",num_texture_cmd,1}, + {"reuse",reuse_cmd,2}, + {"particles",particles_cmd,3}, + {"shatter",shatter_cmd,5}, + {"multiple",multiple_cmd,15}, + {"mreuse",mreuse_cmd,3}, + {"plane",plane_cmd,1}, + {"scale",scale_cmd,1}, + {"look_at",look_at_cmd,1}, + {"travel_as",travel_as_cmd,1}, + {"window_size",window_size_cmd,2}, + {"sparks",sparks_cmd,5}, + {"max_mass",max_mass_cmd,1}, + {"mlook",mlook_cmd,2}, + {"ring",ring_cmd,9}, + {"mring",mring_cmd,10}, + {"ring_texture",ring_texture_cmd,4}, + {"ring_reuse",ring_reuse_cmd,2}, + {"ring_mreuse",ring_mreuse_cmd,3}, + {"fog",fog_cmd,5}, + {"",cmd_error,1} + }; string word; string cmd_string; @@ -992,7 +1041,7 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, int f; switch (getWord(file,word," \t\n"," \t\n")) - { + { case GWEof: cmd=cmd_eof; return false; @@ -1001,40 +1050,40 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, return false; default: break; - } + } f=0; cmd=cmd_error; while(cmd_list[f].cmd!="") - { + { if (CompareNoCase(cmd_list[f].cmd,word)) - { + { cmd_string=word; cmd=cmd_list[f].token; expect=cmd_list[f].args; break; - } + } f++; - } + } if (cmd==cmd_error) - { + { m_error="Unrecognised command " + word; - return false; - } + return false; + } arg.clear(); for(f=0;f(strtol(s.c_str(),&p,0)); if (*p) - { + { m_error="Invalid integer : " + s; - throw m_error; - } + throw m_error; + } return i; } @@ -1111,10 +1160,10 @@ size_t Config::toSize(const string& s) i=static_cast(strtol(s.c_str(),&p,0)); if (*p) - { + { m_error="Invalid integer : " + s; - throw m_error; - } + throw m_error; + } return i; } @@ -1123,10 +1172,10 @@ size_t Config::toSize(const string& s) bool Config::toYesNo(const string& s) { if (s!="yes" && s!="no") - { + { m_error="Invalid yes/no : " + s; throw m_error; - } + } return s=="yes"; } @@ -1140,28 +1189,30 @@ bool Config::bindTexture(const string& name, int w, int h, GLuint id) int x,y; if (!(fp=fopen(name.c_str(),"r"))) - { + { m_error="file " + name + " not found"; - return false; - } + return false; + } mem=new GLubyte[w*h*3]; for(y=0;y=0;x--) - { + { + for(x=w-1;x>=0;x--) + { mem[x*3+y*w*3]=fgetc(fp); mem[x*3+y*w*3+1]=fgetc(fp); mem[x*3+y*w*3+2]=fgetc(fp); if (feof(fp)) - { + { fclose(fp); m_error="file " + name + " to short"; delete[] mem; return false; - } } + } + } fclose(fp); @@ -1189,7 +1240,7 @@ void Config::genRadial(Mass **newMass, const Mass& parent, parent.getDelta(pdx,pdy,pdz); switch(m_plane) - { + { case xy_plane: ox=px+dist*sin(ang); oy=py-dist*cos(ang); @@ -1223,7 +1274,7 @@ void Config::genRadial(Mass **newMass, const Mass& parent, default: m_error="INTERNAL ERROR: invalid plane defined"; throw m_error; - } + } dx+=pdx; dy+=pdy; -- cgit v1.2.3