Skip to content

Commit ec285cc

Browse files
committed
Add initial CSV append redirect
1 parent ea372e8 commit ec285cc

9 files changed

Lines changed: 163 additions & 11 deletions

ExtFileSystem/CSVAppendParser.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "CSVAppendParser.h"
2+
3+
CSVAppendParser::CSVAppendParser(ICsvParser *original_parser) : original_parser(original_parser), cols(0), rows(0),
4+
values(nullptr)
5+
{
6+
}
7+
8+
void CSVAppendParser::clear_append_data()
9+
{
10+
dispose(true);
11+
}
12+
13+
void CSVAppendParser::initialize_csv(std::vector<std::wstring> const &values)
14+
{
15+
clear_append_data();
16+
}
17+
18+
ICsvParser *CSVAppendParser::dispose(bool disposing)
19+
{
20+
delete[] values;
21+
values = nullptr;
22+
23+
original_parser->dispose(disposing);
24+
25+
if (disposing)
26+
delete this;
27+
return this;
28+
}
29+
30+
void CSVAppendParser::get_as_bytes(int col, int row, void *dest, int size)
31+
{
32+
original_parser->get_as_bytes(col, row, dest, size);
33+
}
34+
35+
int CSVAppendParser::copy_str(int col, int row, std::string *str)
36+
{
37+
return original_parser->copy_str(col, row, str);
38+
}
39+
40+
void CSVAppendParser::get_as_string(int col, int row, void *dest, int size)
41+
{
42+
original_parser->get_as_string(col, row, dest, size);
43+
}
44+
45+
int CSVAppendParser::get_as_int(int col, int row)
46+
{
47+
return original_parser->get_as_int(col, row);
48+
}
49+
50+
float CSVAppendParser::get_as_float(int col, int row)
51+
{
52+
return original_parser->get_as_float(col, row);
53+
}
54+
55+
int CSVAppendParser::get_cell_strlen(int col, int row)
56+
{
57+
return original_parser->get_cell_strlen(col, row);
58+
}
59+
60+
int CSVAppendParser::get_cell_byte_length(int col, int row)
61+
{
62+
return original_parser->get_cell_byte_length(col, row);
63+
}
64+
65+
bool CSVAppendParser::is_valid_cell(int col, int row)
66+
{
67+
return original_parser->is_valid_cell(col, row);
68+
}
69+
70+
bool CSVAppendParser::is_valid()
71+
{
72+
return original_parser->is_valid();
73+
}
74+
75+
int CSVAppendParser::get_cols()
76+
{
77+
return original_parser->get_cols();
78+
}
79+
80+
int CSVAppendParser::get_rows()
81+
{
82+
return original_parser->get_rows();
83+
}
84+
85+
int CSVAppendParser::get_as_bool(int col, int row)
86+
{
87+
return original_parser->get_as_bool(col, row);
88+
}

ExtFileSystem/CSVAppendParser.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "ICsvParser.h"
4+
#include <vector>
5+
6+
class CSVAppendParser : public ICsvParser
7+
{
8+
public:
9+
CSVAppendParser(ICsvParser *original_parser);
10+
11+
void clear_append_data();
12+
void initialize_csv(std::vector<std::wstring> const &values);
13+
ICsvParser *dispose(bool disposing) override;
14+
void get_as_bytes(int col, int row, void *dest, int size) override;
15+
int copy_str(int col, int row, std::string *str) override;
16+
void get_as_string(int col, int row, void *dest, int size) override;
17+
int get_as_int(int col, int row) override;
18+
float get_as_float(int col, int row) override;
19+
int get_cell_strlen(int col, int row) override;
20+
int get_cell_byte_length(int col, int row) override;
21+
bool is_valid_cell(int col, int row) override;
22+
bool is_valid() override;
23+
int get_cols() override;
24+
int get_rows() override;
25+
int get_as_bool(int col, int row) override;
26+
27+
ICsvParser* original_parser;
28+
29+
private:
30+
int cols, rows;
31+
char *values;
32+
};

ExtFileSystem/CSVParserHooks.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "FileMemory.h"
55
#include "hook.h"
66
#include "NamedFile.h"
7+
#include "ProxyFileSystem.h"
8+
#include "CSVAppendParser.h"
79

810
struct CSVParserData
911
{
@@ -12,19 +14,36 @@ struct CSVParserData
1214
size_t data_length;
1315
};
1416

15-
DEF_HOOK(CSVParserData*, DLL_CSV_CreateCsvParser)
16-
{
17-
return nullptr;
18-
}
19-
2017
DEF_HOOK(bool, DLL_CSV_Open, CSVParserData* parser_data, FileMemory* file)
2118
{
2219
auto named_file = dynamic_cast<NamedFile*>(file);
23-
2420
if (!named_file)
2521
return DLL_CSV_Open_original(parser_data, file);
2622

27-
23+
auto append_parser = dynamic_cast<CSVAppendParser*>(parser_data->csv_parser);
24+
auto append_paths = csv_append_paths.find(named_file->filename);
25+
26+
if (append_paths == csv_append_paths.end())
27+
{
28+
if (append_parser)
29+
{
30+
append_parser->clear_append_data();
31+
parser_data->csv_parser = append_parser->original_parser;
32+
}
33+
auto orig = DLL_CSV_Open_original(parser_data, file);
34+
if (append_parser)
35+
parser_data->csv_parser = append_parser;
36+
37+
return orig;
38+
}
39+
40+
auto orig = DLL_CSV_Open_original(parser_data, file);
41+
42+
if (!append_parser)
43+
append_parser = new CSVAppendParser(parser_data->csv_parser);
44+
append_parser->initialize_csv(append_paths->second);
45+
46+
parser_data->csv_parser = append_parser;
2847

29-
return true;
48+
return orig;
3049
}

ExtFileSystem/ExtFileSystem.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
</Link>
241241
</ItemDefinitionGroup>
242242
<ItemGroup>
243+
<ClInclude Include="CSVAppendParser.h" />
243244
<ClInclude Include="CSVParserHooks.h" />
244245
<ClInclude Include="FileSystemHooks.h" />
245246
<ClInclude Include="heap.h" />
@@ -252,6 +253,7 @@
252253
<ClInclude Include="WindowsFile.h" />
253254
</ItemGroup>
254255
<ItemGroup>
256+
<ClCompile Include="CSVAppendParser.cpp" />
255257
<ClCompile Include="Main.cpp" />
256258
<ClCompile Include="NamedFile.cpp" />
257259
<ClCompile Include="ProxyFileSystem.cpp" />

ExtFileSystem/ExtFileSystem.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<Filter Include="Header Files\Hooks">
2929
<UniqueIdentifier>{9fdf6ea0-642f-4bba-9131-fe8f2625db96}</UniqueIdentifier>
3030
</Filter>
31+
<Filter Include="Source Files\CSV">
32+
<UniqueIdentifier>{987c6393-5965-488c-8197-25c2e0eaf97b}</UniqueIdentifier>
33+
</Filter>
3134
</ItemGroup>
3235
<ItemGroup>
3336
<ClInclude Include="logging.h">
@@ -60,6 +63,9 @@
6063
<ClInclude Include="CSVParserHooks.h">
6164
<Filter>Header Files\Hooks</Filter>
6265
</ClInclude>
66+
<ClInclude Include="CSVAppendParser.h">
67+
<Filter>Header Files\CSV</Filter>
68+
</ClInclude>
6369
</ItemGroup>
6470
<ItemGroup>
6571
<ClCompile Include="Main.cpp">
@@ -74,5 +80,8 @@
7480
<ClCompile Include="NamedFile.cpp">
7581
<Filter>Source Files\FilePointers</Filter>
7682
</ClCompile>
83+
<ClCompile Include="CSVAppendParser.cpp">
84+
<Filter>Source Files\CSV</Filter>
85+
</ClCompile>
7786
</ItemGroup>
7887
</Project>

ExtFileSystem/ICsvParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class ICsvParser // NOLINT(hicpp-special-member-functions)
88
{
99
protected:
10-
virtual ~ICsvParser() = default;
10+
~ICsvParser() = default;
1111
public:
1212
virtual ICsvParser* dispose(bool disposing) = 0;
1313
virtual void get_as_bytes(int col, int row, void* dest, int size) = 0;

ExtFileSystem/Main.cpp

-74 Bytes
Binary file not shown.

ExtFileSystem/NamedFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "NamedFile.h"
22

3-
NamedFile::NamedFile(FileMemory *wrapped, wchar_t *filename)
3+
NamedFile::NamedFile(FileMemory *wrapped, wchar_t const *filename)
44
{
55
this->wrapped = wrapped;
66
this->filename = filename;

ExtFileSystem/ProxyFileSystem.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ std::vector<std::wstring> *CreateListWide(FileSystemArchiveNative *fs, std::vect
7878
LOG_SELF(self, "Number of files: " << std::dec << result->size());
7979

8080
for (auto &s : *result)
81-
LOG_SELF(self, "Got file: " << narrow(s));
81+
{
82+
LOG_SELF(self, "Got file: " << narrow(s));
83+
}
8284

8385
auto search_path = self->proxy_path / path;
8486

0 commit comments

Comments
 (0)