Descent3/linux/lnxdata.cpp

204 lines
5.0 KiB
C++
Raw Normal View History

2024-04-16 03:43:29 +00:00
/*
2024-04-16 18:56:40 +00:00
* $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
*
*
2024-04-16 03:43:29 +00:00
* 9 7/14/99 9:09p Jeff
* added comment header
2024-04-16 18:56:40 +00:00
*
* $NoKeywords: $
*/
2024-04-16 03:43:29 +00:00
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include "appdatabase.h"
#include "linux/lnxdatabase.h"
#include "pserror.h"
#include "mono.h"
#include "pserror.h"
#include "registry.h"
#include "loki_utils.h"
#define REGISTRY_FILENAME ".Descent3Registry"
2024-04-16 18:56:40 +00:00
// Construction and destruction.
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
oeLnxAppDatabase::oeLnxAppDatabase() {
// Open up the database file, for reading, read in all data and keep it in memory
// then close the database
2024-04-16 03:43:29 +00:00
char *prefPath = (char *)loki_getprefpath();
char fileName[strlen(prefPath) + strlen(REGISTRY_FILENAME) + 2];
sprintf(fileName, "%s/%s", prefPath, REGISTRY_FILENAME);
database = new CRegistry(fileName);
database->Import();
2024-04-16 18:56:40 +00:00
create_record("Version");
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
oeLnxAppDatabase::oeLnxAppDatabase(oeLnxAppDatabase *parent) {
char name[256];
CRegistry *db = parent->GetSystemRegistry();
db->Export();
database = new CRegistry("");
db->GetSystemName(name);
database->SetSystemName(name);
database->Import();
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
oeLnxAppDatabase::~oeLnxAppDatabase() {
if (database) {
2024-04-16 03:43:29 +00:00
database->Export();
delete database;
return;
}
2024-04-16 18:56:40 +00:00
mprintf((0, "Can't Export Database Since It's Not There!\n"));
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
CRegistry *oeLnxAppDatabase::GetSystemRegistry() { return database; }
// Record functions
// these are actual folders of information
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// creates an empty classification or structure where you can store information
bool oeLnxAppDatabase::create_record(const char *pathname) {
ASSERT(pathname != NULL);
if (database) {
2024-04-16 03:43:29 +00:00
database->CreateKey((char *)pathname);
return true;
}
2024-04-16 18:56:40 +00:00
mprintf((0, "Can't CreateKey because database NULL\n"));
2024-04-16 03:43:29 +00:00
return false;
}
2024-04-16 18:56:40 +00:00
// set current database focus to a particular record
bool oeLnxAppDatabase::lookup_record(const char *pathname) {
2024-04-16 03:43:29 +00:00
ASSERT(pathname);
2024-04-16 18:56:40 +00:00
if (database) {
2024-04-16 03:43:29 +00:00
return database->LookupKey((char *)pathname);
}
2024-04-16 18:56:40 +00:00
mprintf((0, "Can't lookup key because database NULL\n"));
2024-04-16 03:43:29 +00:00
return false;
}
2024-04-16 18:56:40 +00:00
// read either a string from the current record
bool oeLnxAppDatabase::read(const char *label, char *entry, int *entrylen) {
2024-04-16 03:43:29 +00:00
ASSERT(label);
ASSERT(entry);
ASSERT(entrylen);
2024-04-16 18:56:40 +00:00
if (!database) {
mprintf((0, "Can't read record because database NULL\n"));
2024-04-16 03:43:29 +00:00
return false;
}
2024-04-16 18:56:40 +00:00
// See if it exists
2024-04-16 03:43:29 +00:00
int size = database->GetDataSize((char *)label);
2024-04-16 18:56:40 +00:00
if (size > 0)
*entrylen = size - 1; //-1 because of NULL
2024-04-16 03:43:29 +00:00
else
return false;
2024-04-16 18:56:40 +00:00
// ok it exists, no look it up
database->LookupRecord((char *)label, entry);
2024-04-16 03:43:29 +00:00
return true;
}
2024-04-16 18:56:40 +00:00
// read a variable-sized integer from the current record
bool oeLnxAppDatabase::read(const char *label, void *entry, int wordsize) {
2024-04-16 03:43:29 +00:00
ASSERT(label);
ASSERT(entry);
2024-04-16 18:56:40 +00:00
if (!database) {
mprintf((0, "Can't read record because Database NULL\n"));
2024-04-16 03:43:29 +00:00
return false;
}
int size = database->GetDataSize((char *)label);
2024-04-16 18:56:40 +00:00
if (size == 0)
2024-04-16 03:43:29 +00:00
return false;
2024-04-16 18:56:40 +00:00
// ok so it does exist
2024-04-16 03:43:29 +00:00
int data;
2024-04-16 18:56:40 +00:00
database->LookupRecord((char *)label, &data);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
switch (wordsize) {
2024-04-16 03:43:29 +00:00
case 1:
*((unsigned char *)entry) = (unsigned char)data;
break;
case 2:
*((unsigned short *)entry) = (unsigned short)data;
break;
case 4:
*((unsigned int *)entry) = (unsigned int)data;
break;
default:
2024-04-16 18:56:40 +00:00
mprintf((0, "Unable to read key %s, unsupported size", label));
2024-04-16 03:43:29 +00:00
return false;
break;
}
return true;
}
2024-04-16 18:56:40 +00:00
bool oeLnxAppDatabase::read(const char *label, bool *entry) {
2024-04-16 03:43:29 +00:00
bool data;
2024-04-16 18:56:40 +00:00
if (!read(label, &data, sizeof(bool)))
return false;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
*entry = (data != 0) ? true : false;
2024-04-16 03:43:29 +00:00
return true;
}
2024-04-16 18:56:40 +00:00
// write either an integer or string to a record.
bool oeLnxAppDatabase::write(const char *label, const char *entry, int entrylen) {
2024-04-16 03:43:29 +00:00
ASSERT(label);
ASSERT(entry);
2024-04-16 18:56:40 +00:00
if (!database) {
mprintf((0, "Can't write record because database NULL\n"));
2024-04-16 03:43:29 +00:00
return false;
}
2024-04-16 18:56:40 +00:00
return database->CreateRecord((char *)label, REGT_STRING, (void *)entry);
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
bool oeLnxAppDatabase::write(const char *label, int entry) {
2024-04-16 03:43:29 +00:00
ASSERT(label);
2024-04-16 18:56:40 +00:00
if (!database) {
mprintf((0, "Can't write record because database NULL\n"));
2024-04-16 03:43:29 +00:00
return false;
}
2024-04-16 18:56:40 +00:00
return database->CreateRecord((char *)label, REGT_DWORD, &entry);
2024-04-16 03:43:29 +00:00
}
// get the current user's name from the os
2024-04-16 18:56:40 +00:00
void oeLnxAppDatabase::get_user_name(char *buffer, ulong *size) {
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);
}
2024-04-16 03:43:29 +00:00
}