Debian Codegraph @

linux/lnxdata.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/linux/lnxdata.cpp $
21 * $Revision: 1.3 $
22 * $Date: 2004/03/21 17:11:39 $
23 * $Author: kevinb $
24 *
25 * Linux database routines
26 *
27 * $Log: lnxdata.cpp,v $
28 * Revision 1.3 2004/03/21 17:11:39 kevinb
29 * Fixes so linux will compile again. Tested with gcc-2.96
30 *
31 * Revision 1.2 2000/04/28 20:19:05 icculus
32 * Writes "registry" to prefpath instead of current directory.
33 *
34 * Revision 1.1.1.1 2000/04/18 00:00:39 icculus
35 * initial checkin
36 *
37 *
38 * 9 7/14/99 9:09p Jeff
39 * added comment header
40 *
41 * $NoKeywords: $
42 */
43
44 #include <string.h>
45 #include <stdio.h>
46 #include <sys/types.h>
47
48 #if defined(POSIX)
49 #include <unistd.h>
50 #include <pwd.h>
51 #else
52 #include <windows.h>
53 #include <Lmcons.h>
54 #endif
55
56 #include <SDL.h>
57
58 #include "appdatabase.h"
59 #include "linux/lnxdatabase.h"
60 #include "pserror.h"
61 #include "mono.h"
62 #include "pserror.h"
63 #include "registry.h"
64
65 #define REGISTRY_FILENAME ".Descent3Registry"
66
67 // Construction and destruction.
68
69 oeLnxAppDatabase::oeLnxAppDatabase() {
70 // Open up the database file, for reading, read in all data and keep it in memory
71 // then close the database
72
73 char const* prefPath = SDL_GetPrefPath("Outrage Entertainment", "Descent 3");
74 if (prefPath == NULL) {
75 fprintf(stderr, "ERROR: Couldn't find preference directory!\n");
76 exit(43);
77 }
78
79 const size_t fileLen = strlen(prefPath) + strlen(REGISTRY_FILENAME) + 2;
80 char* fileName = new char[fileLen];
81 snprintf(fileName, fileLen, "%s/%s", prefPath, REGISTRY_FILENAME);
82
83 database = new CRegistry(fileName);
84 database->Import();
85 create_record("Version");
86 delete [] fileName;
87 }
88
89 oeLnxAppDatabase::oeLnxAppDatabase(oeLnxAppDatabase *parent) {
90 char name[256];
91 CRegistry *db = parent->GetSystemRegistry();
92 db->Export();
93 database = new CRegistry("");
94 db->GetSystemName(name);
95 database->SetSystemName(name);
96 database->Import();
97 }
98
99 oeLnxAppDatabase::~oeLnxAppDatabase() {
100 if (database) {
101 database->Export();
102 delete database;
103 return;
104 }
105
106 mprintf(0, "Can't Export Database Since It's Not There!\n");
107 }
108
109 CRegistry *oeLnxAppDatabase::GetSystemRegistry() { return database; }
110
111 // Record functions
112 // these are actual folders of information
113
114 // creates an empty classification or structure where you can store information
115 bool oeLnxAppDatabase::create_record(const char *pathname) {
116 ASSERT(pathname != NULL);
117 if (database) {
118 database->CreateKey((char *)pathname);
119 return true;
120 }
121 mprintf(0, "Can't CreateKey because database NULL\n");
122 return false;
123 }
124
125 // set current database focus to a particular record
126 bool oeLnxAppDatabase::lookup_record(const char *pathname) {
127 ASSERT(pathname);
128 if (database) {
129 return database->LookupKey((char *)pathname);
130 }
131 mprintf(0, "Can't lookup key because database NULL\n");
132 return false;
133 }
134
135 // read either a string from the current record
136 bool oeLnxAppDatabase::read(const char *label, char *entry, int *entrylen) {
137 ASSERT(label);
138 ASSERT(entry);
139 ASSERT(entrylen);
140 if (!database) {
141 mprintf(0, "Can't read record because database NULL\n");
142 return false;
143 }
144
145 // See if it exists
146 int size = database->GetDataSize((char *)label);
147 if (size > 0)
148 *entrylen = size - 1; //-1 because of NULL
149 else
150 return false;
151
152 // ok it exists, no look it up
153 database->LookupRecord((char *)label, entry);
154 return true;
155 }
156
157 // read a variable-sized integer from the current record
158 bool oeLnxAppDatabase::read(const char *label, void *entry, int wordsize) {
159 ASSERT(label);
160 ASSERT(entry);
161 if (!database) {
162 mprintf(0, "Can't read record because Database NULL\n");
163 return false;
164 }
165
166 int size = database->GetDataSize((char *)label);
167 if (size == 0)
168 return false;
169
170 // ok so it does exist
171 int data;
172 database->LookupRecord((char *)label, &data);
173
174 switch (wordsize) {
175 case 1:
176 *((uint8_t *)entry) = (uint8_t)data;
177 break;
178 case 2:
179 *((uint16_t *)entry) = (uint16_t)data;
180 break;
181 case 4:
182 *((uint32_t *)entry) = (uint32_t)data;
183 break;
184 default:
185 mprintf(0, "Unable to read key %s, unsupported size", label);
186 return false;
187 break;
188 }
189 return true;
190 }
191
192 bool oeLnxAppDatabase::read(const char *label, bool *entry) {
193 bool data;
194 if (!read(label, &data, sizeof(bool)))
195 return false;
196
197 *entry = (data != 0) ? true : false;
198 return true;
199 }
200
201 // write either an integer or string to a record.
202 bool oeLnxAppDatabase::write(const char *label, const char *entry, int entrylen) {
203 ASSERT(label);
204 ASSERT(entry);
205 if (!database) {
206 mprintf(0, "Can't write record because database NULL\n");
207 return false;
208 }
209
210 return database->CreateRecord((char *)label, REGT_STRING, (void *)entry);
211 }
212
213 bool oeLnxAppDatabase::write(const char *label, int entry) {
214 ASSERT(label);
215 if (!database) {
216 mprintf(0, "Can't write record because database NULL\n");
217 return false;
218 }
219 return database->CreateRecord((char *)label, REGT_DWORD, &entry);
220 }
221
222 // get the current user's name from the os
223 void oeLnxAppDatabase::get_user_name(char *buffer, size_t *size) {
224 #if defined(POSIX)
225 struct passwd *pwuid = getpwuid(geteuid());
226
227 if ((pwuid != NULL) && (pwuid->pw_name != NULL)) {
228 strncpy(buffer, pwuid->pw_name, (*size) - 1);
229 buffer[(*size) - 1] = '\0';
230 *size = strlen(buffer);
231 } else {
232 strncpy(buffer, "Unknown", (*size) - 1);
233 buffer[(*size) - 1] = '\0';
234 *size = strlen(buffer);
235 }
236 #else
237 DWORD unamelen = 0;
238 GetUserName(buffer, &unamelen);
239 *size = static_cast<size_t>(unamelen);
240 #endif
241 }