mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 03:37:49 +02:00
added reading/writing to jurisdiction files
This commit is contained in:
parent
983db5f70d
commit
81a363f053
3 changed files with 96 additions and 13 deletions
|
@ -7,6 +7,8 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
#include <QString>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
#include "JurisdictionMap.h"
|
#include "JurisdictionMap.h"
|
||||||
#include "VoxelNode.h"
|
#include "VoxelNode.h"
|
||||||
|
@ -16,16 +18,20 @@ JurisdictionMap::~JurisdictionMap() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void JurisdictionMap::clear() {
|
void JurisdictionMap::clear() {
|
||||||
delete[] _rootOctalCode;
|
if (_rootOctalCode) {
|
||||||
_rootOctalCode = NULL;
|
delete[] _rootOctalCode;
|
||||||
|
_rootOctalCode = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < _endNodes.size(); i++) {
|
for (int i = 0; i < _endNodes.size(); i++) {
|
||||||
delete[] _endNodes[i];
|
if (_endNodes[i]) {
|
||||||
|
delete[] _endNodes[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_endNodes.clear();
|
_endNodes.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
JurisdictionMap::JurisdictionMap() {
|
JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) {
|
||||||
unsigned char* rootCode = new unsigned char[1];
|
unsigned char* rootCode = new unsigned char[1];
|
||||||
*rootCode = 0;
|
*rootCode = 0;
|
||||||
|
|
||||||
|
@ -33,13 +39,14 @@ JurisdictionMap::JurisdictionMap() {
|
||||||
init(rootCode, emptyEndNodes);
|
init(rootCode, emptyEndNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
JurisdictionMap::JurisdictionMap(const char* filename) {
|
JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) {
|
||||||
clear(); // clean up our own memory
|
clear(); // clean up our own memory
|
||||||
readFromFile(filename);
|
readFromFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes) {
|
JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes)
|
||||||
|
: _rootOctalCode(NULL) {
|
||||||
init(rootOctalCode, endNodes);
|
init(rootOctalCode, endNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,10 +62,65 @@ bool JurisdictionMap::isMyJurisdiction(VoxelNode* node, int childIndex) const {
|
||||||
|
|
||||||
|
|
||||||
bool JurisdictionMap::readFromFile(const char* filename) {
|
bool JurisdictionMap::readFromFile(const char* filename) {
|
||||||
QSettings settings(QString(filename), QSettings::IniFormat);
|
QString settingsFile(filename);
|
||||||
QString rootCode = settings->value("root","").toString();
|
QSettings settings(settingsFile, QSettings::IniFormat);
|
||||||
qDebug("rootCode=%s\n",rootCode);
|
QString rootCode = settings.value("root","").toString();
|
||||||
|
qDebug() << "rootCode=" << rootCode << "\n";
|
||||||
|
|
||||||
|
unsigned char* rootOctCode = hexStringToOctalCode(rootCode);
|
||||||
|
printOctalCode(rootOctCode);
|
||||||
|
|
||||||
|
settings.beginGroup("endNodes");
|
||||||
|
const QStringList childKeys = settings.childKeys();
|
||||||
|
QHash<QString, QString> values;
|
||||||
|
foreach (const QString &childKey, childKeys) {
|
||||||
|
QString childValue = settings.value(childKey).toString();
|
||||||
|
values.insert(childKey, childValue);
|
||||||
|
qDebug() << childKey << "=" << childValue << "\n";
|
||||||
|
|
||||||
|
unsigned char* octcode = hexStringToOctalCode(childValue);
|
||||||
|
printOctalCode(octcode);
|
||||||
|
|
||||||
|
_endNodes.push_back(octcode);
|
||||||
|
}
|
||||||
|
settings.endGroup();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JurisdictionMap::writeToFile(const char* filename) {
|
bool JurisdictionMap::writeToFile(const char* filename) {
|
||||||
|
QString settingsFile(filename);
|
||||||
|
QSettings settings(settingsFile, QSettings::IniFormat);
|
||||||
|
|
||||||
|
settings.setValue("root", "rootNodeValue");
|
||||||
|
|
||||||
|
settings.beginGroup("endNodes");
|
||||||
|
for (int i = 0; i < _endNodes.size(); i++) {
|
||||||
|
QString key = QString("endnode%1").arg(i);
|
||||||
|
QString value = QString("valuenode%1").arg(i);
|
||||||
|
settings.setValue(key, value);
|
||||||
|
}
|
||||||
|
settings.endGroup();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned char* JurisdictionMap::hexStringToOctalCode(const QString& input) {
|
||||||
|
// i variable used to hold position in string
|
||||||
|
int i = 0;
|
||||||
|
// x variable used to hold byte array element position
|
||||||
|
int x = 0;
|
||||||
|
// allocate byte array based on half of string length
|
||||||
|
unsigned char* bytes = new unsigned char[(input.length()) / 2];
|
||||||
|
// loop through the string - 2 bytes at a time converting
|
||||||
|
// it to decimal equivalent and store in byte array
|
||||||
|
while (input.length() > i + 1) {
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
uint value = input.mid(i, 2).toUInt(&ok,16);
|
||||||
|
bytes[x] = (unsigned char)value;
|
||||||
|
i += 2;
|
||||||
|
x += 1;
|
||||||
|
}
|
||||||
|
// return the finished byte array of decimal values
|
||||||
|
return bytes;
|
||||||
}
|
}
|
|
@ -10,6 +10,7 @@
|
||||||
#define __hifi__JurisdictionMap__
|
#define __hifi__JurisdictionMap__
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
class VoxelNode; // forward declaration
|
class VoxelNode; // forward declaration
|
||||||
|
|
||||||
|
@ -20,15 +21,16 @@ public:
|
||||||
JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes);
|
JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes);
|
||||||
~JurisdictionMap();
|
~JurisdictionMap();
|
||||||
|
|
||||||
|
|
||||||
bool isMyJurisdiction(VoxelNode* node, int childIndex) const;
|
bool isMyJurisdiction(VoxelNode* node, int childIndex) const;
|
||||||
|
|
||||||
|
bool writeToFile(const char* filename);
|
||||||
|
bool readFromFile(const char* filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void clear();
|
void clear();
|
||||||
void init(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes);
|
void init(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes);
|
||||||
|
|
||||||
bool writeToFile(const char* filename);
|
unsigned char* hexStringToOctalCode(const QString& input);
|
||||||
bool readFromFile(const char* filename);
|
|
||||||
|
|
||||||
|
|
||||||
unsigned char* _rootOctalCode;
|
unsigned char* _rootOctalCode;
|
||||||
|
|
|
@ -430,6 +430,7 @@ void attachVoxelNodeDataToNode(Node* newNode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int receivedPacketCount = 0;
|
int receivedPacketCount = 0;
|
||||||
|
JurisdictionMap* jurisdiction = NULL;
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char * argv[]) {
|
||||||
pthread_mutex_init(&::treeLock, NULL);
|
pthread_mutex_init(&::treeLock, NULL);
|
||||||
|
@ -448,6 +449,20 @@ int main(int argc, const char * argv[]) {
|
||||||
printf("portParameter=%s listenPort=%d\n", portParameter, listenPort);
|
printf("portParameter=%s listenPort=%d\n", portParameter, listenPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* JURISDICTION_FILE = "--jurisdictionFile";
|
||||||
|
const char* jurisdictionFile = getCmdOption(argc, argv, JURISDICTION_FILE);
|
||||||
|
if (jurisdictionFile) {
|
||||||
|
printf("jurisdictionFile=%s\n", jurisdictionFile);
|
||||||
|
|
||||||
|
printf("about to readFromFile().... jurisdictionFile=%s\n", jurisdictionFile);
|
||||||
|
jurisdiction = new JurisdictionMap(jurisdictionFile);
|
||||||
|
printf("after readFromFile().... jurisdictionFile=%s\n", jurisdictionFile);
|
||||||
|
|
||||||
|
// test writing the file...
|
||||||
|
//printf("about to writeToFile().... jurisdictionFile=%s\n", jurisdictionFile);
|
||||||
|
//jurisdiction->writeToFile(jurisdictionFile);
|
||||||
|
//printf("after writeToFile().... jurisdictionFile=%s\n", jurisdictionFile);
|
||||||
|
}
|
||||||
|
|
||||||
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_VOXEL_SERVER, listenPort);
|
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_VOXEL_SERVER, listenPort);
|
||||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||||
|
@ -734,6 +749,10 @@ int main(int argc, const char * argv[]) {
|
||||||
pthread_join(sendVoxelThread, NULL);
|
pthread_join(sendVoxelThread, NULL);
|
||||||
pthread_mutex_destroy(&::treeLock);
|
pthread_mutex_destroy(&::treeLock);
|
||||||
|
|
||||||
|
if (jurisdiction) {
|
||||||
|
delete jurisdiction;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue