Skip to content

Commit 3b88920

Browse files
committed
adding bit option to libtest main
1 parent ba142cc commit 3b88920

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ makelib: lzw10pack.o lzw10unpack.o common.o
2121
ar rcs liblzw10.a lzw10pack.o lzw10unpack.o common.o
2222

2323
libtest : libtest.cpp
24-
$(CC) $(CFLAGS) -DLIBTEST_MAIN -o lzw_test libtest.cpp $(CLIBS) -L. -llzw10
24+
$(CC) $(CFLAGS) -o lzw_test libtest.cpp $(CLIBS) -L. -llzw10
2525

2626
.PHONY: clean
2727

common.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ int tmpnam_s(char* temp_name, size_t sizeInChars)
7676
if (NULL == tmpnam(buffer))
7777
return -1;
7878

79-
/* if (strlen (mktemp (buffer)) == 0)
80-
return errno; */
81-
8279
if (strlen (buffer) >= sizeInChars)
8380
{
8481
return ERANGE;
@@ -94,4 +91,17 @@ int tmpnam_s(char* temp_name, size_t sizeInChars)
9491
return 0;
9592
}
9693

94+
int strcpy_s(char *dest, size_t dest_len, const char *src)
95+
{
96+
if (!src || !dest)
97+
return EINVAL;
98+
99+
if (strlen (src) >= dest_len)
100+
return ERANGE;
101+
102+
strcpy (dest, src);
103+
104+
return 0;
105+
}
106+
97107
#endif // _MSC_VER

common.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ char *str_dup (const char *s);
2929

3030
#ifndef _MSC_VER
3131
int tmpnam_s(char* temp_name, size_t sizeInChars);
32+
int strcpy_s(char *dest, size_t dest_len, const char *src);
3233
#endif

libtest.cpp

+41-8
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,52 @@
1313
#include <cstring>
1414
#include <cstdlib>
1515
#include <cstdio>
16+
#include <cassert>
1617
#include "export.h"
1718

1819
#include <chrono>
1920
#include <iostream>
2021

2122
#include "common.h"
2223

24+
#if defined(__linux__)
25+
#define LIBTEST_MAIN
26+
#endif
27+
2328
#ifdef LIBTEST_MAIN // add LIBTEST #define to compile the test with static library
2429

2530
int main (int argc, char *argv[])
2631
{
27-
const char presetInput[] = "sample.txt";
32+
int bits = DEFAULT_MAX_BITS;
33+
bool bits_set = false;
34+
35+
char inputFile [PATH_MAX] = { 0 };
2836

29-
const char *inputFile = presetInput;
37+
char compressedFile [PATH_MAX] = { 0 };
3038

31-
char compressedFile [PATH_MAX], outputFile [PATH_MAX];
39+
char outputFile [PATH_MAX] = { 0 };
3240

33-
if (argc == 2)
41+
if (2 == argc)
42+
{
43+
strcpy_s (inputFile, PATH_MAX, argv[1]);
44+
}
45+
else if (3 == argc)
3446
{
35-
inputFile = argv[1];
47+
if (0 == strncmp (argv[1], "-b", 2))
48+
{
49+
char *ptr = argv[1];
50+
bits = atoi (ptr + 2);
51+
52+
bits_set = true;
53+
54+
strcpy_s(inputFile, PATH_MAX, argv[2]);
55+
}
56+
}
57+
58+
if (strlen (inputFile) == 0 || (bits < 9 || bits > SUPPORTED_MAX_BITS))
59+
{
60+
printf ("Usage: %s [-b{12-16}] fileToCompress\n", argv[0]);
61+
return EXIT_FAILURE;
3662
}
3763

3864
printf ("Testing compression on: %s\n", inputFile);
@@ -41,13 +67,20 @@ int main (int argc, char *argv[])
4167

4268
tmpnam_s (outputFile, sizeof(outputFile));
4369

70+
assert (strlen(compressedFile) > 0 && strlen (outputFile) > 0);
71+
4472
std::chrono::high_resolution_clock::time_point start;
4573

4674
start = std::chrono::high_resolution_clock::now();
4775

48-
int ret = Compress (inputFile, compressedFile, VERBOSE_OUTPUT);
76+
int ret = 0;
77+
78+
if (!bits_set)
79+
ret = Compress (inputFile, compressedFile, VERBOSE_OUTPUT);
80+
else
81+
ret = Compress2 (inputFile, compressedFile, VERBOSE_OUTPUT, bits);
4982

50-
printf ("Compression : %s.\n", ret ? "Successful" : "Failed");
83+
printf ("Compression %s.\n", ret ? "successful" : "failed");
5184

5285
if (!ret)
5386
return EXIT_FAILURE;
@@ -61,7 +94,7 @@ int main (int argc, char *argv[])
6194

6295
ret = Decompress (compressedFile, outputFile, VERBOSE_OUTPUT);
6396

64-
printf ("Decompression : %s.\n", ret ? "Successful" : "Failed");
97+
printf ("Decompression %s.\n", ret ? "successful" : "failed");
6598

6699
if (!ret)
67100
return EXIT_FAILURE;

0 commit comments

Comments
 (0)