| 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(POSIXcxx . . $ `<built-in>:477:9`!) |
| 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"cxx . . $ `<file>/lib/appdatabase.h`/ |
| 59 | #include "linux/lnxdatabase.h"cxx . . $ `<file>/lib/linux/lnxdatabase.h`/ |
| 60 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 61 | #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/ |
| 62 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 63 | #include "registry.h"cxx . . $ `<file>/linux/registry.h`/ |
| 64 | |
| 65 | #define REGISTRY_FILENAMEcxx . . $ `linux/lnxdata.cpp:65:9`! ".Descent3Registry" |
| 66 | |
| 67 | // Construction and destruction. |
| 68 | |
| 69 | oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#oeLnxAppDatabase(49f6e7a06ebc5aa8).() { |
| 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 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 75 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: Couldn't find preference directory!\n"); |
| 76 | exitcxx . . $ exit(44ed87f96534fad7).(43); |
| 77 | } |
| 78 | |
| 79 | const size_t fileLen = strlen(prefPath) + strlen(REGISTRY_FILENAMEcxx . . $ `linux/lnxdata.cpp:65:9`!) + 2; |
| 80 | char* fileName = new char[fileLen]; |
| 81 | snprintf(fileName, fileLen, "%s/%s", prefPath, REGISTRY_FILENAMEcxx . . $ `linux/lnxdata.cpp:65:9`!); |
| 82 | |
| 83 | databasecxx . . $ oeLnxAppDatabase#database. = new CRegistrycxx . . $ CRegistry#CRegistry(85c52e162fed56f9).(fileName); |
| 84 | databasecxx . . $ oeLnxAppDatabase#database.->Importcxx . . $ CRegistry#Import(c9857a73d68e59ba).(); |
| 85 | create_recordcxx . . $ oeLnxAppDatabase#create_record(c5f7da30cbc0f4e9).("Version"); |
| 86 | delete [] fileName; |
| 87 | } |
| 88 | |
| 89 | oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#oeLnxAppDatabase(7e3a78ff70dc88ae).(oeLnxAppDatabasecxx . . $ oeLnxAppDatabase# *parent) { |
| 90 | char name[256]; |
| 91 | CRegistrycxx . . $ CRegistry# *db = parent->GetSystemRegistrycxx . . $ oeLnxAppDatabase#GetSystemRegistry(50e13c8b5aaced6c).(); |
| 92 | db->Exportcxx . . $ CRegistry#Export(49f6e7a06ebc5aa8).(); |
| 93 | databasecxx . . $ oeLnxAppDatabase#database. = new CRegistrycxx . . $ CRegistry#CRegistry(85c52e162fed56f9).(""); |
| 94 | db->GetSystemNamecxx . . $ CRegistry#GetSystemName(fda57c8427c60377).(name); |
| 95 | databasecxx . . $ oeLnxAppDatabase#database.->SetSystemNamecxx . . $ CRegistry#SetSystemName(85c52e162fed56f9).(name); |
| 96 | databasecxx . . $ oeLnxAppDatabase#database.->Importcxx . . $ CRegistry#Import(c9857a73d68e59ba).(); |
| 97 | } |
| 98 | |
| 99 | oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::~cxx . . $ oeLnxAppDatabase#`~oeLnxAppDatabase`(ced63f7c635d850d).oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#() { |
| 100 | if (databasecxx . . $ oeLnxAppDatabase#database.) { |
| 101 | databasecxx . . $ oeLnxAppDatabase#database.->Exportcxx . . $ CRegistry#Export(49f6e7a06ebc5aa8).(); |
| 102 | delete databasecxx . . $ oeLnxAppDatabase#database.; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Can't Export Database Since It's Not There!\n"); |
| 107 | } |
| 108 | |
| 109 | CRegistrycxx . . $ CRegistry# *oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::GetSystemRegistrycxx . . $ oeLnxAppDatabase#GetSystemRegistry(50e13c8b5aaced6c).() { return databasecxx . . $ oeLnxAppDatabase#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 oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::create_recordcxx . . $ oeLnxAppDatabase#create_record(c5f7da30cbc0f4e9).(const char *pathname) { |
| 116 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(pathname != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 117 | if (databasecxx . . $ oeLnxAppDatabase#database.) { |
| 118 | databasecxx . . $ oeLnxAppDatabase#database.->CreateKeycxx . . $ CRegistry#CreateKey(85c52e162fed56f9).((char *)pathname); |
| 119 | return true; |
| 120 | } |
| 121 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Can't CreateKey because database NULL\n"); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | // set current database focus to a particular record |
| 126 | bool oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::lookup_recordcxx . . $ oeLnxAppDatabase#lookup_record(c5f7da30cbc0f4e9).(const char *pathname) { |
| 127 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(pathname); |
| 128 | if (databasecxx . . $ oeLnxAppDatabase#database.) { |
| 129 | return databasecxx . . $ oeLnxAppDatabase#database.->LookupKeycxx . . $ CRegistry#LookupKey(c5f7da30cbc0f4e9).((char *)pathname); |
| 130 | } |
| 131 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(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 oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::readcxx . . $ oeLnxAppDatabase#read(3bcbb53201fcdcb8).(const char *label, char *entry, int *entrylen) { |
| 137 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(label); |
| 138 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(entry); |
| 139 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(entrylen); |
| 140 | if (!databasecxx . . $ oeLnxAppDatabase#database.) { |
| 141 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Can't read record because database NULL\n"); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // See if it exists |
| 146 | int size = databasecxx . . $ oeLnxAppDatabase#database.->GetDataSizecxx . . $ CRegistry#GetDataSize(5c4306d8553b3696).((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 | databasecxx . . $ oeLnxAppDatabase#database.->LookupRecordcxx . . $ CRegistry#LookupRecord(f758117682a82874).((char *)label, entry); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | // read a variable-sized integer from the current record |
| 158 | bool oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::readcxx . . $ oeLnxAppDatabase#read(340cb0a925eda13d).(const char *label, void *entry, int wordsize) { |
| 159 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(label); |
| 160 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(entry); |
| 161 | if (!databasecxx . . $ oeLnxAppDatabase#database.) { |
| 162 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Can't read record because Database NULL\n"); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | int size = databasecxx . . $ oeLnxAppDatabase#database.->GetDataSizecxx . . $ CRegistry#GetDataSize(5c4306d8553b3696).((char *)label); |
| 167 | if (size == 0) |
| 168 | return false; |
| 169 | |
| 170 | // ok so it does exist |
| 171 | int data; |
| 172 | databasecxx . . $ oeLnxAppDatabase#database.->LookupRecordcxx . . $ CRegistry#LookupRecord(f758117682a82874).((char *)label, &data); |
| 173 | |
| 174 | switch (wordsize) { |
| 175 | case 1: |
| 176 | *((uint8_tcxx . . $ uint8_t# *)entry) = (uint8_tcxx . . $ uint8_t#)data; |
| 177 | break; |
| 178 | case 2: |
| 179 | *((uint16_tcxx . . $ uint16_t# *)entry) = (uint16_tcxx . . $ uint16_t#)data; |
| 180 | break; |
| 181 | case 4: |
| 182 | *((uint32_tcxx . . $ uint32_t# *)entry) = (uint32_tcxx . . $ uint32_t#)data; |
| 183 | break; |
| 184 | default: |
| 185 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Unable to read key %s, unsupported size", label); |
| 186 | return false; |
| 187 | break; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | bool oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::readcxx . . $ oeLnxAppDatabase#read(c1ce79fc3dc965f1).(const char *label, bool *entry) { |
| 193 | bool data; |
| 194 | if (!readcxx . . $ oeLnxAppDatabase#read(340cb0a925eda13d).(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 oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::writecxx . . $ oeLnxAppDatabase#write(f827cee075336303).(const char *label, const char *entry, int entrylen) { |
| 203 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(label); |
| 204 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(entry); |
| 205 | if (!databasecxx . . $ oeLnxAppDatabase#database.) { |
| 206 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Can't write record because database NULL\n"); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return databasecxx . . $ oeLnxAppDatabase#database.->CreateRecordcxx . . $ CRegistry#CreateRecord(5438c15cf96d2f9c).((char *)label, REGT_STRINGcxx . . $ `linux/registry.h:49:9`!, (void *)entry); |
| 211 | } |
| 212 | |
| 213 | bool oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::writecxx . . $ oeLnxAppDatabase#write(2551bed5d6fc04f6).(const char *label, int entry) { |
| 214 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(label); |
| 215 | if (!databasecxx . . $ oeLnxAppDatabase#database.) { |
| 216 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Can't write record because database NULL\n"); |
| 217 | return false; |
| 218 | } |
| 219 | return databasecxx . . $ oeLnxAppDatabase#database.->CreateRecordcxx . . $ CRegistry#CreateRecord(5438c15cf96d2f9c).((char *)label, REGT_DWORDcxx . . $ `linux/registry.h:50:9`!, &entry); |
| 220 | } |
| 221 | |
| 222 | // get the current user's name from the os |
| 223 | void oeLnxAppDatabasecxx . . $ oeLnxAppDatabase#::get_user_namecxx . . $ oeLnxAppDatabase#get_user_name(5edd826d049e7804).(char *buffer, size_t *size) { |
| 224 | #if defined(POSIXcxx . . $ `<built-in>:477:9`!) |
| 225 | struct passwd *pwuid = getpwuid(geteuid()); |
| 226 | |
| 227 | if ((pwuid != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) && (pwuid->pw_name != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) { |
| 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 | } |