forked from HeliumProject/Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
litesql-gen.pl
202 lines (166 loc) · 4.94 KB
/
litesql-gen.pl
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
use strict;
use Cwd;
use Data::Dumper;
use File::Basename;
use File::Find;
use File::Path;
use File::Spec;
use Getopt::Long;
use Time::Local;
my $verbose = 0;
###############################################################################
sub PrintUsage
{
# get just the binary name, not the full path to it
my $scriptName = $0;
$scriptName =~ s/^.*\\(.*?)$/$1/;
print qq{
Usage: $scriptName <file> <lite-sql.exe path>
Examples:
$scriptName "Luna\\Vault\\AssetCacheDB.xml"
$scriptName "Luna\\Vault\\AssetCacheDB.xml" "SDK\\LiteSQL\\0.3.8_prebuilt\\bin\\litesql-gen.exe"
};
}
###############################################################################
my $xmlFilename = shift @ARGV;
if ( not defined $xmlFilename )
{
print "Please pass in LiteSQL config file.\n";
PrintUsage();
exit 1;
}
if ( not -e $xmlFilename )
{
print "Can't find LiteSQL config file at: $xmlFilename.\n";
PrintUsage();
exit 1;
}
$xmlFilename = File::Spec->rel2abs( $xmlFilename );
my $liteSQLPath = shift @ARGV;
if ( not defined $liteSQLPath )
{
$liteSQLPath = File::Spec->rel2abs( "SDK\\LiteSQL\\0.3.8_prebuilt\\bin\\litesql-gen.exe" );
}
if ( not -e $liteSQLPath )
{
print "Can't find litesql-gen.exe at: $liteSQLPath.\n";
PrintUsage();
exit 1;
}
$liteSQLPath = File::Spec->rel2abs( $liteSQLPath );
my($dbName, $outFolder, $suffix) = fileparse( $xmlFilename, qr/\.[^.]*/ );
my $lowDBName = $dbName;
$lowDBName =~ tr/[A-Z]/[a-z]/;
my $inFolder = dirname( $liteSQLPath );
my $inCppFile = File::Spec->catfile( $inFolder, "$lowDBName.cpp" );
my $outCppFile = File::Spec->catfile( $outFolder, "$dbName.cpp" );
my $inHFile = File::Spec->catfile( $inFolder, "$lowDBName.hpp" );
my $outHFile = File::Spec->catfile( $outFolder, "$dbName.h");
if ( $verbose )
{
print "\n";
print "dbName: $dbName\n";
print "lowDBName: $lowDBName\n";
print "inFolder: $inFolder\n";
print "outFolder: $outFolder\n";
print "suffix: $suffix\n";
}
unlink( $inCppFile ) if -e $inCppFile;
unlink( $inHFile ) if -e $inHFile;
chdir "$inFolder";
my $generateDate = localtime;
DoSystemCommand( "\"$liteSQLPath\" -t c++ \"$xmlFilename\"" );
CleanFile( $inHFile, $outHFile );
CleanFile( $inCppFile, $outCppFile );
exit 0;
###############################################################################
sub CleanFile
{
my $inFile = shift;
my $outFile = shift;
open( IN, "<$inFile" ) or die $!;
my @file = <IN>;
chomp @file;
close IN;
my($inFilename, $inFolder, $inSuffix) = fileparse( $inFile, qr/\.[^.]*/ );
my($outFilename, $outFolder, $outSuffix) = fileparse( $outFile, qr/\.[^.]*/ );
if ( $verbose )
{
print "\n";
print "inSuffix: $inSuffix\n";
print "outSuffix: $outSuffix\n";
print "in: $inFilename$inSuffix\n";
print "out: $outFilename$outSuffix\n";
print "\n";
print "inFile: $inFile\n";
print "\n";
print "outFile: $outFile\n";
}
foreach my $line ( @file )
{
chomp $line;
#Fixup includes to header hpp file
$line =~ s/$inFilename\.hpp/$outFilename.h/img;
next if( $line =~ /\#include/ );
# Quote string literals
$line =~ s/("(?:[^"\\]+|\\.)*")/ TXT( \1 )/g;
# Fixup string vars
$line =~ s/([^\w]+)char(?=\s*\*)/\1tchar/mg;
$line =~ s/std\:\:string/tstring/mg;
$line =~ s/std\:\:istream/tistream/mg;
$line =~ s/std\:\:ostream/tostream/mg;
$line =~ s/std\:\:iostream/tiostream/mg;
$line =~ s/std\:\:ifstream/tifstream/mg;
$line =~ s/std\:\:ofstream/tofstream/mg;
$line =~ s/std\:\:fstream/tfstream/mg;
$line =~ s/std\:\:istringstream/tistringstream/mg;
$line =~ s/std\:\:ostringstream/tostringstream/mg;
$line =~ s/std\:\:stringstream/tstringstream/mg;
}
my $fileContents = "";
$fileContents .="///////////////////////////////////////////////////////////////////////////\n";
$fileContents .="// Generated by litesql-gen.exe on $generateDate (localtime): \n";
$fileContents .="// $liteSQLPath\n";
$fileContents .="// http://sourceforge.net/apps/trac/litesql/ \n";
$fileContents .="//\n";
$fileContents .="// PLEASE DO \"NOT\" EDIT THIS FILE!\n";
$fileContents .="///////////////////////////////////////////////////////////////////////////\n";
$fileContents .="\n";
if ( $inSuffix =~ /\.cpp/i )
{
$fileContents .="#include \"Precompile.h\"\n";
$fileContents .="\n";
}
elsif ( $inSuffix =~ /\.h(?:pp){0,1}/i )
{
$fileContents .="#pragma once\n";
$fileContents .="\n";
$fileContents .="#include \"Platform/Types.h\"\n";
$fileContents .="\n";
}
else
{
die "What kind of file is this?";
}
$fileContents .= join( "\n", @file );
unlink ( $outFile ) if -e $outFile;
open( OUT, ">$outFile" ) or die $!;
print OUT $fileContents;
unlink( $inFile ) if -e $inFile;
}
###############################################################################
sub DoSystemCommand
{
my $command = shift;
if( $verbose )
{
print( $command . "\n" );
}
system( $command );
my $result = $? >> 8;
if ( $result != 0 )
{
print( "$command failed with error $result" );
exit 1;
}
}