File: lnxdata.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (241 lines) | stat: -rw-r--r-- 6,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/linux/lnxdata.cpp $
 * $Revision: 1.3 $
 * $Date: 2004/03/21 17:11:39 $
 * $Author: kevinb $
 *
 * Linux database routines
 *
 * $Log: lnxdata.cpp,v $
 * Revision 1.3  2004/03/21 17:11:39  kevinb
 * Fixes so linux will compile again. Tested with gcc-2.96
 *
 * Revision 1.2  2000/04/28 20:19:05  icculus
 * Writes "registry" to prefpath instead of current directory.
 *
 * Revision 1.1.1.1  2000/04/18 00:00:39  icculus
 * initial checkin
 *
 *
 * 9     7/14/99 9:09p Jeff
 * added comment header
 *
 * $NoKeywords: $
 */

#include <string.h>
#include <stdio.h>
#include <sys/types.h>

#if defined(POSIX)
#include <unistd.h>
#include <pwd.h>
#else
#include <windows.h>
#include <Lmcons.h>
#endif

#include <SDL.h>

#include "appdatabase.h"
#include "linux/lnxdatabase.h"
#include "pserror.h"
#include "mono.h"
#include "pserror.h"
#include "registry.h"

#define REGISTRY_FILENAME ".Descent3Registry"

// Construction and destruction.

oeLnxAppDatabase::oeLnxAppDatabase() {
  // Open up the database file, for reading, read in all data and keep it in memory
  // then close the database

  char const* prefPath = SDL_GetPrefPath("Outrage Entertainment", "Descent 3");
  if (prefPath == NULL) {
    fprintf(stderr, "ERROR: Couldn't find preference directory!\n");
    exit(43);
  }

  const size_t fileLen = strlen(prefPath) + strlen(REGISTRY_FILENAME) + 2;
  char* fileName = new char[fileLen];
  snprintf(fileName, fileLen, "%s/%s", prefPath, REGISTRY_FILENAME);

  database = new CRegistry(fileName);
  database->Import();
  create_record("Version");
  delete [] fileName;
}

oeLnxAppDatabase::oeLnxAppDatabase(oeLnxAppDatabase *parent) {
  char name[256];
  CRegistry *db = parent->GetSystemRegistry();
  db->Export();
  database = new CRegistry("");
  db->GetSystemName(name);
  database->SetSystemName(name);
  database->Import();
}

oeLnxAppDatabase::~oeLnxAppDatabase() {
  if (database) {
    database->Export();
    delete database;
    return;
  }

  mprintf(0, "Can't Export Database Since It's Not There!\n");
}

CRegistry *oeLnxAppDatabase::GetSystemRegistry() { return database; }

// Record functions
// these are actual folders of information

// creates an empty classification or structure where you can store information
bool oeLnxAppDatabase::create_record(const char *pathname) {
  ASSERT(pathname != NULL);
  if (database) {
    database->CreateKey((char *)pathname);
    return true;
  }
  mprintf(0, "Can't CreateKey because database NULL\n");
  return false;
}

// set current database focus to a particular record
bool oeLnxAppDatabase::lookup_record(const char *pathname) {
  ASSERT(pathname);
  if (database) {
    return database->LookupKey((char *)pathname);
  }
  mprintf(0, "Can't lookup key because database NULL\n");
  return false;
}

// read either a string from the current record
bool oeLnxAppDatabase::read(const char *label, char *entry, int *entrylen) {
  ASSERT(label);
  ASSERT(entry);
  ASSERT(entrylen);
  if (!database) {
    mprintf(0, "Can't read record because database NULL\n");
    return false;
  }

  // See if it exists
  int size = database->GetDataSize((char *)label);
  if (size > 0)
    *entrylen = size - 1; //-1 because of NULL
  else
    return false;

  // ok it exists, no look it up
  database->LookupRecord((char *)label, entry);
  return true;
}

// read a variable-sized integer from the current record
bool oeLnxAppDatabase::read(const char *label, void *entry, int wordsize) {
  ASSERT(label);
  ASSERT(entry);
  if (!database) {
    mprintf(0, "Can't read record because Database NULL\n");
    return false;
  }

  int size = database->GetDataSize((char *)label);
  if (size == 0)
    return false;

  // ok so it does exist
  int data;
  database->LookupRecord((char *)label, &data);

  switch (wordsize) {
  case 1:
    *((uint8_t *)entry) = (uint8_t)data;
    break;
  case 2:
    *((uint16_t *)entry) = (uint16_t)data;
    break;
  case 4:
    *((uint32_t *)entry) = (uint32_t)data;
    break;
  default:
    mprintf(0, "Unable to read key %s, unsupported size", label);
    return false;
    break;
  }
  return true;
}

bool oeLnxAppDatabase::read(const char *label, bool *entry) {
  bool data;
  if (!read(label, &data, sizeof(bool)))
    return false;

  *entry = (data != 0) ? true : false;
  return true;
}

// write either an integer or string to a record.
bool oeLnxAppDatabase::write(const char *label, const char *entry, int entrylen) {
  ASSERT(label);
  ASSERT(entry);
  if (!database) {
    mprintf(0, "Can't write record because database NULL\n");
    return false;
  }

  return database->CreateRecord((char *)label, REGT_STRING, (void *)entry);
}

bool oeLnxAppDatabase::write(const char *label, int entry) {
  ASSERT(label);
  if (!database) {
    mprintf(0, "Can't write record because database NULL\n");
    return false;
  }
  return database->CreateRecord((char *)label, REGT_DWORD, &entry);
}

// get the current user's name from the os
void oeLnxAppDatabase::get_user_name(char *buffer, size_t *size) {
#if defined(POSIX)
  struct passwd *pwuid = getpwuid(geteuid());

  if ((pwuid != NULL) && (pwuid->pw_name != NULL)) {
    strncpy(buffer, pwuid->pw_name, (*size) - 1);
    buffer[(*size) - 1] = '\0';
    *size = strlen(buffer);
  } else {
    strncpy(buffer, "Unknown", (*size) - 1);
    buffer[(*size) - 1] = '\0';
    *size = strlen(buffer);
  }
#else
DWORD unamelen = 0;
GetUserName(buffer, &unamelen);
*size = static_cast<size_t>(unamelen);
#endif
}