xoreos  0.0.5
visfile.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/readstream.h"
27 #include "src/common/util.h"
28 #include "src/common/error.h"
29 #include "src/common/strutil.h"
30 
31 #include "src/aurora/visfile.h"
32 
33 namespace Aurora {
34 
36 }
37 
39 }
40 
42  _map.clear();
43 }
44 
46  clear();
47 
49  tokenizer.addSeparator(' ');
50  tokenizer.addChunkEnd('\n');
51  tokenizer.addIgnore('\r');
52 
53  for (;;) {
54  std::vector<Common::UString> strings;
55  tokenizer.getTokens(vis, strings);
56 
57  // Make sure we don't get any empty lines
58  while (!vis.eos() && strings.empty()) {
59  tokenizer.nextChunk(vis);
60  tokenizer.getTokens(vis, strings);
61  }
62 
63  if (vis.eos())
64  break;
65 
66  if ((strings.size() == 1) && (strings[0] == "[Adjacent]"))
67  // TODO: New in Jade Empire
68  break;
69 
70  if (strings.size() > 2)
71  throw Common::Exception("Malformed VIS file");
72 
73  Common::UString room = strings[0].toLower();
74  std::vector<Common::UString> visibilityArray;
75 
76  int roomCount = 0;
77  if (strings.size() > 1)
78  Common::parseString(strings[1], roomCount);
79 
80  int realRoomCount = 0;
81 
82  visibilityArray.reserve(roomCount);
83  while (!vis.eos()) {
84  size_t lineStart = vis.pos();
85 
86  tokenizer.nextChunk(vis);
87 
88  if (vis.readChar() != ' ') {
89  // Not indented => new room
90 
91  vis.seek(lineStart);
92  break;
93  }
94 
95  tokenizer.getTokens(vis, strings);
96 
97  if (strings.size() != 1) {
98  // More than one token => new room
99 
100  vis.seek(lineStart);
101  break;
102  }
103 
104  visibilityArray.push_back(strings[0]);
105  realRoomCount++;
106  }
107 
108  if (roomCount != realRoomCount)
109  // Thanks, BioWare! -.-
110  warning("Malformed VIS file. Wanted %d rooms, got %d?!?", roomCount, realRoomCount);
111 
112  if (!visibilityArray.empty())
113  _map[room] = visibilityArray;
114  }
115 }
116 
117 static const std::vector<Common::UString> emptyRoom;
118 const std::vector<Common::UString> &VISFile::getVisibilityArray(const Common::UString &room) const {
119  std::map<Common::UString, std::vector<Common::UString> >::const_iterator vRoom = _map.find(room.toLower());
120  if (vRoom == _map.end())
121  return emptyRoom;
122 
123  return vRoom->second;
124 }
125 
126 } // End of namespace Aurora
A class holding an UTF-8 string.
Definition: ustring.h:48
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
virtual bool eos() const =0
Returns true if a read failed because the stream has been reached.
Tokenizes a stream.
void addChunkEnd(uint32 c)
Add a character marking the end of a chunk.
Utility templates and functions for working with strings and streams.
const std::vector< Common::UString > & getVisibilityArray(const Common::UString &room) const
Return the visibility array for a given room.
Definition: visfile.cpp:118
Exception that provides a stack of explanations.
Definition: error.h:36
void load(Common::SeekableReadStream &vis)
Load a VIS file.
Definition: visfile.cpp:45
void clear()
Clear all information.
Definition: visfile.cpp:41
Basic exceptions to throw.
Utility templates and functions.
Handling BioWare&#39;s VISs (Visibility files).
void addIgnore(uint32 c)
Add a character to ignore.
void warning(const char *s,...)
Definition: util.cpp:33
Basic reading stream interfaces.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
size_t getTokens(SeekableReadStream &stream, std::vector< UString > &list, size_t min=0, size_t max=SIZE_MAX, const UString &def="")
Parse tokens out of the stream.
std::map< Common::UString, std::vector< Common::UString > > _map
Definition: visfile.h:73
UString toLower() const
Return a lowercased copy of the string.
Definition: ustring.cpp:481
Parse tokens out of a stream.
void nextChunk(SeekableReadStream &stream)
Skip past end of chunk characters.
Ignore all repeated separators.
uint32 readChar()
Reads the next character from stream and returns it as an unsigned char cast to an uint32...
Definition: readstream.h:108
void addSeparator(uint32 c)
Add a character on where to split tokens.
Interface for a seekable & readable data stream.
Definition: readstream.h:265
static const std::vector< Common::UString > emptyRoom
Definition: visfile.cpp:117
void parseString(const UString &str, T &value, bool allowEmpty)
Parse a string into any POD integer, float/double or bool type.
Definition: strutil.cpp:215