Skip to content

Commit 6aa3819

Browse files
committed
Some code cleanup. In main, use return not exit (Valgrind)
1 parent b963f68 commit 6aa3819

File tree

4 files changed

+20
-31
lines changed

4 files changed

+20
-31
lines changed

src/Context.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@ limitations under the License.
1717
#ifndef _Context_
1818
#define _Context_
1919

20+
#include <map>
2021
#include <sstream>
2122
#include <string>
2223
#include "concurrent.hpp"
2324
#include "util/strings.hpp"
2425

25-
#if __cplusplus >= 201103L
26-
#include <unordered_map>
27-
#define CTX_MAP std::unordered_map
28-
#else
29-
#include <map>
30-
#define CTX_MAP std::map
31-
#endif
32-
3326
namespace kanzi
3427
{
3528

@@ -75,7 +68,7 @@ namespace kanzi
7568
#endif
7669

7770
private:
78-
CTX_MAP<std::string, ContextVal> _map;
71+
std::map<std::string, ContextVal> _map;
7972

8073
#ifdef CONCURRENCY_ENABLED
8174
ThreadPool* _pool;
@@ -97,7 +90,7 @@ namespace kanzi
9790

9891
inline int64 Context::getLong(const std::string& key, int64 defValue) const
9992
{
100-
CTX_MAP<std::string, ContextVal>::const_iterator it = _map.find(key);
93+
std::map<std::string, ContextVal>::const_iterator it = _map.find(key);
10194

10295
if (it == _map.end())
10396
return defValue;
@@ -108,7 +101,7 @@ namespace kanzi
108101

109102
inline std::string Context::getString(const std::string& key, const std::string& defValue) const
110103
{
111-
CTX_MAP<std::string, ContextVal>::const_iterator it = _map.find(key);
104+
std::map<std::string, ContextVal>::const_iterator it = _map.find(key);
112105

113106
if (it == _map.end())
114107
return defValue;

src/app/Kanzi.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ bool toInt(string& s, int& res)
273273
return true;
274274
}
275275

276-
int processCommandLine(int argc, const char* argv[], Context& map)
276+
int processCommandLine(int argc, const char* argv[], Context& map, Printer& log)
277277
{
278278
string inputName;
279279
string outputName;
@@ -296,7 +296,6 @@ int processCommandLine(int argc, const char* argv[], Context& map)
296296
int blockSize = -1;
297297
int autoBlockSize = -1;
298298
string mode;
299-
Printer log(cout);
300299
bool showHeader = true;
301300
bool showHelp = false;
302301

@@ -910,15 +909,16 @@ int main(int argc, const char* argv[])
910909
#endif
911910

912911
Context args;
913-
int status = processCommandLine(argc, argv, args);
912+
Printer log(cout);
913+
int status = processCommandLine(argc, argv, args, log);
914914

915915
// Command line processing error ?
916916
if (status != 0)
917-
exit(status);
917+
return status;
918918

919919
// Help mode only ?
920920
if (args.has("mode") == false)
921-
exit(0);
921+
return 0;
922922

923923
string mode = args.getString("mode");
924924
int jobs = args.getInt("jobs", -1);
@@ -929,7 +929,6 @@ int main(int argc, const char* argv[])
929929
const int verbosity = args.getInt("verbosity");
930930
stringstream ss;
931931
ss << "Warning: the number of jobs is limited to 1 in this version";
932-
Printer log(cout);
933932
log.println(ss.str(), verbosity > 0);
934933
}
935934

@@ -948,7 +947,6 @@ int main(int argc, const char* argv[])
948947
const int verbosity = args.getInt("verbosity");
949948
stringstream ss;
950949
ss << "Warning: the number of jobs is too high, defaulting to " << MAX_CONCURRENCY;
951-
Printer log(cout);
952950
log.println(ss.str(), verbosity > 0);
953951
jobs = MAX_CONCURRENCY;
954952
}
@@ -968,11 +966,11 @@ int main(int argc, const char* argv[])
968966
BlockCompressor bc(ctx);
969967
uint64 written = 0;
970968
int code = bc.compress(written);
971-
exit(code);
969+
return code;
972970
}
973971
catch (exception& e) {
974972
cerr << "Could not create the compressor: " << e.what() << endl;
975-
exit(Error::ERR_CREATE_COMPRESSOR);
973+
return Error::ERR_CREATE_COMPRESSOR;
976974
}
977975
}
978976

@@ -981,11 +979,11 @@ int main(int argc, const char* argv[])
981979
BlockDecompressor bd(ctx);
982980
uint64 read = 0;
983981
int code = bd.decompress(read);
984-
exit(code);
982+
return code;
985983
}
986984
catch (exception& e) {
987985
cerr << "Could not create the decompressor: " << e.what() << endl;
988-
exit(Error::ERR_CREATE_DECOMPRESSOR);
986+
return Error::ERR_CREATE_DECOMPRESSOR;
989987
}
990988
}
991989

@@ -995,11 +993,11 @@ int main(int argc, const char* argv[])
995993
catch (invalid_argument& e) {
996994
// May be thrown by ThreadPool
997995
cerr << e.what() << endl;
998-
exit(Error::ERR_INVALID_PARAM);
996+
return Error::ERR_INVALID_PARAM;
999997
}
1000998
catch (runtime_error& e) {
1001999
// May be thrown by ThreadPool
10021000
cerr << e.what() << endl;
1003-
exit(Error::ERR_INVALID_PARAM);
1001+
return Error::ERR_INVALID_PARAM;
10041002
}
10051003
}

src/transform/BWTBlockCodec.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ bool BWTBlockCodec::inverse(SliceArray<byte>& input, SliceArray<byte>& output, i
101101

102102
if (_bsVersion > 5) {
103103
// Number of chunks and primary index size in bitstream since bsVersion 6
104-
const byte* src = &input._array[input._index];
105-
byte mode = src[0];
104+
byte mode = input._array[input._index++];
106105
const uint logNbChunks = uint(mode >> 2) & 0x07;
107106
const int pIndexSize = (int(mode) & 0x03) + 1;
108107

@@ -117,24 +116,23 @@ bool BWTBlockCodec::inverse(SliceArray<byte>& input, SliceArray<byte>& output, i
117116
const int headerSize = 1 + chunks * pIndexSize;
118117

119118
if ((input._length < headerSize) || (blockSize < headerSize))
120-
return false;
119+
return false;
121120

122121
// Read header
123-
for (int i = 0, idx = 1; i < chunks; i++) {
122+
for (int i = 0; i < chunks; i++) {
124123
int shift = (pIndexSize - 1) << 3;
125124
int primaryIndex = 0;
126125

127126
// Extract BWT primary index
128127
while (shift >= 0) {
129-
primaryIndex = (primaryIndex << 8) | int(src[idx++]);
128+
primaryIndex = (primaryIndex << 8) | int(input._array[input._index++]);
130129
shift -= 8;
131130
}
132131

133132
if (_pBWT->setPrimaryIndex(i, primaryIndex + 1) == false)
134133
return false;
135134
}
136135

137-
input._index += headerSize;
138136
blockSize -= headerSize;
139137
}
140138
else {

src/util/Printer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace kanzi
3030
{
3131
public:
3232
Printer(std::ostream& os) { _os = &os; }
33-
~Printer() {}
33+
~Printer() { _os->flush(); }
3434

3535
void print(const char* msg, bool print) {
3636
if ((print == true) && (msg != nullptr)) {

0 commit comments

Comments
 (0)