Регистрация | Вход
#pragma once#define _CRT_SECURE_NO_WARNINGS#include <cstdlib>#include <cstdio>#include <fstream>#include "vectors.h"namespace csv{ char* readTextFile(const char* filename) { char* fcontent = NULL; FILE* f; fopen_s(&f, filename, "rb"); if (f) { fseek(f, 0, SEEK_END); long fsize = ftell(f); fseek(f, 0, SEEK_SET); fcontent = (char*)malloc(fsize + 1); fread(fcontent, fsize, 1, f); fclose(f); fcontent[fsize] = 0; } return fcontent; } double atof2(char* s, char* e) { long double res = 0; int sign = 1, powflag = 0; unsigned long long pow = 1; while (s != e) { if(*s == '-') sign = -1; else if (*s >= '0' && *s <= '9') { res = res * 10 + (*s - '0'); if (powflag) pow *= 10; } else if (*s == '.') powflag = 1; s++; } return sign * (res / pow); } int tokCount(char* s, char tok) { int count = 0; while (*s != '\0') { if (*s == tok) count++; s++; } return count; } int tokCount2(char* s, char* e, char tok) { int count = 0; while (s != e) { if (*s == tok) count++; s++; } return count; } vectors read_csv(const char* path) { char* text = readTextFile(path); int linesCount = tokCount(text, '\n'); char *lineStart = text, *lineEnd = lineStart; double** res = new double*[linesCount]; int line = 0, symbol = 0; while (*lineEnd != '\0') { if (*lineEnd == '\n') { int digCount = tokCount2(lineStart, lineEnd, ',') + 1; res[line] = new double[digCount]; char *digitEnd = lineStart, *digitStart = lineStart; symbol = 0; while (digitEnd != lineEnd) { if (*digitEnd == ',' || *(digitEnd + 1) == '\n')// { if (*digitEnd == ',') res[line][symbol] = atof2(digitStart, digitEnd); else res[line][symbol] = atof2(digitStart, digitEnd + 1); digitStart = digitEnd; symbol++; } digitEnd++; } lineStart = lineEnd; line++; } lineEnd++; } delete[] text; return vectors(res, line, symbol); }}
vector<vector<double>> parseCsv(string path, char token){ ifstream read(path); vector<string> lines; string line; while (getline(read, line)) lines.push_back(line); vector<vector<double>> result; for (int i = 0; i < lines.size(); ++i) { stringstream test(lines[i]); vector<double> seglist; string segment; while (getline(test, segment, token)) seglist.push_back(stod(segment)); result.push_back(seglist); } return result;}
#pragma once#include <string>#include "vectors.h"#include <fstream>#include <ppl.h>namespace iotools{ using namespace std; char* copychar(const char* beg, const char* end) { int l = end - beg; char* res = new char[l + 1]; const char* flow = beg; for (int i = 0; i < l + 1; ++i) res[i] = *flow++; res[l] = '\0'; return res; } char** split(const string& text, char tok, int& len) { const auto begin = text.data(); const auto endend = text.data() + text.size(); auto start = begin; auto end = start; char** lines = new char*[text.size()]; int i = 0; while (end != endend) { if (*end == tok) { lines[i] = copychar(start, end); start = end + 1; ++i; } else if (end == endend - 1) { lines[i] = copychar(start, end + 1); ++i; } ++end; } len = i; return lines; } double s2d(char* str) { int d1 = 0, d2 = 0, e = 0, sig = 1; auto tmp = str; if (tmp) { if (*tmp == '-') { sig = -1; ++tmp; } while (*tmp != '\0' && *tmp >= '0' && *tmp <= '9') { if (*tmp == '.') break; d1 = d1 * 10 + *tmp++ - '0'; } if (*tmp == '.') { ++tmp; while (*tmp != '\0' && *tmp >= '0' && *tmp <= '9' && e < 8) { d2 = d2 * 10 + *tmp++ - '0'; ++e; } } } return sig * (d1 + d2 / pow(10, e)); } void delchararr(char** arr, int len) { for (int i = 0; i < len; ++i) delete[] arr[i]; delete[] arr; } vectors CsvParser(string path) { ifstream t(path, ios::binary | ios::in); t.seekg(0, ios::end); const int length = t.tellg(); t.seekg(0, ios::beg); char* buffer = new char[length](); t.read(buffer, length); t.close(); int split1Len = 0, split2Len = 0; auto split1 = split(buffer, '\n', split1Len); auto s2 = split(split1[0], ',', split2Len); double** body = new double*[split1Len]; Concurrency::parallel_for(0, split1Len, [&](int i) { auto split2 = split(split1[i], ',', split2Len); body[i] = new double[split2Len](); for (int j = 0; j < split2Len; ++j) body[i][j] = s2d(split2[j]); delchararr(split2, split2Len); }); delchararr(split1, split1Len); delchararr(s2, split2Len); delete[] buffer; return vectors(body, split1Len, split2Len); }}