-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
197 lines (173 loc) · 4.36 KB
/
main.cc
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
#include <QVector>
#ifdef Q_OS_WIN32
#include <windows.h>
#else
#include <limits.h>
#endif
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <string>
#include "folding.hh"
#include "utf8.hh"
#include "dsl.hh"
#include "langcoder.hh"
using std::string;
#ifdef Q_OS_WIN32
int main()
{
int num;
WCHAR dslName[ MAX_PATH ], glsName[ MAX_PATH ], abbrName[ MAX_PATH ];
char uName[ MAX_PATH * 4 ], uAbbrName[ MAX_PATH * 4 ];
LPWSTR *pstr = CommandLineToArgvW( GetCommandLineW(), &num );
if( pstr && num > 2 )
{
wcscpy_s( dslName, MAX_PATH, pstr[ 1 ] );
wcscpy_s( glsName, MAX_PATH, pstr[ 2 ] );
if( wcscmp( dslName, glsName ) == 0 )
{
printf( "Names must be different\n" );
return -1;
}
}
else
{
printf( "Usage: DslToGls dsl_file gls_file\n" );
return -1;
}
if( WideCharToMultiByte( CP_UTF8, 0, dslName, -1, uName, MAX_PATH * 4, 0, 0 ) == 0 )
{
printf( "Invalid dsl file name\n" );
return -1;
}
// Check for abbreviations file
abbrName[ 0 ] = 0;
uAbbrName[ 0 ] = 0;
int n = wcslen( dslName );
if( _wcsicmp( dslName + n - 4, L".dsl" ) == 0 )
{
wcsncpy( abbrName, dslName, n - 4 );
abbrName[ n - 4 ] = 0;
}
else
if( _wcsicmp( dslName + n - 7, L".dsl.dz" ) == 0 )
{
wcsncpy( abbrName, dslName, n - 7 );
abbrName[ n - 7 ] = 0;
}
if( abbrName[ 0 ] )
{
wcscat_s( abbrName, MAX_PATH, L"_abrv.dsl" );
WIN32_FIND_DATA FindFileData;
HANDLE handle = FindFirstFile( abbrName, &FindFileData );
if( handle == INVALID_HANDLE_VALUE )
{
wcscat_s( abbrName, MAX_PATH, L".dz" );
handle = FindFirstFile( abbrName, &FindFileData );
}
if( handle != INVALID_HANDLE_VALUE )
{
FindClose( handle );
WideCharToMultiByte( CP_UTF8, 0, abbrName, -1, uAbbrName, MAX_PATH * 4, 0, 0 );
}
}
FILE * outFile = _wfopen( glsName, L"wt" );
if( outFile == 0 )
{
printf( "\nCan't open output file\n" );
return -1;
}
#else
int main( int argc, char **argv )
{
char uAbbrName[ PATH_MAX ];
char *uName, *glsName;
if( argc > 2 )
{
uName = argv[ 1 ];
glsName = argv[ 2 ];
if( strcmp( uName, glsName ) == 0 )
{
printf( "Names must be different\n" );
return -1;
}
}
else
{
printf( "Usage: dsltogls dsl_file gls_file\n" );
return -1;
}
// Check for abbreviations file
uAbbrName[ 0 ] = 0;
int n = strlen( uName );
if( strcmp( uName + n - 4, ".dsl" ) == 0 )
{
strncpy( uAbbrName, uName, n - 4 );
uAbbrName[ n - 4 ] = 0;
}
else
if( strcmp( uName + n - 7, ".dsl.dz" ) == 0 )
{
strncpy( uAbbrName, uName, n - 7 );
uAbbrName[ n - 7 ] = 0;
}
if( uAbbrName[ 0 ] )
{
strncat( uAbbrName, "_abrv.dsl", PATH_MAX );
if( access( uAbbrName, 0 ) != 0 )
{
strncat( uAbbrName, ".dz", PATH_MAX );
if( access( uAbbrName, 0 ) != 0 )
{
uAbbrName[ 0 ] = 0;
}
}
}
FILE * outFile = fopen( glsName, "wt" );
if( outFile == 0 )
{
printf( "\nCan't open output file\n" );
return -1;
}
#endif
DslDictionary dict;
try
{
int n = dict.setFiles( string( uName ), uAbbrName );
if( n )
{
fclose( outFile );
return n;
}
fprintf( outFile, "### Glossary title:%s\n", Utf8::encode( dict.getParams().name ).c_str() );
fprintf( outFile, "### Author:\n" );
fprintf( outFile, "### Description:%s\n", dict.getDescription( uName ).toUtf8().data() );
fprintf( outFile, "### Source language:%s\n", Utf8::encode( dict.getParams().langFrom ).c_str() );
fprintf( outFile, "### Target language:%s\n", Utf8::encode( dict.getParams().langTo ).c_str() );
fprintf( outFile, "### Glossary section:\n\n" );
for( int i = 0; i < dict.getCards().size(); i++ )
{
DslCard const & card = dict.getCards()[ i ];
string article;
dict.getArticle( card, article );
fprintf( outFile, "%s", Utf8::encode( card.headwords.at( 0 ) ).c_str() );
for( int j = 1; j < card.headwords.size(); j++ )
fprintf( outFile, "|%s", Utf8::encode( card.headwords.at( j ) ).c_str() );
fprintf( outFile, "\n%s\n\n", article.c_str() );
}
}
catch( std::exception & e )
{
fclose( outFile );
printf( "\nConversion failed, error: %s\n", e.what() );
return -1;
}
catch( ... )
{
fclose( outFile );
printf( "\nConversion failed\n" );
return -1;
}
fclose( outFile );
return 0;
}