Skip to content

Commit ff2c7a6

Browse files
author
d00mas
committed
Changed almost all wchar_t to wstring. This makes the code cleaner and probably more stable.
1 parent bf4ee4b commit ff2c7a6

File tree

16 files changed

+63
-121
lines changed

16 files changed

+63
-121
lines changed

examples/XML/XML.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ int main(int argc, char *argv[])
113113

114114
void printTokens (vector <Token*> &t) {
115115
for (integer i = 0; i < t.size(); i++) {
116-
wchar_t *pwe = t[i]->symbol;
117-
wprintf (t[i]->symbol);
116+
wprintf (t[i]->symbol.c_str());
118117
wprintf (L":");
119-
wprintf (t[i]->image);
118+
wprintf (t[i]->image.c_str());
120119
wprintf (L"\n");
121120
}
122121
}

examples/logic/logic.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
char *load_source (char *filename);
2929
void printTokens (vector <Token*> &t);
30-
30+
3131
int main(int argc, char *argv[])
3232
{
3333
CGTFile cgtFile;
@@ -107,11 +107,10 @@ int main(int argc, char *argv[])
107107

108108

109109
void printTokens (vector <Token*> &t) {
110-
for (integer i = 0; i < t.size(); i++) {
111-
wchar_t *pwe = t[i]->symbol;
112-
wprintf (t[i]->symbol);
110+
for (integer i = 0; i < t.size(); i++) {
111+
wprintf (t[i]->symbol.c_str());
113112
wprintf (L":");
114-
wprintf (t[i]->image);
113+
wprintf (t[i]->image.c_str());
115114
wprintf (L"\n");
116115
}
117116
}

examples/logic/logic.ncb

0 Bytes
Binary file not shown.

examples/simple/simple.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ int main(int argc, char *argv[])
102102

103103
void printTokens (vector <Token*> &t) {
104104
for (integer i = 0; i < t.size(); i++) {
105-
wchar_t *pwe = t[i]->symbol;
106-
wprintf (t[i]->symbol);
105+
wprintf (t[i]->symbol.c_str());
107106
wprintf (L":");
108-
wprintf (t[i]->image);
107+
wprintf (t[i]->image.c_str());
109108
wprintf (L"\n");
110109
}
111110
}

include/CGTFile.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define _GP_CGTFILE_H
2121

2222
#include <stdio.h>
23-
#include <wchar.h>
2423
#include <iostream>
2524
#include <fstream>
2625
#include <vector>
@@ -33,11 +32,11 @@
3332

3433
using namespace std;
3534

36-
typedef union EntryStruct {
35+
typedef struct EntryStruct {
3736
bool vBool;
38-
integer vInteger;
39-
byte vByte;
40-
wchar_t *vString;
37+
integer vInteger;
38+
byte vByte;
39+
wstring vString;
4140
} EntryStruct ;
4241

4342
#if defined (WIN32) && defined (_USRDLL)
@@ -71,9 +70,10 @@
7170
private:
7271

7372
void readEntry (EntryStruct *entry);
74-
wchar_t *readUnicodeString ();
73+
wstring readUnicodeString ();
74+
7575
// Header
76-
wchar_t *header;
76+
wstring header;
7777

7878
// Info
7979
GrammarInfo *gInfo;
@@ -82,7 +82,6 @@
8282

8383
char *errorString;
8484

85-
// FILE *file;
8685
ifstream *theStream;
8786

8887
// Character Table

include/CharacterSetTable.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
#ifndef _GP_CHARACTER_SET_TABLE_H
1919
#define _GP_CHARACTER_SET_TABLE_H
2020

21+
#include <vector>
22+
#include <string>
2123
#include "wchar.h"
2224
#include "misc.h"
2325

2426

2527
class CharacterSetTable {
2628
public:
27-
integer nbrEntries;
28-
wchar_t **characters;
29+
integer m_nbrEntries;
30+
std::vector <std::wstring> characters;
2931

3032
CharacterSetTable (integer nbrEntries);
3133
~CharacterSetTable ();

include/GrammarInfo.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
#ifndef GRAMMAR_INFO_H
1919
#define GRAMMAR_INFO_H
2020

21-
#include <wchar.h>
21+
#include <string>
2222

2323

2424

2525
class GrammarInfo {
2626

2727
public:
28-
wchar_t *name;
29-
wchar_t *version;
30-
wchar_t *author;
31-
wchar_t *about;
28+
std::wstring name;
29+
std::wstring version;
30+
std::wstring author;
31+
std::wstring about;
3232

3333
GrammarInfo ();
3434
~GrammarInfo ();

include/SymbolTable.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
#define SYMBOLTABLE_H
1919

2020
#include "misc.h"
21-
#include <wchar.h>
21+
#include <string>
2222

2323
enum SymbolType {TERMINAL = 1, NON_TERMINAL = 0};
2424

2525
typedef struct SymbolStruct {
26-
wchar_t *name;
26+
std::wstring name;
2727
SymbolType kind;
2828
} SymbolStruct;
2929

3030

3131
class SymbolTable {
3232
public:
33-
integer nbrEntries;
33+
integer m_nbrEntries;
3434
SymbolStruct *symbols;
3535

3636
SymbolTable (integer nbrEntries);

include/Token.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef TOKEN_H
1717
#define TOKEN_H
1818

19-
#include <wchar.h>
19+
#include <string>
2020
#include "misc.h"
2121

2222

@@ -29,8 +29,8 @@
2929
~Token();
3030
Token *newInstance ();
3131

32-
wchar_t *symbol;
33-
wchar_t *image;
32+
std::wstring symbol;
33+
std::wstring image;
3434
integer symbolIndex;
3535
integer line, col;
3636

src/CGTFile.cpp

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
errorString = NULL;
2929

30-
header = NULL;
31-
3230
ruleTable = NULL;
3331
symbolTable = NULL;
3432
stateTable = NULL;
@@ -44,7 +42,6 @@
4442
delete gInfo;
4543

4644
delete [] errorString;
47-
delete [] header;
4845

4946
// Delete tables
5047
delete symbolTable;
@@ -343,40 +340,17 @@
343340
Reads a Unicode String
344341
*/
345342

346-
wchar_t *CGTFile::readUnicodeString () {
347-
int i = 0;
343+
wstring CGTFile::readUnicodeString () {
348344
wchar_t readChar;
349-
wchar_t tmpString[512];
350-
wchar_t *retString;
351-
char tmpChar1, tmpChar2;
352-
353-
//theStream->get (tmpChar1);
354-
//theStream->get (tmpChar2);
355-
theStream->read(&tmpChar1, 1);
356-
theStream->read(&tmpChar2, 1);
357-
358-
while ((tmpChar1 != 0) || (tmpChar2 != 0)) {
359-
readChar = (wchar_t) tmpChar2;
360-
readChar <<= 8;
361-
readChar |= tmpChar1;
362-
363-
tmpString[i] = readChar;
364-
365-
//theStream->get (tmpChar1);
366-
//theStream->get (tmpChar2);
367-
theStream->read(&tmpChar1,1);
368-
theStream->read(&tmpChar2,1);
369-
370-
i++;
345+
wstring str;
346+
347+
theStream->read ((char*) &readChar, 2);
348+
while (readChar != 0) {
349+
str.append (1, readChar);
350+
theStream->read ((char*)&readChar, 2);
371351
}
372-
tmpString[i] = 0;
373-
374-
retString = new wchar_t [wcslen (tmpString) + 1];
375-
376-
wcscpy (retString, tmpString);
377-
//wprintf(L"Header: %s", retString);
378352

379-
return retString;
353+
return str;
380354
}
381355

382356

@@ -401,9 +375,9 @@
401375
void CGTFile::printInfo () {
402376
// Prints the info of this grammar
403377
wprintf (L"Grammar Information\n");
404-
wprintf (L"Name: %s\n, ", gInfo->name);
405-
wprintf (L"%s\n", gInfo->version);
406-
wprintf (L"Author: %s\n", gInfo->author);
407-
wprintf (L"About: %s\n", gInfo->about);
378+
wprintf (L"Name: %s\n", gInfo->name.c_str());
379+
wprintf (L"Version: %s\n", gInfo->version.c_str());
380+
wprintf (L"Author: %s\n", gInfo->author.c_str());
381+
wprintf (L"About: %s\n", gInfo->about.c_str());
408382
}
409383

src/CharacterSetTable.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,11 @@
1919

2020

2121
CharacterSetTable::CharacterSetTable (integer nbrEntries) {
22-
this->nbrEntries = nbrEntries;
23-
characters = new wchar_t * [nbrEntries];
24-
for (int i=0; i < nbrEntries; i++) {
25-
characters[i] = NULL;
26-
}
27-
22+
m_nbrEntries = nbrEntries;
23+
characters.resize (nbrEntries);
2824
}
2925

3026
CharacterSetTable::~CharacterSetTable () {
31-
for (int i = 0; i < nbrEntries; i++) {
32-
delete [] characters[i];
33-
}
34-
delete [] characters;
27+
3528
}
3629

src/DFA.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
Token *t = NULL;
6161

6262
wstring tmpImage;
63-
wchar_t *tokenSymbol, *tokenImage;
63+
wstring tokenSymbol, tokenImage;
6464
wchar_t currentChar;
6565
integer currentState = startState;
6666
int i=0, imgIndex = 0;
@@ -123,7 +123,7 @@
123123
integer index = stateTable->states[currentState].
124124
edges[e].characterSetIndex;
125125
// Check if the current character matchs one of the edge string
126-
wchar_t *edgeString = characterSetTable->characters[index];
126+
wstring edgeString = characterSetTable->characters[index];
127127
integer s = 0;
128128
while (edgeString[s] != 0) {
129129
if (currentChar == edgeString[s]) {
@@ -161,11 +161,11 @@
161161
case 1 :
162162
if (!commentBlock) {
163163
// tmpImage[imgIndex-1] = 0;
164-
tokenSymbol = new wchar_t [wcslen(symbolTable->symbols[index2].name)+1];
165-
tokenImage = new wchar_t [tmpImage.length()+1];
164+
tokenSymbol = symbolTable->symbols[index2].name;
165+
tokenImage = tmpImage;
166166

167-
wcscpy (tokenSymbol, symbolTable->symbols[index2].name);
168-
wcscpy (tokenImage, tmpImage.c_str());
167+
// wcscpy (tokenSymbol, symbolTable->symbols[index2].name);
168+
// wcscpy (tokenImage, tmpImage.c_str());
169169

170170
t = new Token();
171171
t->symbol = tokenSymbol;
@@ -229,13 +229,11 @@
229229

230230
// Generate EOF token
231231
t = new Token ();
232-
t->symbol = new wchar_t [wcslen(L"EOF")+1];
233-
t->image = new wchar_t [wcslen (L"EOF")+1];
234232
t->line = currentLine;
235233
t->col = currentCol;
236234

237-
wcscpy (t->symbol, L"EOF");
238-
wcscpy (t->image, L"EOF");
235+
t->symbol = L"EOF";
236+
t->image = L"EOF";
239237
tokens.push_back(t);
240238

241239
return true;

src/GrammarInfo.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818
#include "GrammarInfo.h"
1919

2020
GrammarInfo::GrammarInfo () {
21-
name = NULL;
22-
author = NULL;
23-
version = NULL;
24-
about = NULL;
21+
2522
}
2623

2724

2825
GrammarInfo::~GrammarInfo () {
29-
delete [] name;
30-
delete [] author;
31-
delete [] version;
32-
delete [] about;
26+
3327
}
3428

src/LALR.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
case ACTION_SHIFT:
152152
if (DEBUG) {
153153
wprintf (L"Shifting: ");
154-
wprintf (tokens[tokenIndex]->symbol);
154+
wprintf (tokens[tokenIndex]->symbol.c_str());
155155
wprintf ( L"\nGo to state:%d\n\n", target);
156156
}
157157

@@ -213,7 +213,7 @@
213213
}
214214

215215
if (DEBUG) {
216-
wprintf (symbolTable->symbols[ruleTable->rules[target].ruleIndex].name);
216+
wprintf (symbolTable->symbols[ruleTable->rules[target].ruleIndex].name.c_str());
217217
wprintf (L" = ");
218218
}
219219

@@ -240,7 +240,7 @@
240240
if (!trim) {
241241
symIndex = newNonTerminal->children[i]->symbolIndex;
242242
}
243-
wprintf (symbolTable->symbols[symIndex].name);
243+
wprintf (symbolTable->symbols[symIndex].name.c_str());
244244
wprintf (L" ");
245245
}
246246
wprintf (L"\n");
@@ -369,7 +369,7 @@
369369
}
370370

371371
if (reduction->type == NON_TERMINAL) {
372-
wprintf (symbolTable->symbols[reduction->symbolIndex].name);
372+
wprintf (symbolTable->symbols[reduction->symbolIndex].name.c_str());
373373
wprintf (L"\n");
374374

375375
for (i=0; i < ((NonTerminal*) reduction)->children.size(); i++) {

0 commit comments

Comments
 (0)