Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ContentBundle.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2006. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: ContentBundle.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Chained collection of localized strings
13 */
14 
15 #include "MemDebug.h"
16 #include "ContentBundle.h"
17 #include "DataLoader.h"
18 
19 void Print(const char* fmt, ...);
20 
21 // +--------------------------------------------------------------------+
22 
23 ContentBundle::ContentBundle(const char* bundle, Locale* locale)
24 {
25  Text file = FindFile(bundle, locale);
26  if (file.length() > 0) {
27  LoadBundle(file);
28  }
29 }
30 
31 // +--------------------------------------------------------------------+
32 
34 {
35 }
36 
37 // +--------------------------------------------------------------------+
38 
39 Text
40 ContentBundle::GetText(const char* key) const
41 {
42  return values.find(key, Text(key));
43 }
44 
45 // +--------------------------------------------------------------------+
46 
47 Text
48 ContentBundle::FindFile(const char* bundle, Locale* locale)
49 {
50  Text result;
51  Text basename = Text(bundle);
53 
54  if (loader && bundle) {
55  if (locale) {
56  result = basename + locale->GetFullCode() + ".txt";
57 
58  if (loader->FindFile(result))
59  return result;
60 
61  result = basename + "_" + locale->GetLanguage() + ".txt";
62 
63  if (loader->FindFile(result))
64  return result;
65  }
66 
67  result = basename + ".txt";
68 
69  if (loader->FindFile(result))
70  return result;
71  }
72 
73  return Text();
74 }
75 
76 // +--------------------------------------------------------------------+
77 
78 void
79 ContentBundle::LoadBundle(const char* filename)
80 {
82  if (loader && filename && *filename) {
83  BYTE* buffer = 0;
84  loader->LoadBuffer(filename, buffer, true, true);
85  if (buffer && *buffer) {
86  char key[1024];
87  char val[2048];
88  char* p = (char*) buffer;
89  int s = 0, ik = 0, iv = 0;
90 
91  key[0] = 0;
92  val[0] = 0;
93 
94  while (*p) {
95  if (*p == '=') {
96  s = 1;
97  }
98  else if (*p == '\n' || *p == '\r') {
99  if (key[0] && val[0])
100  values.insert(Text(key).trim(), Text(val).trim());
101 
102  ZeroMemory(key, 1024);
103  ZeroMemory(val, 2048);
104  s = 0;
105  ik = 0;
106  iv = 0;
107  }
108  else if (s == 0) {
109  if (!key[0]) {
110  if (*p == '#') {
111  s = -1; // comment
112  }
113  else if (!isspace(*p)) {
114  key[ik++] = *p;
115  }
116  }
117  else {
118  key[ik++] = *p;
119  }
120  }
121  else if (s == 1) {
122  if (!isspace(*p)) {
123  s = 2;
124  val[iv++] = *p;
125  }
126  }
127  else if (s == 2) {
128  val[iv++] = *p;
129  }
130 
131  p++;
132  }
133 
134  loader->ReleaseBuffer(buffer);
135  }
136  }
137 }