Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[2fa05f]: gemrb / plugins / 2DAImporter / 2DAImporter.cpp Maximize Restore History

Download this file

2DAImporter.cpp    113 lines (100 with data), 2.9 kB

  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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#include "2DAImporter.h"
#include "win32def.h"
#include "Interface.h"
#include "System/FileStream.h"
#define MAXLENGTH 4096 //if a 2da has longer lines, change this
#define SIGNLENGTH 256 //if a 2da has longer default value, change this
p2DAImporter::p2DAImporter(void)
{
}
p2DAImporter::~p2DAImporter(void)
{
for (unsigned int i = 0; i < ptrs.size(); i++) {
free( ptrs[i] );
}
}
bool p2DAImporter::Open(DataStream* str)
{
if (str == NULL) {
return false;
}
char Signature[SIGNLENGTH];
str->CheckEncrypted();
str->ReadLine( Signature, sizeof(Signature) );
char* strp = Signature;
while (*strp == ' ')
strp++;
if (strncmp( strp, "2DA V1.0", 8 ) != 0) {
printMessage("2DAImporter", "Bad signature (%s)! Ignoring...\n",YELLOW, str->filename );
// we don't care about this, so exptable.2da of iwd2 won't cause a bigger problem
// also, certain creatures are described by 2da's without signature.
// return false;
}
Signature[0] = 0;
str->ReadLine( Signature, sizeof(Signature) );
char* token = strtok( Signature, " " );
if (token) {
strncpy(defVal, token, sizeof(defVal) );
} else { // no whitespace
strncpy(defVal, Signature, sizeof(defVal) );
}
bool colHead = true;
int row = 0;
while (true) {
char* line = ( char* ) malloc( MAXLENGTH );
int len = str->ReadLine( line, MAXLENGTH-1 );
if (len <= 0) {
free( line );
break;
}
if (line[0] == '#') { // allow comments
free( line );
continue;
}
if (len < MAXLENGTH)
line = ( char * ) realloc( line, len + 1 );
ptrs.push_back( line );
if (colHead) {
colHead = false;
char* str = strtok( line, " " );
while (str != NULL) {
colNames.push_back( str );
str = strtok( NULL, " " );
}
} else {
char* str = strtok( line, " " );
if (str == NULL)
continue;
rowNames.push_back( str );
RowEntry r;
rows.push_back( r );
while (( str = strtok( NULL, " " ) ) != NULL) {
rows[row].push_back( str );
}
row++;
}
}
delete str;
return true;
}
#include "plugindef.h"
GEMRB_PLUGIN(0xB22F938, "2DA File Importer")
PLUGIN_CLASS(IE_2DA_CLASS_ID, p2DAImporter)
END_PLUGIN()