Skip to content

Commit 88b9270

Browse files
committed
Further simplified file cleaning
Now file cleaning is done through methods to avoid code repetition. Signed-off-by: Victor Matia <[email protected]>
1 parent fc66184 commit 88b9270

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

Core/Libraries/Source/Compression/LZHCompress/NoxCompress.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
#define DbgFree free
3434
#define DEBUG_LOG(x) {}
3535

36+
static Bool CleanFilesOnSuccess(char* in, char* out)
37+
{
38+
// `in` and `out` will always be non-NULL at this point
39+
DbgFree(in);
40+
DbgFree(out);
41+
return TRUE;
42+
}
43+
44+
static Bool CleanFilesOnError(char* in, char* out)
45+
{
46+
if (in) DbgFree(in);
47+
if (out) DbgFree(out);
48+
return FALSE;
49+
}
50+
3651
Bool DecompressFile (char *infile, char *outfile)
3752
{
3853
UnsignedInt rawSize = 0, compressedSize = 0;
@@ -70,9 +85,7 @@ Bool DecompressFile (char *infile, char *outfile)
7085

7186
if (( inBlock == NULL ) || ( outBlock == NULL ))
7287
{
73-
if (inBlock) DbgFree(inBlock);
74-
if (outBlock) DbgFree(outBlock);
75-
return FALSE;
88+
return CleanFilesOnError(inBlock, outBlock);
7689
}
7790

7891
// Read in a big chunk o file
@@ -110,15 +123,11 @@ Bool DecompressFile (char *infile, char *outfile)
110123
}
111124
else
112125
{
113-
if (inBlock) DbgFree(inBlock);
114-
if (outBlock) DbgFree(outBlock);
115-
return FALSE;
126+
return CleanFilesOnError(inBlock, outBlock);
116127
}
117128

118129
// Clean up this mess
119-
DbgFree(inBlock);
120-
DbgFree(outBlock);
121-
return TRUE;
130+
return CleanFilesOnSuccess(inBlock, outBlock);
122131
} // End of if fileptr
123132

124133
return FALSE;
@@ -140,8 +149,6 @@ Bool CompressFile (char *infile, char *outfile)
140149

141150
if (( infile == NULL ) || ( outfile == NULL ))
142151
{
143-
if (infile) DbgFree(infile);
144-
if (outfile) DbgFree(outfile);
145152
return FALSE;
146153
}
147154

@@ -160,9 +167,7 @@ Bool CompressFile (char *infile, char *outfile)
160167

161168
if (( inBlock == NULL ) || ( outBlock == NULL ))
162169
{
163-
DbgFree(inBlock);
164-
DbgFree(outBlock);
165-
return FALSE;
170+
return CleanFilesOnError(inBlock, outBlock);
166171
}
167172

168173
// Read in a big chunk o file
@@ -191,15 +196,11 @@ Bool CompressFile (char *infile, char *outfile)
191196
}
192197
else
193198
{
194-
DbgFree(inBlock);
195-
DbgFree(outBlock);
196-
return FALSE;
199+
return CleanFilesOnError(inBlock, outBlock);
197200
}
198201

199202
// Clean up
200-
DbgFree(inBlock);
201-
DbgFree(outBlock);
202-
return TRUE;
203+
return CleanFilesOnSuccess(inBlock, outBlock);
203204
}
204205

205206
return FALSE;

0 commit comments

Comments
 (0)