forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass1.cpp
317 lines (294 loc) · 8.6 KB
/
class1.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using namespace System;
using namespace System::IO;
ref class Coder
{
private:
String^ inputFileName;
String^ outputFileName;
public:
Coder( String^ inFile, String^ outFile )
{
inputFileName = (String^)(inFile->Clone());
outputFileName = (String^)(outFile->Clone());
}
//<Snippet1>
public:
void EncodeWithString()
{
FileStream^ inFile;
array<Byte>^ binaryData;
try
{
inFile = gcnew FileStream( inputFileName,
FileMode::Open,
FileAccess::Read );
binaryData = gcnew array<Byte>((int)(inFile->Length));
long bytesRead = inFile->Read( binaryData, 0,
(int)inFile->Length );
inFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or reading from it.
Console::WriteLine( " {0}", exp->Message );
return;
}
// Convert the binary input into Base64 UUEncoded output.
String^ base64String;
try
{
base64String = Convert::ToBase64String( binaryData,
0,
binaryData->Length );
}
catch ( ArgumentNullException^ )
{
Console::WriteLine( "Binary data array is null." );
return;
}
// Write the UUEncoded version to the output file.
StreamWriter^ outFile;
try
{
outFile = gcnew StreamWriter( outputFileName,
false,
Text::Encoding::ASCII );
outFile->Write( base64String );
outFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or writing to it.
Console::WriteLine( " {0}", exp->Message );
}
}
//</Snippet1>
//<Snippet2>
public:
void EncodeWithCharArray()
{
FileStream^ inFile;
array<Byte>^binaryData;
try
{
inFile = gcnew FileStream( inputFileName,
FileMode::Open,
FileAccess::Read );
binaryData = gcnew array<Byte>((int)(inFile->Length));
long bytesRead = inFile->Read( binaryData, 0,
(int)inFile->Length );
inFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or reading from it.
Console::WriteLine( "{0}", exp->Message );
return;
}
// Convert the binary input into Base64 UUEncoded output.
// Each 3 Byte sequence in the source data becomes a 4 Byte
// sequence in the character array.
long arrayLength = (long)((4.0 / 3.0) * binaryData->Length);
// If array length is not divisible by 4, go up to the next
// multiple of 4.
if ( arrayLength % 4 != 0 )
{
arrayLength += 4 - arrayLength % 4;
}
array<Char>^ base64CharArray = gcnew array<Char>(arrayLength);
try
{
Convert::ToBase64CharArray( binaryData,
0,
binaryData->Length,
base64CharArray, 0 );
}
catch ( ArgumentNullException^ )
{
Console::WriteLine( "Binary data array is null." );
return;
}
catch ( ArgumentOutOfRangeException^ )
{
Console::WriteLine( "Char Array is not large enough." );
return;
}
// Write the UUEncoded version to the output file.
StreamWriter^ outFile;
try
{
outFile = gcnew StreamWriter( outputFileName,
false,
Text::Encoding::ASCII );
outFile->Write( base64CharArray );
outFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or writing to it.
Console::WriteLine( " {0}", exp->Message );
}
}
//</Snippet2>
//<Snippet3>
public:
void DecodeWithCharArray()
{
StreamReader^ inFile;
array<Char>^base64CharArray;
try
{
inFile = gcnew StreamReader( inputFileName,
Text::Encoding::ASCII );
base64CharArray = gcnew array<Char>((int)(inFile->BaseStream->Length));
inFile->Read( base64CharArray, 0, (int)inFile->BaseStream->Length );
inFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or reading from it.
Console::WriteLine( "{0}", exp->Message );
return;
}
// Convert the Base64 UUEncoded input into binary output.
array<Byte>^binaryData;
try
{
binaryData = Convert::FromBase64CharArray( base64CharArray,
0,
base64CharArray->Length );
}
catch ( ArgumentNullException^ )
{
Console::WriteLine( "Base 64 character array is null." );
return;
}
catch ( FormatException^ )
{
Console::WriteLine( "Base 64 Char Array length is not " +
"4 or is not an even multiple of 4." );
return;
}
// Write out the decoded data.
FileStream^ outFile;
try
{
outFile = gcnew FileStream( outputFileName,
FileMode::Create,
FileAccess::Write );
outFile->Write( binaryData, 0, binaryData->Length );
outFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or writing to it.
Console::WriteLine( "{0}", exp->Message );
}
}
//</Snippet3>
//<Snippet4>
public:
void DecodeWithString()
{
StreamReader^ inFile;
String^ base64String;
try
{
array<Char>^base64CharArray;
inFile = gcnew StreamReader( inputFileName,
Text::Encoding::ASCII );
base64CharArray = gcnew array<Char>((int)(inFile->BaseStream->Length));
inFile->Read( base64CharArray, 0, (int)inFile->BaseStream->Length );
base64String = gcnew String( base64CharArray );
}
catch ( Exception^ exp )
{
// Error creating stream or reading from it.
Console::WriteLine( "{0}", exp->Message );
return;
}
// Convert the Base64 UUEncoded input into binary output.
array<Byte>^binaryData;
try
{
binaryData = Convert::FromBase64String( base64String );
}
catch ( ArgumentNullException^ )
{
Console::WriteLine( "Base 64 String^ is null." );
return;
}
catch ( FormatException^ )
{
Console::WriteLine( "Base 64 String^ length is not " +
"4 or is not an even multiple of 4." );
return;
}
// Write out the decoded data.
FileStream^ outFile;
try
{
outFile = gcnew FileStream( outputFileName,
FileMode::Create,
FileAccess::Write );
outFile->Write( binaryData, 0, binaryData->Length );
outFile->Close();
}
catch ( Exception^ exp )
{
// Error creating stream or writing to it.
Console::WriteLine( "{0}", exp->Message );
}
}
//</Snippet4>
};
void main()
{
array<String^>^ args = Environment::GetCommandLineArgs();
if ( args->Length != 5 )
{
Console::WriteLine( "Usage: UUCodeC -d | -e " +
"-s | -c inputFile outputFile" );
return;
}
String^ inputFileName = args[ 3 ];
String^ outputFileName = args[ 4 ];
Coder^ coder = gcnew Coder( inputFileName,outputFileName );
if ( args[ 1 ] == "-d" )
{
if ( args[ 2 ] == "-s" )
{
coder->DecodeWithString();
}
else if ( args[ 2 ] == "-c" )
{
coder->DecodeWithCharArray();
}
else
{
Console::WriteLine( "Second arg must be -s or -c" );
return;
}
}
else
if ( args[ 1 ] == "-e" )
{
if ( args[ 2 ] == "-s" )
{
coder->EncodeWithString();
}
else if ( args[ 2 ] == "-c" )
{
coder->EncodeWithCharArray();
}
else
{
Console::WriteLine( "Second arg must be -s or -c" );
return;
}
}
else
{
Console::WriteLine( "First arg must be -d or -e" );
}
}