Skip to content

Commit

Permalink
Merge pull request #230 from private-octopus/extract-ip-stats
Browse files Browse the repository at this point in the history
Add option to load CSV file.
  • Loading branch information
huitema authored Mar 7, 2024
2 parents 4f47c4d + 5956d6f commit 0991437
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 17 deletions.
8 changes: 8 additions & 0 deletions ithiunit/unittest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ namespace ithiunit
Assert::AreEqual(ret, true);
}

TEST_METHOD(IPStatsCsv)
{
IPStatsCsvTest test;
bool ret = test.DoTest();

Assert::AreEqual(ret, true);
}

TEST_METHOD(IPStatsLoad)
{
IPStatsLoadTest test;
Expand Down
58 changes: 42 additions & 16 deletions lib/ipstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ParseCsvCell(char const* line, size_t* index, size_t* index_first, size_t*
size_t i = *index;

/* Skip the first blanks */
while (line[i] == ' ' || line[i] == '\t' || line[i] != '\r' || line[i] != '\n') {
while (line[i] == ' ' || line[i] == '\t' || line[i] == '\r' || line[i] == '\n') {
i++;
}
/* Skip until the next blank or comma */
Expand All @@ -51,7 +51,7 @@ void ParseCsvCell(char const* line, size_t* index, size_t* index_first, size_t*
}
*index_last = i;
/* Skip the blanks until the comma */
while (line[i] == ' ' || line[i] == '\t' || line[i] != '\r' || line[i] != '\n') {
while (line[i] == ' ' || line[i] == '\t' || line[i] == '\r' || line[i] == '\n') {
i++;
}
/* Skip the comma if present */
Expand Down Expand Up @@ -508,6 +508,9 @@ bool IPStats::LoadInputFiles(size_t nb_files, char const** fileNames)
ret = LoadCborCxFile(fileNames[i]);
}
/* If ends with ".csv", load as csv file */
else if (ithi_endswith(fileNames[i], ".csv")) {
ret = LoadCsvFile(fileNames[i]);
}
}

return ret;
Expand Down Expand Up @@ -553,26 +556,49 @@ bool IPStats::LoadCborCxFile(char const* fileName)
return ret;
}

bool IPStats::SaveToCsv(char const* file_name)
bool IPStats::LoadCsvFile(char const* file_name)
{
bool ret = true;
FILE* F;
#ifdef _WINDOWS
errno_t err = fopen_s(&F, file_name, "wt");
if (err != 0) {
if (F != NULL) {
fclose(F);
F = NULL;
}
int last_err = 0;
FILE* F = ithi_file_open_ex(file_name, "rt", &last_err);

if (F == NULL) {
fprintf(stderr, "Cannot open input file %s\n", file_name);
ret = false;
}
#else
F = fopen(file_name, "wt");
else {
char buf[1024];

ret &= (F != NULL);
#endif
while (fgets(buf, 1024, F) != NULL) {
IPStatsRecord* ipsr = IPStatsRecord::ParseLine(buf);

if (ipsr != NULL) {
bool ipsr_stored = false;
this->ip_records.InsertOrAdd(ipsr, false, &ipsr_stored);
if (!ipsr_stored) {
delete ipsr;
}
}
else {
fprintf(stderr, "Cannot parse: %s", buf);
}
}

fclose(F);
}
return ret;
}

bool IPStats::SaveToCsv(char const* file_name)
{
bool ret = true;
int last_err = 0;
FILE* F = ithi_file_open_ex(file_name, "wt", &last_err);

if (F != NULL) {
if (F == NULL) {
ret = false;
}
else {
/* Enumerate the records in the binhash, store the keys in a vector */
std::vector<IPStatsRecord*> records(ip_records.GetCount());
size_t record_index = 0;
Expand Down
1 change: 1 addition & 0 deletions lib/ipstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class IPStats
bool LoadInputFiles(size_t nb_files, char const** fileNames);
bool LoadCborFile(char const* fileName);
bool LoadCborCxFile(char const* fileName);
bool LoadCsvFile(char const* fileName);

bool SaveToCsv(char const* file_name);

Expand Down
18 changes: 17 additions & 1 deletion test/IPStatsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static char const * ipstats_test_output = "data/ipstats-tiny-ref.csv";
static char const * ipstats_xz_test_input = "data/tiny-capture.cbor.xz";
#endif
static char const* ip_stats_csv = "tiny-capture-ipstats.csv";
static char const* ip_stats_csv_csv = "tiny-capture-ipstats-csv.csv";
static char const* ip_stats_xz_csv = "tiny-capture-ipstats-xz.csv";


Expand Down Expand Up @@ -89,6 +90,21 @@ bool IPStatsTest::DoTest()
return ret;
}

IPStatsCsvTest::IPStatsCsvTest()
{}

IPStatsCsvTest::~IPStatsCsvTest()
{}

bool IPStatsCsvTest::DoTest()
{
IPStats ipstats;
char const * list[1] = { ipstats_test_output };

bool ret = IPStatsTestOne(ip_stats_csv_csv, ipstats_test_output, list, 1);

return ret;
}

IPStatsXZTest::IPStatsXZTest()
{}
Expand All @@ -101,7 +117,7 @@ bool IPStatsXZTest::DoTest()
IPStats ipstats;
char const * list[1] = { ipstats_xz_test_input };

bool ret = IPStatsTestOne(ip_stats_xz_csv, /* TODO: change?*/ ipstats_test_output, list, 1);
bool ret = IPStatsTestOne(ip_stats_xz_csv, ipstats_test_output, list, 1);

return ret;
}
Expand Down
9 changes: 9 additions & 0 deletions test/IPStatsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class IPStatsTest : public ithi_test_class
bool DoTest() override;
};

class IPStatsCsvTest : public ithi_test_class
{
public:
IPStatsCsvTest();
~IPStatsCsvTest();

bool DoTest() override;
};

class IPStatsXZTest : public ithi_test_class
{
public:
Expand Down
7 changes: 7 additions & 0 deletions test/ithi_test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ enum test_list_enum {
test_enum_TrailingZeroes,
test_enum_HyperLogLog,
test_enum_IPStats,
test_enum_IPStatsCsv,
test_enum_IPStatsLoad,
test_enum_IPStatsXZ,
test_enum_max_number,
Expand Down Expand Up @@ -180,6 +181,8 @@ char const * ithi_test_class::GetTestName(int number)
return("HyperLogLog");
case test_enum_IPStats:
return("IPStats");
case test_enum_IPStatsCsv:
return("IPStatsCsv");
case test_enum_IPStatsLoad:
return("IPStatsLoad");
case test_enum_IPStatsXZ:
Expand Down Expand Up @@ -336,11 +339,15 @@ ithi_test_class * ithi_test_class::TestByNumber(int number)
case test_enum_IPStats:
test = new IPStatsTest();
break;
case test_enum_IPStatsCsv:
test = new IPStatsCsvTest();
break;
case test_enum_IPStatsLoad:
test = new IPStatsLoadTest();
break;
case test_enum_IPStatsXZ:
test = new IPStatsXZTest();
break;
default:
break;
}
Expand Down

0 comments on commit 0991437

Please sign in to comment.