** local csv=[[a,b,"hello, world!","is ""quoted"" back there"]] ** local tbl = db:csv_to_table(csv) ** => #tbl = 4 ** tbl[1] = a ** tbl[2] = b ** tbl[3] = hello, world! ** tbl[4] = is "quoted" back there */
| 3982 | ** tbl[4] = is "quoted" back there |
| 3983 | */ |
| 3984 | static int db_csv_to_table(Lua L) |
| 3985 | { |
| 3986 | luaL_checkudata(L, 1, dbtypes.db); |
| 3987 | lua_remove(L, 1); |
| 3988 | |
| 3989 | CSVReader csv = {0}; |
| 3990 | csv.in = luaL_checkstring(L, 1); |
| 3991 | csv.nLine = 1; |
| 3992 | csv.lua = L; |
| 3993 | csv.cSeparator = ','; |
| 3994 | csv_append_char(&csv, 0); /* To ensure sCsv.z is allocated */ |
| 3995 | |
| 3996 | lua_newtable(L); |
| 3997 | int cols = 0, lines = 0; |
| 3998 | lua_newtable(L); |
| 3999 | while (csv_read_one_field(&csv)) { |
| 4000 | lua_pushstring(L, csv.z); |
| 4001 | lua_rawseti(L, -2, ++cols); |
| 4002 | if (csv.cTerm != csv.cSeparator) { |
| 4003 | lua_rawseti(L, -2, ++lines); |
| 4004 | if (csv.cTerm == 0) break; |
| 4005 | cols = 0; |
| 4006 | lua_newtable(L); |
| 4007 | } |
| 4008 | } |
| 4009 | free(csv.z); |
| 4010 | // Expected Lua stack: |
| 4011 | // 2: Array of parsed values |
| 4012 | // 1: CSV string |
| 4013 | if (lua_gettop(L) > 2) { // clean up any left-overs |
| 4014 | if (cols) { |
| 4015 | lua_rawseti(L, -2, ++lines); |
| 4016 | } else { |
| 4017 | lua_pop(L, 1); //new-line (EOF) |
| 4018 | } |
| 4019 | } |
| 4020 | if (lua_gettop(L) != 2) { |
| 4021 | return luaL_error(L, "csv_to_table failed"); |
| 4022 | } |
| 4023 | if (lines == 1) lua_rawgeti(L, -1, 1); |
| 4024 | return 1; |
| 4025 | } |
| 4026 | |
| 4027 | static int cson_to_table_annotated(Lua, cson_value *, int); |
| 4028 | static int db_json_to_table(Lua lua) |
nothing calls this directly
no test coverage detected