OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
esmcommon.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_ESM_COMMON_H
2 #define OPENMW_ESM_COMMON_H
3 
4 #include <string>
5 #include <cstring>
6 
7 #include <stdint.h>
8 #include <string.h>
9 
10 namespace ESM
11 {
12 enum Version
13  {
14  VER_12 = 0x3f99999a,
15  VER_13 = 0x3fa66666
16  };
17 
18 
19 // CRTP for FIXED_STRING class, a structure used for holding fixed-length strings
20 template< template<size_t> class DERIVED, size_t SIZE>
22 {
23  /* The following methods must be implemented in derived classes:
24  * char const* ro_data() const; // return pointer to ro buffer
25  * char* rw_data(); // return pointer to rw buffer
26  */
27 public:
28  enum { size = SIZE };
29 
30  template<size_t OTHER_SIZE>
31  bool operator==(char const (&str)[OTHER_SIZE]) const
32  {
33  size_t other_len = strnlen(str, OTHER_SIZE);
34  if (other_len != this->length())
35  return false;
36  return std::strncmp(self()->ro_data(), str, size) == 0;
37  }
38  bool operator==(const char* const str) const
39  {
40  char const* const data = self()->ro_data();
41  for(size_t i = 0; i < size; ++i)
42  {
43  if(data[i] != str[i]) return false;
44  else if(data[i] == '\0') return true;
45  }
46  return str[size] == '\0';
47  }
48  bool operator!=(const char* const str) const { return !( (*this) == str ); }
49 
50  bool operator==(const std::string& str) const
51  {
52  return (*this) == str.c_str();
53  }
54  bool operator!=(const std::string& str) const { return !( (*this) == str ); }
55 
56  size_t data_size() const { return size; }
57  size_t length() const { return strnlen(self()->ro_data(), size); }
58  std::string toString() const { return std::string(self()->ro_data(), this->length()); }
59 
60  void assign(const std::string& value) { std::strncpy(self()->rw_data(), value.c_str(), size); }
61  void clear() { this->assign(""); }
62 private:
63  DERIVED<size> const* self() const
64  {
65  return static_cast<DERIVED<size> const*>(this);
66  }
67 
68  // write the non-const version in terms of the const version
69  // Effective C++ 3rd ed., Item 3 (p. 24-25)
70  DERIVED<size>* self()
71  {
72  return const_cast<DERIVED<size>*>(static_cast<FIXED_STRING_BASE const*>(this)->self());
73  }
74 };
75 
76 // Generic implementation
77 template <size_t SIZE>
78 struct FIXED_STRING : public FIXED_STRING_BASE<FIXED_STRING, SIZE>
79 {
80  char data[SIZE];
81 
82  char const* ro_data() const { return data; }
83  char* rw_data() { return data; }
84 };
85 
86 // In the case of SIZE=4, it can be more efficient to match the string
87 // as a 32 bit number, therefore the struct is implemented as a union with an int.
88 template <>
89 struct FIXED_STRING<4> : public FIXED_STRING_BASE<FIXED_STRING, 4>
90 {
91  union {
92  char data[4];
93  uint32_t intval;
94  };
95 
96  using FIXED_STRING_BASE::operator==;
97  using FIXED_STRING_BASE::operator!=;
98 
99  bool operator==(uint32_t v) const { return v == intval; }
100  bool operator!=(uint32_t v) const { return v != intval; }
101 
102  char const* ro_data() const { return data; }
103  char* rw_data() { return data; }
104 };
105 
110 
111 /* This struct defines a file 'context' which can be saved and later
112  restored by an ESMReader instance. It will save the position within
113  a file, and when restored will let you read from that position as
114  if you never left it.
115  */
117 {
118  std::string filename;
119  uint32_t leftRec, leftSub;
120  size_t leftFile;
122  // When working with multiple esX files, we will generate lists of all files that
123  // actually contribute to a specific cell. Therefore, we need to store the index
124  // of the file belonging to this contest. See CellStore::(list/load)refs for details.
125  int index;
126 
127  // True if subName has been read but not used.
128  bool subCached;
129 
130  // File position. Only used for stored contexts, not regularly
131  // updated within the reader itself.
132  size_t filePos;
133 };
134 
135 }
136 
137 #endif
FIXED_STRING< 256 > NAME256
Definition: esmcommon.hpp:109
bool operator!=(const std::string &str) const
Definition: esmcommon.hpp:54
FIXED_STRING< 4 > NAME
Definition: esmcommon.hpp:106
Definition: esmcommon.hpp:89
bool operator!=(const char *const str) const
Definition: esmcommon.hpp:48
FIXED_STRING< 32 > NAME32
Definition: esmcommon.hpp:107
Definition: esmcommon.hpp:78
Version
Definition: esmcommon.hpp:12
uint32_t leftRec
Definition: esmcommon.hpp:119
size_t length() const
Definition: esmcommon.hpp:57
FIXED_STRING< 64 > NAME64
Definition: esmcommon.hpp:108
bool operator!=(uint32_t v) const
Definition: esmcommon.hpp:100
char data[SIZE]
Definition: esmcommon.hpp:80
NAME recName
Definition: esmcommon.hpp:121
char * rw_data()
Definition: esmcommon.hpp:103
bool operator==(const char *const str) const
Definition: esmcommon.hpp:38
Definition: esmcommon.hpp:21
std::string filename
Definition: esmcommon.hpp:118
size_t data_size() const
Definition: esmcommon.hpp:56
char const * ro_data() const
Definition: esmcommon.hpp:82
bool subCached
Definition: esmcommon.hpp:128
std::string toString() const
Definition: esmcommon.hpp:58
Definition: esmcommon.hpp:116
Definition: esmcommon.hpp:15
NAME subName
Definition: esmcommon.hpp:121
int index
Definition: esmcommon.hpp:125
Definition: esmcommon.hpp:28
uint32_t leftSub
Definition: esmcommon.hpp:119
size_t leftFile
Definition: esmcommon.hpp:120
char const * ro_data() const
Definition: esmcommon.hpp:102
Definition: esmcommon.hpp:14
bool operator==(uint32_t v) const
Definition: esmcommon.hpp:99
uint32_t intval
Definition: esmcommon.hpp:93
size_t filePos
Definition: esmcommon.hpp:132
void assign(const std::string &value)
Definition: esmcommon.hpp:60
bool operator==(char const (&str)[OTHER_SIZE]) const
Definition: esmcommon.hpp:31
char * rw_data()
Definition: esmcommon.hpp:83
void clear()
Definition: esmcommon.hpp:61
bool operator==(const std::string &str) const
Definition: esmcommon.hpp:50