-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
439 lines (387 loc) · 14.8 KB
/
Main.cpp
File metadata and controls
439 lines (387 loc) · 14.8 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// ============================================================================
//
// This file contains the actions, conditions and expressions your object uses
//
// ============================================================================
#include "Common.h"
//Macros for Thread Safety
//If accessing rdPtr, wrap the accessing code in these
#define ThreadSafe_Start() \
while (rdPtr -> threadsafe) \
{ \
Sleep(0); \
} \
rdPtr -> threadsafe=true
#define ThreadSafe_End() rdPtr -> threadsafe=false
//Macro for simplicity
#define Terminate() \
ThreadSafe_Start(); \
delete[] infilename, outfilename; \
rdPtr -> LastOutput = outfilename; \
rdPtr -> rRd -> CallEvent(1); \
ThreadSafe_End(); \
return 1
// Please note: Using GenerateEvent in place of PushEvent is not a good idea, being that
// access to rdPtr is blocked by ThreadSafe_Start. Example code:
// ThreadSafe_Start();
// rdPtr -> GenerateEvent(0);
// ThreadSafe_End();
// If the user retrieves expressions with GenerateEvent, the ThreadSafe_End is not run
// until after they retrieve the variables. So of course the while loop in ThreadSafe_Start
// runs without stopping.
// PushEvent is slower, being called on next MMF loop, which means the expressions are accessed
// after: No "infinite-while-loop" problem.
// So here we have the caller type:
#define CallEvent PushEvent
//Define struct used to export data from actions to threads
struct Params
{
char para_infilename [MAX_PATH]; //Input file name
char para_outfilename [MAX_PATH]; //Output file name
bool para_UseAppend; //If true, append; otherwise (over)write output
LPRDATA para_rdPtr; //rdPtr (must be given or threads cannot access rdPtr)
};
//Calculate file size
unsigned long file_size(char *filename) {
FILE *pFile = NULL;
if (fopen_s(&pFile, filename, "rb")) // Open file
return 0; // If handle has a problem, return 0, eg no file
fseek(pFile, 0, SEEK_END); //If not, goto end of file
unsigned long size = ftell(pFile); //Get position (Must be stored so fclose can be run)
fclose(pFile); //Close file
return size; //Return
}
bool FileSizeCheck(char *infilename, LPRDATA rdPtr)
{
if (file_size(infilename) < 1)
{
ThreadSafe_Start();
rdPtr -> returnstring = "Input file size 0 or nonexistent.";
ThreadSafe_End();
return true;
}
else
return false;
}
bool HandleCheck(FILE *file, gzFile gzfile, LPRDATA rdPtr)
{
if (!file)
{
ThreadSafe_Start();
rdPtr -> returnstring = "Input file malfunctioned.";
gzclose(gzfile);
ThreadSafe_End();
return true;
}
else if (!gzfile)
{
ThreadSafe_Start();
rdPtr -> returnstring = "Output file malfunctioned.";
fclose(file);
ThreadSafe_End();
return true;
}
else
{
return false;
}
}
//Compress a file
DWORD WINAPI compress_one_file(Params *pDataArray)
{
//Open struct and set variables
char *infilename = new char [MAX_PATH]; //Get input file name
strcpy_s(infilename, MAX_PATH, pDataArray -> para_infilename);
char *outfilename = new char [MAX_PATH]; //Get output file name
strcpy_s(outfilename, MAX_PATH, pDataArray -> para_outfilename);
bool UseAppend = pDataArray -> para_UseAppend; //G et whether to append or write
LPRDATA rdPtr = pDataArray -> para_rdPtr; //Get rdPtr
delete pDataArray; //Container is expendable
char * WriteType = "wb9"; //User is using write (overwriting)
if (UseAppend) WriteType = "ab9"; //User is using append
//Check input file size - no handles to close
if (FileSizeCheck(infilename, rdPtr)) {Terminate();}
//Open handles to both files
FILE *infile = fopen(infilename, "rb"); //r = read, b = binary
gzFile outfile = gzopen(outfilename, WriteType); //a = append (or w=write), b = binary, 9 = max compression
//Check handles - HandleCheck() automatically closes handles if invalid
if (HandleCheck(infile, outfile, rdPtr)) {Terminate();}
//Declare variables
unsigned long PreviousOutputSize = 0; //For calculation purposes
signed int num_read = 0;
unsigned long total_read = 0;
ThreadSafe_Start();
unsigned short tempinbuffersize = rdPtr -> inbuffersize;
ThreadSafe_End();
//If using Write, output size should not be included in the calculation later. Otherwise:
if (UseAppend) PreviousOutputSize = file_size(outfilename);
//This makes sure that the buffer is the right size - if too large, set buffer smaller
if ( file_size(infilename) < tempinbuffersize)
tempinbuffersize = (unsigned short)file_size(infilename);
//Then declare final variable, the buffer
char *inbuffer = new char[tempinbuffersize];
//Iteration through the files
while ((num_read = fread(inbuffer, 1, tempinbuffersize, infile)) > 0)
{
total_read += num_read;
gzwrite(outfile, inbuffer, num_read);
}
//Close thread
fclose(infile);
gzclose(outfile);
delete[] inbuffer, infilename, outfilename;
//Set variables afnter completion
ThreadSafe_Start();
unsigned long saveoutfilesize = file_size(outfilename);
unsigned long saveinfilesize = file_size(infilename);
rdPtr -> PercentageDifference = ((file_size(outfilename)-PreviousOutputSize)*(1.0/file_size(infilename)))*100.0;
stringstream temp;
temp <<"Buffer used: "
<< tempinbuffersize
<<", total bytes read: "
<< file_size(infilename)
<<", total bytes written: "
<< file_size(outfilename)-PreviousOutputSize
<<", compression rate: "
<< rdPtr -> PercentageDifference
<<"%.";
rdPtr -> returnstring = temp.str();
temp.flush();
rdPtr -> LastOutput = outfilename;
rdPtr -> rRd -> PushEvent(0);
ThreadSafe_End();
return 0;
}
//Decompress a file
DWORD WINAPI decompress_one_file(Params *pDataArray)
{
//Open struct and set variables
char *infilename = new char [MAX_PATH]; //Get input file name
strcpy_s(infilename, MAX_PATH, pDataArray -> para_infilename);
char *outfilename = new char [MAX_PATH]; //Get output file name
strcpy_s(outfilename, MAX_PATH, pDataArray -> para_outfilename);
bool UseAppend = pDataArray -> para_UseAppend; //Get whether to append or write
LPRDATA rdPtr = pDataArray -> para_rdPtr; //Get rdPtr
delete pDataArray; //Container is expendable
char * WriteType = "wb"; //User is using write (overwriting)
if (UseAppend) WriteType = "ab"; //User is using append
if (FileSizeCheck(infilename, rdPtr)) {Terminate();}
//Open handles to both files
gzFile infile = gzopen(infilename, "rb"); //r = read, b = binary
FILE *outfile = fopen(outfilename, WriteType); //a = append or w = write, b = binary
if (HandleCheck(outfile, infile, rdPtr)) {Terminate();}
//Declare variables
unsigned long PreviousOutputSize = 0; //For calculation purposes
int num_read = 0;
unsigned long total_read = 0;
ThreadSafe_Start();
unsigned short tempinbuffersize = rdPtr -> inbuffersize;
ThreadSafe_End();
//If using Write, output size should not be included in the calculation later. Otherwise:
if (UseAppend) PreviousOutputSize = file_size(outfilename);
//This makes sure that the buffer is the right size - if too large, set buffer smaller
if ( file_size(infilename) < tempinbuffersize)
tempinbuffersize = (unsigned short)file_size(infilename);
//Then declare final variable, the buffer
char *inbuffer = new char[tempinbuffersize];
//Iteration through the files
while ((num_read = gzread(infile, inbuffer, tempinbuffersize)) > 0)
{
total_read += num_read;
fwrite(inbuffer, 1, num_read, outfile);
}
//Close thread
gzclose(infile);
fclose(outfile);
delete[] inbuffer, infilename, outfilename;
//Set variables after completion
ThreadSafe_Start();
rdPtr -> PercentageDifference = ((file_size(outfilename)-PreviousOutputSize)*(1.0/file_size(infilename)))*100.0;
stringstream temp;
temp <<"Buffer used: "
<< tempinbuffersize
<<", total bytes read: "
<< file_size(infilename)
<<", total bytes written: "
<< file_size(outfilename)-PreviousOutputSize
<<", decompression rate: "
<< rdPtr -> PercentageDifference
<<"%.";
rdPtr -> returnstring = temp.str();
temp.flush();
rdPtr -> LastOutput = outfilename;
rdPtr -> rRd -> CallEvent(0);
ThreadSafe_End();
return 0;
}
// ============================================================================
//
// CONDITIONS
//
// ============================================================================
CONDITION(
/* ID */ 0,
/* Name */ "%o: On success",
/* Flags */ 0,
/* Params */ (0)
) {
return true;
}
CONDITION(
/* ID */ 1,
/* Name */ "%o: On error",
/* Flags */ 0,
/* Params */ (0)
) {
return true;
}
// ============================================================================
//
// ACTIONS
//
// ============================================================================
ACTION( //Old compress function
/* ID */ 0,
/* Name */ "<<REPLACE THIS ACTION>> Compress a file (%0) --> (%1)",
/* Flags */ 0,
/* Params */ (2, PARAM_STRING, "Input file:", PARAM_STRING, "Output file:")
) {
//Old version of compress action: Notify user
MessageBoxA(NULL, "Sorry to interrupt your programming, but you have\nan old \"Compress file\" action in ZlibStream which needs replacing.\nClick the ZlibStream icon in your event editor to see all the events\nZlibStream is in.", "Darkwire Software - Old Event Notice", MB_OK);
}
ACTION( //Old decompress function
/* ID */ 1,
/* Name */ "<<REPLACE THIS ACTION>> Decompress a file (%0) --> (%1)",
/* Flags */ 0,
/* Params */ (2, PARAM_STRING, "Input file:", PARAM_STRING, "Output file:")
) {
//Old version of decompress action: Notify user
MessageBoxA(NULL, "Sorry to interrupt your programming, but you have\nan old \"Decompress file\" action in ZlibStream which needs replacing.\nClick the ZlibStream icon in your event editor to see all the events\nZlibStream is in.", "Darkwire Software - Old Event Notice", MB_OK);
}
ACTION(
/* ID */ 2,
/* Name */ "Set buffer size to %0 bytes",
/* Flags */ 0,
/* Params */ (1, PARAM_NUMBER, "Buffer size (1 to 16384 bytes):")
) {
int p1 = Param(TYPE_INT);
ThreadSafe_Start();
if (p1 > 0 && p1 < 16385)
rdPtr -> inbuffersize = p1;
else
{
rdPtr -> returnstring = "Buffer is an invalid size. Must be between 0 and 16385 (exclusive).";
rdPtr -> rRd -> CallEvent(1);
}
ThreadSafe_End();
}
ACTION(
/* ID */ 3,
/* Name */ "Compress a file (%0) --> (%1), append = %2",
/* Flags */ 0,
/* Params */ (3, PARAM_STRING, "Input file:", PARAM_STRING, "Output file:", PARAM_NUMBER, "Append if output file already exists? (0 for overwriting, 1 for adding to end)")
) {
//Retrieve variables from parameters
char *p1 = (char *) Param(TYPE_STRING);
char *p2 = (char *) Param(TYPE_STRING);
int p3 = Param(TYPE_INT);
//Set bool for simplicity
bool temp = false;
if (p3 != 0)
temp = true;
//Declare struct and set variables in struct
Params* Parameters = new Params; //Declaration
strcpy_s(Parameters -> para_infilename, MAX_PATH, p1); //Pass the input file name
strcpy_s(Parameters -> para_outfilename, MAX_PATH, p2); //Pass the output file name
Parameters -> para_UseAppend = temp; //Pass the append command
Parameters -> para_rdPtr = rdPtr; //Pass rdPtr
//Create thread
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&compress_one_file, Parameters, 0, NULL);
}
ACTION(
/* ID */ 4,
/* Name */ "Decompress a file (%0) --> (%1), append = %2",
/* Flags */ 0,
/* Params */ (3, PARAM_STRING, "Input file:", PARAM_STRING, "Output file:", PARAM_NUMBER, "Append if output file already exists? (0 for overwriting, 1 for adding to end)")
) {
//Retrieve variables from parameters
char *p1 = (char *) Param(TYPE_STRING);
char *p2 = (char *) Param(TYPE_STRING);
int p3 = Param(TYPE_INT);
//Set bool for simplicity
bool temp = false;
if (p3 != 0)
temp = true;
//Declare struct and set variables in struct
Params* Parameters = new Params; //Declaration
strcpy_s(Parameters -> para_infilename, MAX_PATH, p1); //Pass the input file name
strcpy_s(Parameters -> para_outfilename, MAX_PATH, p2); //Pass the output file name
Parameters -> para_UseAppend = temp; //Pass the append command
Parameters -> para_rdPtr = rdPtr; //Pass rdPtr
//Create thread
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&decompress_one_file, Parameters, 0, NULL);
}
// ============================================================================
//
// EXPRESSIONS
//
// ============================================================================
EXPRESSION(
/* ID */ 0,
/* Name */ "ReturnedCode(",
/* Flags */ 0,
/* Params */ (0)
) {
//Deprecated variable: Notify user
MessageBoxA(NULL, "Sorry to interrupt your programming, but you have\nan old \"ReturnedCode()\" expression from ZlibStream which is removed.\nClick the ZlibStream icon in your event editor to see all the events\nZlibStream is in.", "Darkwire Software - Old Expression Notice", MB_OK);
return false;
}
EXPRESSION(
/* ID */ 1,
/* Name */ "ReturnedString$(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (0)
) {
ThreadSafe_Start();
string temp = rdPtr -> returnstring;
ThreadSafe_End();
ReturnStringSafe(temp.c_str());
}
EXPRESSION(
/* ID */ 2,
/* Name */ "BufferSize(",
/* Flags */ 0,
/* Params */ (0)
) {
ThreadSafe_Start();
unsigned short temp = rdPtr -> inbuffersize;
ThreadSafe_End();
return temp;
}
EXPRESSION(
/* ID */ 3,
/* Name */ "Percentage(",
/* Flags */ EXPFLAG_DOUBLE,
/* Params */ (0)
) {
ThreadSafe_Start();
double temp = rdPtr -> PercentageDifference;
ThreadSafe_End();
//This casts to 2 decimal places
char sprintfdest[20];
sprintf(sprintfdest,"%.2f", temp);
float temp2 = (float)atof(sprintfdest);
//Voila
ReturnFloat(temp2);
}
EXPRESSION(
/* ID */ 4,
/* Name */ "LastOutputFile$(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (0)
) {
ThreadSafe_Start();
string temp = rdPtr -> LastOutput;
ThreadSafe_End();
ReturnStringSafe(temp.c_str());
}