Add a function to parse CSV files to multidimensional arrays.

This commit is contained in:
Snowdream 2022-07-25 00:38:42 -04:00 committed by Nathan-MV
parent ef90a2f30b
commit 7c066151ff
3 changed files with 1925 additions and 0 deletions

View file

@ -41,6 +41,7 @@
#include "eventthread.h" #include "eventthread.h"
#include <vector> #include <vector>
#include "util/rapidcsv.h"
extern "C" { extern "C" {
#include <ruby.h> #include <ruby.h>
@ -152,6 +153,8 @@ RB_METHOD(mkxpStringToUTF8Bang);
VALUE json2rb(json5pp::value const &v); VALUE json2rb(json5pp::value const &v);
json5pp::value rb2json(VALUE v); json5pp::value rb2json(VALUE v);
RB_METHOD(mkxpParseCSV);
static void mriBindingInit() { static void mriBindingInit() {
tableBindingInit(); tableBindingInit();
etcBindingInit(); etcBindingInit();
@ -249,6 +252,8 @@ static void mriBindingInit() {
_rb_define_module_function(mod, "default_font_family=", mkxpSetDefaultFontFamily); _rb_define_module_function(mod, "default_font_family=", mkxpSetDefaultFontFamily);
_rb_define_module_function(mod, "parse_csv", mkxpParseCSV);
_rb_define_method(rb_cString, "to_utf8", mkxpStringToUTF8); _rb_define_method(rb_cString, "to_utf8", mkxpStringToUTF8);
_rb_define_method(rb_cString, "to_utf8!", mkxpStringToUTF8Bang); _rb_define_method(rb_cString, "to_utf8!", mkxpStringToUTF8Bang);
@ -688,6 +693,32 @@ RB_METHOD_GUARD(mkxpLaunch) {
} }
RB_METHOD_GUARD_END RB_METHOD_GUARD_END
RB_METHOD(mkxpParseCSV) {
RB_UNUSED_PARAM;
VALUE str;
rb_scan_args(argc, argv, "1", &str);
SafeStringValue(str);
VALUE ret = rb_ary_new();
std::stringstream stream(RSTRING_PTR(str));
try {
rapidcsv::Document doc(stream);
for (int r = 0; r < doc.GetRowCount(); r++) {
VALUE col = rb_ary_new();
for (int c = 0; c < doc.GetColumnCount(); c++) {
std::string str = doc.GetCell<std::string>(c, r);
rb_ary_push(col, rb_utf8_str_new(str.c_str(), str.length()));
}
rb_ary_push(ret, col);
}
} catch (std::exception &e) {
raiseRbExc(Exception(Exception::MKXPError, "Failed to parse CSV: %s", e.what()));
}
return ret;
}
json5pp::value loadUserSettings() { json5pp::value loadUserSettings() {
json5pp::value ret; json5pp::value ret;
VALUE cpath = rb_utf8_str_new_cstr(shState->config().userConfPath.c_str()); VALUE cpath = rb_utf8_str_new_cstr(shState->config().userConfPath.c_str());

View file

@ -1141,6 +1141,7 @@
96573E7A27913B46002C3E77 /* TouchBar.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = TouchBar.mm; path = views/TouchBar.mm; sourceTree = "<group>"; }; 96573E7A27913B46002C3E77 /* TouchBar.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = TouchBar.mm; path = views/TouchBar.mm; sourceTree = "<group>"; };
96573E7B27913B46002C3E77 /* TouchBar.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; name = TouchBar.h; path = views/TouchBar.h; sourceTree = "<group>"; }; 96573E7B27913B46002C3E77 /* TouchBar.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; name = TouchBar.h; path = views/TouchBar.h; sourceTree = "<group>"; };
96573E80279152DC002C3E77 /* TouchBar.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = TouchBar.xcassets; path = views/TouchBar.xcassets; sourceTree = "<group>"; }; 96573E80279152DC002C3E77 /* TouchBar.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = TouchBar.xcassets; path = views/TouchBar.xcassets; sourceTree = "<group>"; };
968CB90E288E46F4008BC75D /* rapidcsv.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = rapidcsv.h; sourceTree = "<group>"; };
96D8EDD028728DCA00A331EA /* gamecontrollerdb.txt */ = {isa = PBXFileReference; lastKnownFileType = text; name = gamecontrollerdb.txt; path = ../assets/gamecontrollerdb.txt; sourceTree = "<group>"; }; 96D8EDD028728DCA00A331EA /* gamecontrollerdb.txt */ = {isa = PBXFileReference; lastKnownFileType = text; name = gamecontrollerdb.txt; path = ../assets/gamecontrollerdb.txt; sourceTree = "<group>"; };
A8AA4AE49BB66C97EFAE3055 /* libbrotlicommon-static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libbrotlicommon-static.a"; path = "Dependencies/build-macosx-universal/lib/libbrotlicommon-static.a"; sourceTree = "<group>"; }; A8AA4AE49BB66C97EFAE3055 /* libbrotlicommon-static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libbrotlicommon-static.a"; path = "Dependencies/build-macosx-universal/lib/libbrotlicommon-static.a"; sourceTree = "<group>"; };
CBEA4C45BE737EE0FF5A8A4C /* bicubic.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; name = bicubic.frag; path = ../shader/bicubic.frag; sourceTree = "<group>"; }; CBEA4C45BE737EE0FF5A8A4C /* bicubic.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; name = bicubic.frag; path = ../shader/bicubic.frag; sourceTree = "<group>"; };
@ -1457,6 +1458,7 @@
3B012198261544A0001E574A /* string-util.h */, 3B012198261544A0001E574A /* string-util.h */,
3B10ED402568E95D00372D13 /* util.h */, 3B10ED402568E95D00372D13 /* util.h */,
3B10EE1F2569348E00372D13 /* json5pp.hpp */, 3B10EE1F2569348E00372D13 /* json5pp.hpp */,
968CB90E288E46F4008BC75D /* rapidcsv.h */,
); );
path = util; path = util;
sourceTree = "<group>"; sourceTree = "<group>";

1892
src/util/rapidcsv.h Normal file

File diff suppressed because it is too large Load diff