Skip to content

Commit

Permalink
TSVParser: add reset
Browse files Browse the repository at this point in the history
  • Loading branch information
zzjjzzgggg committed Dec 17, 2019
1 parent eab56a2 commit c76faaa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)



find_package(gflags REQUIRED)

include_directories(lz4/lib)
Expand Down
5 changes: 5 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

* Introduction

A collection of C++

* install

git clone --recursive [email protected]:zzjjzzgggg/cpplib.git
Expand Down
14 changes: 11 additions & 3 deletions io/ioutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ std::unique_ptr<IOIn> getIOIn(const std::string& filename);

class TSVParser {
private:
size_t line_NO_;
char split_ch_;
char buf_[1024]; // assume each line has a maximum length of 1024 bytes.
size_t line_NO_;
std::string filename_;

// assume each line has a maximum length of 1024 bytes.
char buf_[1024];
std::vector<std::string> field_vec_;
std::unique_ptr<IOIn> in_ptr_;

public:
TSVParser(const std::string& filename, const char sep = '\t')
: line_NO_(0), split_ch_(sep) {
: split_ch_(sep), line_NO_(0), filename_(filename) {
in_ptr_ = getIOIn(filename);
}

void reset() {
line_NO_ = 0;
in_ptr_ = getIOIn(filename_);
}

bool next();

int getNumFields() const { return (int)(field_vec_.size()); }
Expand Down
2 changes: 1 addition & 1 deletion io/lz4io.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ class LZ4In : public IOIn {

void decompress(const char* output_file_name);
};
}
} // namespace ioutils

#endif /* __LZ4IO_H__ */
18 changes: 15 additions & 3 deletions test/test_ioutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

#include <cstdio>
#include "ioutils.h"
#include "osutils.h"
#include "../io/ioutils.h"
#include "../os/osutils.h"

void test_null_ptr() {
// auto ptr = std::unique_ptr<ioutils::IOIn>();
Expand All @@ -27,7 +27,19 @@ void test_print() {
ioutils::printMap<int, int>(m);
}

void test_tsvparser() {
printf("use file test.dat\n");
ioutils::TSVParser ss("test.dat");
while (ss.next()) printf("%d ", ss.get<int>(0));
printf("\n");

ss.reset();
while (ss.next()) printf("%d ", ss.get<int>(0));
printf("\n");
}

int main(int argc, char* argv[]) {
test_null_ptr();
// test_null_ptr();
test_tsvparser();
return 0;
}

0 comments on commit c76faaa

Please sign in to comment.