enigma-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Enigma-cvs] enigma/src levelpack.cc,1.9,1.10


From: Ralf Westram <address@hidden>
Subject: [Enigma-cvs] enigma/src levelpack.cc,1.9,1.10
Date: Mon, 27 Oct 2003 11:56:07 +0000

Update of /cvsroot/enigma/enigma/src
In directory subversions:/tmp/cvs-serv13343/src

Modified Files:
        levelpack.cc 
Log Message:
- rewrote LevelPack_Enigma::load_index to load new index format



Index: levelpack.cc
===================================================================
RCS file: /cvsroot/enigma/enigma/src/levelpack.cc,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** levelpack.cc        22 Oct 2003 21:39:22 -0000      1.9
--- levelpack.cc        27 Oct 2003 11:56:05 -0000      1.10
***************
*** 21,25 ****
  #include "game.hh"
  #include "world.hh"
- #include "system.hh"
  #include "px/tools.hh"
  
--- 21,24 ----
***************
*** 64,69 ****
  
          // Load list of levels from "index.txt"
!         void load_index (istream &is);
!         bool load_level (istream &is);
  
      private:
--- 63,68 ----
  
          // Load list of levels from "index.txt"
!         const char *load_index (istream &is);
!         bool        load_level (istream &is);
  
      private:
***************
*** 120,149 ****
  }
  
! void LevelPack_Enigma::load_index (istream &is)
  {
      m_levels.clear();
!     string line;
!     while (getline(is, line)) {
!         vector<string> tokens(4);
!         int ntok = split_copy_n (line, '|', tokens.begin(), tokens.size());
!         if (ntok > 0 && ntok <= 4) {
!             transform(tokens.begin(), tokens.end(), tokens.begin(), 
trim<string>);
!             if (tokens[0].length() > 0) { // ignore lines where name is empty
!                 m_levels.push_back(LevelInfo(tokens[0], tokens[1], tokens[2], 
tokens[3], GAMET_ENIGMA));
              }
          }
      }
  }
  
  void LevelPack_Enigma::reinit()
  {
!     string filename = enigma::FindDataFile(m_initfile);
!     ifstream is(filename.c_str());
  
      if (!is) {
!         fprintf(stderr, "Couldn't load level pack %s.\n", filename.c_str());
          return;
      }
!     load_index (is);
  }
  
--- 119,243 ----
  }
  
! const char *LevelPack_Enigma::load_index (istream &is)
  {
      m_levels.clear();
!     string         line;
!     int            linenumber = 0;
!     const char    *error      = 0;
!     static string  xerror;      // storage for error messages
! 
!     while (!error && getline(is, line)) {
!         ++linenumber;
! 
!         const char *wspace = " \t";
!         size_t      p      = line.find_first_not_of(wspace);
! 
!         if (p != string::npos && line.at(p) == '{') { // found a level 
description
!             string filename, name, author;
!             int    revision  = 1;
!             bool   easymode  = false;
!             int    par_time  = -1;
!             int    par_moves = -1;
!             string hint1, hint2;
! 
!             ++p;
!             while (1) {
!                 string tag;
!                 string content;
! 
!                 // ugly parser code starts here.
!                 // - it parses 'tag = content' or 'tag = "content"'
!                 // - '\"' is allowed in '"content"'
!                 // - whitespace is ignored
!                 // - sets message and breaks while loop in case of error
!                 {
!                     size_t tag_start = line.find_first_not_of(wspace, p);
!                     if (tag_start == string::npos) { error = "Expected tag or 
'}'"; break; }
!                     if (line.at(tag_start) == '}') break; // line done
! 
!                     size_t equal = line.find('=', tag_start);
!                     if (equal == string::npos) { error = "'=' expected"; 
break; }
!                     if (equal == tag_start) { error = "empty tag"; break; }
! 
!                     size_t tag_end = line.find_last_not_of(wspace, equal-1);
!                     tag = line.substr(tag_start, tag_end-tag_start+1);
! 
!                     size_t content_start = line.find_first_not_of(wspace, 
equal+1);
!                     if (line.at(content_start) == '\"') { // content in ""
!                         size_t oquote       = line.find('\"', 
content_start+1);
!                         bool   have_escapes = false;
!                         while (oquote != string::npos && line.at(oquote-1) == 
'\\') { // step over \"
!                             oquote       = line.find('\"', oquote+1);
!                             have_escapes = true;
!                         }
!                         if (oquote == string::npos) { error = "unmatched 
quote"; break; }
!                         content = line.substr(content_start+1, 
oquote-content_start-1);
!                         if (have_escapes) {
!                             size_t esc;
!                             while ((esc = content.find("\\\"")) != 
string::npos)
!                                 content.replace(esc, 2, "\"");
!                         }
!                         p = oquote+1;
!                     }
!                     else { // content w/o ""
!                         size_t content_end = line.find_first_of(" \t}", 
content_start);
!                         if (content_end == string::npos) {
!                             error = "expected space or } behind content";
!                             break;
!                         }
!                         content = line.substr(content_start, 
content_end-content_start);
!                         p       = content_end;
!                     }
!                 }
! 
!                 if      (tag == "file") filename       = content;
!                 else if (tag == "name") name           = content;
!                 else if (tag == "author") author       = content;
!                 else if (tag == "revision") revision   = 
atoi(content.c_str());
!                 else if (tag == "easymode") easymode   = (content == "1");
!                 else if (tag == "par_time") par_time   = 
atoi(content.c_str());
!                 else if (tag == "par_moves") par_moves = 
atoi(content.c_str());
!                 else if (tag == "hint1") hint1         = content;
!                 else if (tag == "hint2") hint2         = content;
!                 else {
!                     xerror = strf("unknown tag '%s'", tag.c_str());
!                     error  = xerror.c_str();
!                 }
!             }
! 
!             if (!error && filename.length() == 0) error = "mandatory tag 
'file' missing";
! 
!             if (!error) {
!                 m_levels.push_back(LevelInfo(GAMET_ENIGMA, filename, name, 
author, revision,
!                                              easymode, par_time, par_moves, 
hint1, hint2));
              }
          }
      }
+ 
+     if (error) {
+         xerror = strf("in line %i: %s", linenumber, error);
+         error  = xerror.c_str();
+     }
+ 
+     return error;
  }
  
  void LevelPack_Enigma::reinit()
  {
!     string      filename = enigma::FindDataFile(m_initfile);
!     ifstream    is(filename.c_str());
!     const char *error    = 0;
  
      if (!is) {
!         error = "cannot open file";
          return;
      }
!     else {
!         error = load_index(is);
!     }
! 
!     if (error) {
!         fprintf(stderr, "Couldn't load level pack '%s' (%s)\n", 
filename.c_str(), error);
!     }
  }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]