开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
开源项目 > 程序开发 > 编程语言/脚本语言 &&
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
13 Star 69 Fork 22

johnsonyl/cpps

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
标签 (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
master
分支 (1)
标签 (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
标签 (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
cpps
/
libs
/
csv
/
csv.h
cpps
/
libs
/
csv
/
csv.h
csv.h 60.25 KB
一键复制 编辑 原始数据 按行查看 历史
johnsonyl 提交于 2021年02月18日 14:08 +08:00 . 2021年02月18日 更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
// Copyright: (2012-2015) Ben Strasser <code@ben-strasser.net>
// License: BSD-3
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#ifndef CSV_H
#define CSV_H
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <exception>
#ifndef CSV_IO_NO_THREAD
#include <mutex>
#include <thread>
#include <condition_variable>
#endif
#include <memory>
#include <cassert>
#include <cerrno>
#include <istream>
#include <cpps/cpps.h>
namespace cpps{
////////////////////////////////////////////////////////////////////////////
// LineReader //
////////////////////////////////////////////////////////////////////////////
namespace csv_error{
struct base : std::exception{
virtual void format_error_message()const = 0;
const char*what()const noexcept override{
format_error_message();
return error_message_buffer;
}
mutable char error_message_buffer[512];
};
const int max_file_name_length = 255;
struct with_file_name{
with_file_name(){
std::memset(file_name, 0, sizeof(file_name));
}
void set_file_name(const char*file_name){
if(file_name != nullptr){
// This call to strncpy has parenthesis around it
// to silence the GCC -Wstringop-truncation warning
(strncpy(this->file_name, file_name, sizeof(this->file_name)));
this->file_name[sizeof(this->file_name)-1] = '0円';
}else{
this->file_name[0] = '0円';
}
}
char file_name[max_file_name_length+1];
};
struct with_file_line{
with_file_line(){
file_line = -1;
}
void set_file_line(int file_line){
this->file_line = file_line;
}
int file_line;
};
struct with_errno{
with_errno(){
errno_value = 0;
}
void set_errno(int errno_value){
this->errno_value = errno_value;
}
int errno_value;
};
struct can_not_open_file :
base,
with_file_name,
with_errno{
void format_error_message()const override{
if(errno_value != 0)
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Can not open file \"%s\" because \"%s\"."
, file_name, std::strerror(errno_value));
else
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Can not open file \"%s\"."
, file_name);
}
};
struct line_length_limit_exceeded :
base,
with_file_name,
with_file_line{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Line number %d in file \"%s\" exceeds the maximum length of 2^24-1."
, file_line, file_name);
}
};
}
class ByteSourceBase{
public:
virtual int read(char*buffer, int size)=0;
virtual ~ByteSourceBase(){}
};
namespace detail{
class OwningStdIOByteSourceBase : public ByteSourceBase{
public:
explicit OwningStdIOByteSourceBase(FILE*file):file(file){
// Tell the std library that we want to do the buffering ourself.
std::setvbuf(file, 0, _IONBF, 0);
}
int read(char*buffer, int size){
return (int)std::fread(buffer, 1, size, file);
}
~OwningStdIOByteSourceBase(){
std::fclose(file);
}
private:
FILE*file;
};
class NonOwningIStreamByteSource : public ByteSourceBase{
public:
explicit NonOwningIStreamByteSource(std::istream&in):in(in){}
int read(char*buffer, int size){
in.read(buffer, size);
return (int)in.gcount();
}
~NonOwningIStreamByteSource(){}
private:
std::istream&in;
};
class NonOwningStringByteSource : public ByteSourceBase{
public:
NonOwningStringByteSource(const char*str, long long size):str(str), remaining_byte_count(size){}
int read(char*buffer, int desired_byte_count){
int to_copy_byte_count = desired_byte_count;
if(remaining_byte_count < to_copy_byte_count)
to_copy_byte_count = (int)remaining_byte_count;
std::memcpy(buffer, str, to_copy_byte_count);
remaining_byte_count -= to_copy_byte_count;
str += to_copy_byte_count;
return to_copy_byte_count;
}
~NonOwningStringByteSource(){}
private:
const char*str;
long long remaining_byte_count;
};
#ifndef CSV_IO_NO_THREAD
class AsynchronousReader{
public:
void init(std::unique_ptr<ByteSourceBase>arg_byte_source){
std::unique_lock<std::mutex>guard(lock);
byte_source = std::move(arg_byte_source);
desired_byte_count = -1;
termination_requested = false;
worker = std::thread(
[&]{
std::unique_lock<std::mutex>guard(lock);
try{
for(;;){
read_requested_condition.wait(
guard,
[&]{
return desired_byte_count != -1 || termination_requested;
}
);
if(termination_requested)
return;
read_byte_count = byte_source->read(buffer, desired_byte_count);
desired_byte_count = -1;
if(read_byte_count == 0)
break;
read_finished_condition.notify_one();
}
}catch(...){
read_error = std::current_exception();
}
read_finished_condition.notify_one();
}
);
}
bool is_valid()const{
return byte_source != nullptr;
}
void start_read(char*arg_buffer, int arg_desired_byte_count){
std::unique_lock<std::mutex>guard(lock);
buffer = arg_buffer;
desired_byte_count = arg_desired_byte_count;
read_byte_count = -1;
read_requested_condition.notify_one();
}
int finish_read(){
std::unique_lock<std::mutex>guard(lock);
read_finished_condition.wait(
guard,
[&]{
return read_byte_count != -1 || read_error;
}
);
if(read_error)
std::rethrow_exception(read_error);
else
return read_byte_count;
}
~AsynchronousReader(){
if(byte_source != nullptr){
{
std::unique_lock<std::mutex>guard(lock);
termination_requested = true;
}
read_requested_condition.notify_one();
worker.join();
}
}
private:
std::unique_ptr<ByteSourceBase>byte_source;
std::thread worker;
bool termination_requested;
std::exception_ptr read_error;
char*buffer;
int desired_byte_count;
int read_byte_count;
std::mutex lock;
std::condition_variable read_finished_condition;
std::condition_variable read_requested_condition;
};
#endif
class SynchronousReader{
public:
void init(std::unique_ptr<ByteSourceBase>arg_byte_source){
byte_source = std::move(arg_byte_source);
}
bool is_valid()const{
return byte_source != nullptr;
}
void start_read(char*arg_buffer, int arg_desired_byte_count){
buffer = arg_buffer;
desired_byte_count = arg_desired_byte_count;
}
int finish_read(){
return byte_source->read(buffer, desired_byte_count);
}
private:
std::unique_ptr<ByteSourceBase>byte_source;
char*buffer;
int desired_byte_count;
};
}
class LineReader{
private:
static const int block_len = 1<<20;
std::unique_ptr<char[]>buffer; // must be constructed before (and thus destructed after) the reader!
#ifdef CSV_IO_NO_THREAD
detail::SynchronousReader reader;
#else
detail::AsynchronousReader reader;
#endif
int data_begin;
int data_end;
char file_name[csv_error::max_file_name_length+1];
unsigned file_line;
static std::unique_ptr<ByteSourceBase> open_file(const char*file_name){
// We open the file in binary mode as it makes no difference under *nix
// and under Windows we handle \r\n newlines ourself.
FILE*file = std::fopen(file_name, "rb");
if(file == 0){
int x = errno; // store errno as soon as possible, doing it after constructor call can fail.
csv_error::can_not_open_file err;
err.set_errno(x);
err.set_file_name(file_name);
throw err;
}
return std::unique_ptr<ByteSourceBase>(new detail::OwningStdIOByteSourceBase(file));
}
void init(std::unique_ptr<ByteSourceBase>byte_source){
file_line = 0;
buffer = std::unique_ptr<char[]>(new char[3*block_len]);
data_begin = 0;
data_end = byte_source->read(buffer.get(), 2*block_len);
// Ignore UTF-8 BOM
if(data_end >= 3 && buffer[0] == '\xEF' && buffer[1] == '\xBB' && buffer[2] == '\xBF')
data_begin = 3;
if(data_end == 2*block_len){
reader.init(std::move(byte_source));
reader.start_read(buffer.get() + 2*block_len, block_len);
}
}
public:
LineReader() = delete;
LineReader(const LineReader&) = delete;
LineReader&operator=(const LineReader&) = delete;
explicit LineReader(const char*file_name){
set_file_name(file_name);
init(open_file(file_name));
}
explicit LineReader(const std::string&file_name){
set_file_name(file_name.c_str());
init(open_file(file_name.c_str()));
}
LineReader(const char*file_name, std::unique_ptr<ByteSourceBase>byte_source){
set_file_name(file_name);
init(std::move(byte_source));
}
LineReader(const std::string&file_name, std::unique_ptr<ByteSourceBase>byte_source){
set_file_name(file_name.c_str());
init(std::move(byte_source));
}
LineReader(const char*file_name, const char*data_begin, const char*data_end){
set_file_name(file_name);
init(std::unique_ptr<ByteSourceBase>(new detail::NonOwningStringByteSource(data_begin, data_end-data_begin)));
}
LineReader(const std::string&file_name, const char*data_begin, const char*data_end){
set_file_name(file_name.c_str());
init(std::unique_ptr<ByteSourceBase>(new detail::NonOwningStringByteSource(data_begin, data_end-data_begin)));
}
LineReader(const char*file_name, FILE*file){
set_file_name(file_name);
init(std::unique_ptr<ByteSourceBase>(new detail::OwningStdIOByteSourceBase(file)));
}
LineReader(const std::string&file_name, FILE*file){
set_file_name(file_name.c_str());
init(std::unique_ptr<ByteSourceBase>(new detail::OwningStdIOByteSourceBase(file)));
}
LineReader(const char*file_name, std::istream&in){
set_file_name(file_name);
init(std::unique_ptr<ByteSourceBase>(new detail::NonOwningIStreamByteSource(in)));
}
LineReader(const std::string&file_name, std::istream&in){
set_file_name(file_name.c_str());
init(std::unique_ptr<ByteSourceBase>(new detail::NonOwningIStreamByteSource(in)));
}
void set_file_name(const std::string&file_name){
set_file_name(file_name.c_str());
}
void set_file_name(const char*file_name){
if(file_name != nullptr){
strncpy(this->file_name, file_name, sizeof(this->file_name)-1);
this->file_name[sizeof(this->file_name)-1] = '0円';
}else{
this->file_name[0] = '0円';
}
}
const char*get_truncated_file_name()const{
return file_name;
}
void set_file_line(unsigned file_line){
this->file_line = file_line;
}
unsigned get_file_line()const{
return file_line;
}
char*next_line(){
if(data_begin == data_end)
return nullptr;
++file_line;
assert(data_begin < data_end);
assert(data_end <= block_len*2);
if(data_begin >= block_len){
std::memcpy(buffer.get(), buffer.get()+block_len, block_len);
data_begin -= block_len;
data_end -= block_len;
if(reader.is_valid())
{
data_end += reader.finish_read();
std::memcpy(buffer.get()+block_len, buffer.get()+2*block_len, block_len);
reader.start_read(buffer.get() + 2*block_len, block_len);
}
}
int line_end = data_begin;
while(buffer[line_end] != '\n' && line_end != data_end){
++line_end;
}
if(line_end - data_begin + 1 > block_len){
csv_error::line_length_limit_exceeded err;
err.set_file_name(file_name);
err.set_file_line(file_line);
throw err;
}
if(buffer[line_end] == '\n' && line_end != data_end){
buffer[size_t(line_end)] = '0円';
}else{
// some files are missing the newline at the end of the
// last line
++data_end;
buffer[size_t(line_end)] = '0円';
}
// handle windows \r\n-line breaks
if(line_end != data_begin && buffer[size_t(size_t(line_end)-1)] == '\r')
buffer[size_t(size_t(line_end) - 1)] = '0円';
char*ret = buffer.get() + data_begin;
data_begin = line_end+1;
return ret;
}
};
////////////////////////////////////////////////////////////////////////////
// CSV //
////////////////////////////////////////////////////////////////////////////
namespace csv_error{
const int max_column_name_length = 63;
struct with_column_name{
with_column_name(){
std::memset(column_name, 0, max_column_name_length+1);
}
void set_column_name(const char*column_name){
if(column_name != nullptr){
std::strncpy(this->column_name, column_name, max_column_name_length);
this->column_name[max_column_name_length] = '0円';
}else{
this->column_name[0] = '0円';
}
}
char column_name[max_column_name_length+1];
};
const int max_column_content_length = 63;
struct with_column_content{
with_column_content(){
std::memset(column_content, 0, max_column_content_length+1);
}
void set_column_content(const char*column_content){
if(column_content != nullptr){
std::strncpy(this->column_content, column_content, max_column_content_length);
this->column_content[max_column_content_length] = '0円';
}else{
this->column_content[0] = '0円';
}
}
char column_content[max_column_content_length+1];
};
struct extra_column_in_header :
base,
with_file_name,
with_column_name{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(Extra column "%s" in header of file "%s".)"
, column_name, file_name);
}
};
struct missing_column_in_header :
base,
with_file_name,
with_column_name{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(Missing column "%s" in header of file "%s".)"
, column_name, file_name);
}
};
struct duplicated_column_in_header :
base,
with_file_name,
with_column_name{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(Duplicated column "%s" in header of file "%s".)"
, column_name, file_name);
}
};
struct header_missing :
base,
with_file_name{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Header missing in file \"%s\"."
, file_name);
}
};
struct too_few_columns :
base,
with_file_name,
with_file_line{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Too few columns in line %d in file \"%s\"."
, file_line, file_name);
}
};
struct too_many_columns :
base,
with_file_name,
with_file_line{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Too many columns in line %d in file \"%s\"."
, file_line, file_name);
}
};
struct escaped_string_not_closed :
base,
with_file_name,
with_file_line{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
"Escaped string was not closed in line %d in file \"%s\"."
, file_line, file_name);
}
};
struct integer_must_be_positive :
base,
with_file_name,
with_file_line,
with_column_name,
with_column_content{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(The integer "%s" must be positive or 0 in column "%s" in file "%s" in line "%d".)"
, column_content, column_name, file_name, file_line);
}
};
struct no_digit :
base,
with_file_name,
with_file_line,
with_column_name,
with_column_content{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(The integer "%s" contains an invalid digit in column "%s" in file "%s" in line "%d".)"
, column_content, column_name, file_name, file_line);
}
};
struct integer_overflow :
base,
with_file_name,
with_file_line,
with_column_name,
with_column_content{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(The integer "%s" overflows in column "%s" in file "%s" in line "%d".)"
, column_content, column_name, file_name, file_line);
}
};
struct integer_underflow :
base,
with_file_name,
with_file_line,
with_column_name,
with_column_content{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(The integer "%s" underflows in column "%s" in file "%s" in line "%d".)"
, column_content, column_name, file_name, file_line);
}
};
struct invalid_single_character :
base,
with_file_name,
with_file_line,
with_column_name,
with_column_content{
void format_error_message()const override{
std::snprintf(error_message_buffer, sizeof(error_message_buffer),
R"(The content "%s" of column "%s" in file "%s" in line "%d" is not a single character.)"
, column_content, column_name, file_name, file_line);
}
};
}
using ignore_column = unsigned int;
static const ignore_column ignore_no_column = 0;
static const ignore_column ignore_extra_column = 1;
static const ignore_column ignore_missing_column = 2;
template<char ... trim_char_list>
struct trim_chars{
private:
constexpr static bool is_trim_char(char){
return false;
}
template<class ...OtherTrimChars>
constexpr static bool is_trim_char(char c, char trim_char, OtherTrimChars...other_trim_chars){
return c == trim_char || is_trim_char(c, other_trim_chars...);
}
public:
static void trim(char*&str_begin, char*&str_end){
while(str_begin != str_end && is_trim_char(*str_begin, trim_char_list...))
++str_begin;
while(str_begin != str_end && is_trim_char(*(str_end-1), trim_char_list...))
--str_end;
*str_end = '0円';
}
};
struct no_comment{
static bool is_comment(const char*){
return false;
}
};
template<char ... comment_start_char_list>
struct single_line_comment{
private:
constexpr static bool is_comment_start_char(char){
return false;
}
template<class ...OtherCommentStartChars>
constexpr static bool is_comment_start_char(char c, char comment_start_char, OtherCommentStartChars...other_comment_start_chars){
return c == comment_start_char || is_comment_start_char(c, other_comment_start_chars...);
}
public:
static bool is_comment(const char*line){
return is_comment_start_char(*line, comment_start_char_list...);
}
};
struct empty_line_comment{
static bool is_comment(const char*line){
if(*line == '0円')
return true;
while(*line == ' ' || *line == '\t'){
++line;
if(*line == 0)
return true;
}
return false;
}
};
template<char ... comment_start_char_list>
struct single_and_empty_line_comment{
static bool is_comment(const char*line){
return single_line_comment<comment_start_char_list...>::is_comment(line) || empty_line_comment::is_comment(line);
}
};
template<char sep>
struct no_quote_escape{
static const char*find_next_column_end(const char*col_begin){
while(*col_begin != sep && *col_begin != '0円')
++col_begin;
return col_begin;
}
static void unescape(char*&, char*&){
}
};
template<char sep, char quote>
struct double_quote_escape{
static const char*find_next_column_end(const char*col_begin){
while(*col_begin != sep && *col_begin != '0円')
if(*col_begin != quote)
++col_begin;
else{
do{
++col_begin;
while(*col_begin != quote){
if(*col_begin == '0円')
throw csv_error::escaped_string_not_closed();
++col_begin;
}
++col_begin;
}while(*col_begin == quote);
}
return col_begin;
}
static void unescape(char*&col_begin, char*&col_end){
if(col_end - col_begin >= 2){
if(*col_begin == quote && *(col_end-1) == quote){
++col_begin;
--col_end;
char*out = col_begin;
for(char*in = col_begin; in!=col_end; ++in){
if(*in == quote && (in+1) != col_end && *(in+1) == quote){
++in;
}
*out = *in;
++out;
}
col_end = out;
*col_end = '0円';
}
}
}
};
struct throw_on_overflow{
template<class T>
static void on_overflow(T&){
throw csv_error::integer_overflow();
}
template<class T>
static void on_underflow(T&){
throw csv_error::integer_underflow();
}
};
struct ignore_overflow{
template<class T>
static void on_overflow(T&){}
template<class T>
static void on_underflow(T&){}
};
struct set_to_max_on_overflow{
template<class T>
static void on_overflow(T&x){
// using (std::numeric_limits<T>::max) instead of std::numeric_limits<T>::max
// to make code including windows.h with its max macro happy
x = (std::numeric_limits<T>::max)();
}
template<class T>
static void on_underflow(T&x){
x = (std::numeric_limits<T>::min)();
}
};
namespace detail{
template<class quote_policy>
void chop_next_column(
char*&line, char*&col_begin, char*&col_end
){
assert(line != nullptr);
col_begin = line;
// the col_begin + (... - col_begin) removes the constness
col_end = col_begin + (quote_policy::find_next_column_end(col_begin) - col_begin);
if(*col_end == '0円'){
line = nullptr;
}else{
*col_end = '0円';
line = col_end + 1;
}
}
template<class trim_policy, class quote_policy>
void parse_line(
char*line,
char**sorted_col,
const std::vector<int>&col_order
){
for (int i : col_order) {
if(line == nullptr)
throw ::cpps::csv_error::too_few_columns();
char*col_begin, *col_end;
chop_next_column<quote_policy>(line, col_begin, col_end);
if (i != -1) {
trim_policy::trim(col_begin, col_end);
quote_policy::unescape(col_begin, col_end);
sorted_col[i] = col_begin;
}
}
if(line != nullptr)
throw ::cpps::csv_error::too_many_columns();
}
template< class trim_policy, class quote_policy>
void parse_header_line(
unsigned column_count,
char*line,
std::vector<int>&col_order,
const std::string*col_name,
ignore_column ignore_policy
){
col_order.clear();
std::vector<bool> found; found.resize(column_count);
for (auto it = found.begin(); it != found.end(); ++it) *it = false;
while(line){
char*col_begin,*col_end;
chop_next_column<quote_policy>(line, col_begin, col_end);
trim_policy::trim(col_begin, col_end);
quote_policy::unescape(col_begin, col_end);
for(unsigned i=0; i<column_count; ++i)
if(col_begin == col_name[i]){
if(found[i]){
csv_error::duplicated_column_in_header err;
err.set_column_name(col_begin);
throw err;
}
found[i] = true;
col_order.push_back(i);
col_begin = 0;
break;
}
if(col_begin){
if(ignore_policy & ::cpps::ignore_extra_column)
col_order.push_back(-1);
else{
csv_error::extra_column_in_header err;
err.set_column_name(col_begin);
throw err;
}
}
}
if(!(ignore_policy & ::cpps::ignore_missing_column)){
for(unsigned i=0; i<column_count; ++i){
if(!found[i]){
csv_error::missing_column_in_header err;
err.set_column_name(col_name[i].c_str());
throw err;
}
}
}
}
template<class overflow_policy>
void parse(char*col, char &x){
if(!*col)
throw csv_error::invalid_single_character();
x = *col;
++col;
if(*col)
throw csv_error::invalid_single_character();
}
template<class overflow_policy>
void parse(char*col, std::string&x){
x = col;
}
template<class overflow_policy>
void parse(char*col, const char*&x){
x = col;
}
template<class overflow_policy>
void parse(char*col, char*&x){
x = col;
}
template<class overflow_policy, class T>
void parse_unsigned_integer(const char*col, T&x){
x = 0;
while(*col != '0円'){
if('0' <= *col && *col <= '9'){
T y = *col - '0';
if(x > ((std::numeric_limits<T>::max)()-y)/10){
overflow_policy::on_overflow(x);
return;
}
x = 10*x+y;
}else
throw csv_error::no_digit();
++col;
}
}
template<class overflow_policy>void parse(char*col, unsigned char &x)
{parse_unsigned_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, unsigned short &x)
{parse_unsigned_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, unsigned int &x)
{parse_unsigned_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, unsigned long &x)
{parse_unsigned_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, unsigned long long &x)
{parse_unsigned_integer<overflow_policy>(col, x);}
template<class overflow_policy, class T>
void parse_signed_integer(const char*col, T&x){
if(*col == '-'){
++col;
x = 0;
while(*col != '0円'){
if('0' <= *col && *col <= '9'){
T y = *col - '0';
if(x < ((std::numeric_limits<T>::min)()+y)/10){
overflow_policy::on_underflow(x);
return;
}
x = 10*x-y;
}else
throw csv_error::no_digit();
++col;
}
return;
}else if(*col == '+')
++col;
parse_unsigned_integer<overflow_policy>(col, x);
}
template<class overflow_policy>void parse(char*col, signed char &x)
{parse_signed_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, signed short &x)
{parse_signed_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, signed int &x)
{parse_signed_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, signed long &x)
{parse_signed_integer<overflow_policy>(col, x);}
template<class overflow_policy>void parse(char*col, signed long long &x)
{parse_signed_integer<overflow_policy>(col, x);}
template<class T>
void parse_float(const char*col, T&x){
bool is_neg = false;
if(*col == '-'){
is_neg = true;
++col;
}else if(*col == '+')
++col;
x = 0;
while('0' <= *col && *col <= '9'){
int y = *col - '0';
x *= 10;
x += y;
++col;
}
if(*col == '.'|| *col == ','){
++col;
T pos = 1;
while('0' <= *col && *col <= '9'){
pos /= 10;
int y = *col - '0';
++col;
x += y*pos;
}
}
if(*col == 'e' || *col == 'E'){
++col;
int e;
parse_signed_integer<set_to_max_on_overflow>(col, e);
if(e != 0){
T base;
if(e < 0){
base = T(0.1);
e = -e;
}else{
base = T(10);
}
while(e != 1){
if((e & 1) == 0){
base = base*base;
e >>= 1;
}else{
x *= base;
--e;
}
}
x *= base;
}
}else{
if(*col != '0円')
throw csv_error::no_digit();
}
if(is_neg)
x = -x;
}
template<class overflow_policy> void parse(char*col, float&x) { parse_float(col, x); }
template<class overflow_policy> void parse(char*col, double&x) { parse_float(col, x); }
template<class overflow_policy> void parse(char*col, long double&x) { parse_float(col, x); }
template<class overflow_policy, class T>
void parse(char*col, T&x){
// Mute unused variable compiler warning
(void)col;
(void)x;
// GCC evalutes "false" when reading the template and
// "sizeof(T)!=sizeof(T)" only when instantiating it. This is why
// this strange construct is used.
static_assert(sizeof(T)!=sizeof(T),
"Can not parse this type. Only buildin integrals, floats, char, char*, const char* and std::string are supported");
}
}
template<
class trim_policy = trim_chars<' ', '\t'>,
class quote_policy = no_quote_escape<','>,
class overflow_policy = throw_on_overflow,
class comment_policy = no_comment
>
class CSVReader{
private:
LineReader in;
unsigned column_count;
char**row;
std::string *column_names;
std::vector<int>col_order;
template<class ...ColNames>
void set_column_names(std::string s, ColNames...cols){
column_names[column_count-sizeof...(ColNames)-1] = std::move(s);
set_column_names(std::forward<ColNames>(cols)...);
}
void set_column_names(){}
public:
CSVReader() = delete;
CSVReader(const CSVReader&) = delete;
CSVReader&operator=(const CSVReader&);
~CSVReader() {
if (row) {
delete[] row;
row = NULL;
}
if (column_names) {
delete[] column_names;
column_names = NULL;
}
col_order.clear();
}
void init(unsigned count) {
if (row) return;
column_count = count;
row = new char* [column_count];
column_names = new std::string[column_count];
std::fill(row, row + column_count, nullptr);
col_order.resize(column_count);
for (unsigned i = 0; i < column_count; ++i)
col_order[i] = i;
for (unsigned i = 1; i <= column_count; ++i)
column_names[i - 1] = "col" + std::to_string(i);
}
template<class ...Args>
explicit CSVReader(Args&&...args):in(std::forward<Args>(args)...){
row = NULL;
column_count = 0;
column_names = NULL;
}
char*next_line(){
return in.next_line();
}
void read_header(ignore_column ignore_policy, std::vector<std::string> &list){
init((cpps::usint32)list.size());
try{
size_t idx = 0;
for (auto &s : list){
column_names[idx++] = s;
}
char*line;
do{
line = in.next_line();
if(!line)
throw csv_error::header_missing();
}while(comment_policy::is_comment(line));
detail::parse_header_line
< trim_policy, quote_policy>
(column_count,line, col_order, column_names, ignore_policy);
}catch(csv_error::with_file_name&err){
err.set_file_name(in.get_truncated_file_name());
throw;
}
}
template<class ...ColNames>
void read_header(ignore_column ignore_policy, ColNames...cols){
init((cpps::usint32)(sizeof...(ColNames)));
try{
set_column_names(std::forward<ColNames>(cols)...);
char*line;
do{
line = in.next_line();
if(!line)
throw csv_error::header_missing();
}while(comment_policy::is_comment(line));
detail::parse_header_line
< trim_policy, quote_policy>
(column_count,line, col_order, column_names, ignore_policy);
}catch(csv_error::with_file_name&err){
err.set_file_name(in.get_truncated_file_name());
throw;
}
}
void set_header(std::vector<std::string>& list){
init((cpps::usint32)list.size());
size_t idx = 0;
for (auto& s : list) {
column_names[idx++] = s;
}
std::fill(row, row + column_count, nullptr);
col_order.resize(column_count);
for (unsigned i = 0; i < column_count; ++i)
col_order[i] = i;
}
template<class ...ColNames>
void set_header(ColNames...cols) {
init((cpps::usint32)(sizeof...(ColNames)));
set_column_names(std::forward<ColNames>(cols)...);
std::fill(row, row + column_count, nullptr);
col_order.resize(column_count);
for (unsigned i = 0; i < column_count; ++i)
col_order[i] = i;
}
bool has_column(const std::string&name) const {
for (size_t i = 0; i < column_count; i++) {
if (column_names[i] == name) {
return std::end(col_order) != std::find(std::begin(col_order), std::end(col_order), i);
}
}
return false;
}
void set_file_name(const std::string&file_name){
in.set_file_name(file_name);
}
void set_file_name(const char*file_name){
in.set_file_name(file_name);
}
const char*get_truncated_file_name()const{
return in.get_truncated_file_name();
}
void set_file_line(unsigned file_line){
in.set_file_line(file_line);
}
unsigned get_file_line()const{
return in.get_file_line();
}
private:
void parse_helper(std::size_t){}
template<class T, class ...ColType>
void parse_helper(std::size_t r, T&t, ColType&...cols){
if(row[r]){
try{
try{
::cpps::detail::parse<overflow_policy>(row[r], t);
}catch(csv_error::with_column_content&err){
err.set_column_content(row[r]);
throw;
}
}catch(csv_error::with_column_name&err){
err.set_column_name(column_names[r].c_str());
throw;
}
}
parse_helper(r+1, cols...);
}
void parse_helper(std::size_t r, std::vector<std::string> &list) {
if (r < column_count && row[r]) {
try {
try {
::cpps::detail::parse<overflow_policy>(row[r], list[r]);
}
catch (csv_error::with_column_content& err) {
err.set_column_content(row[r]);
throw;
}
}
catch (csv_error::with_column_name& err) {
err.set_column_name(column_names[r].c_str());
throw;
}
parse_helper(r + 1, list);
}
}
public:
bool read_row(std::vector<std::string> &list) {
try {
try {
char* line;
do {
line = in.next_line();
if (!line)
return false;
} while (comment_policy::is_comment(line));
detail::parse_line<trim_policy, quote_policy>
(line, row, col_order);
list.resize(column_count);
parse_helper(0, list);
}
catch (csv_error::with_file_name& err) {
err.set_file_name(in.get_truncated_file_name());
throw;
}
}
catch (csv_error::with_file_line& err) {
err.set_file_line(in.get_file_line());
throw;
}
return true;
}
template<class ...ColType>
bool read_row(ColType& ...cols) {
static_assert(sizeof...(ColType) >= column_count,
"not enough columns specified");
static_assert(sizeof...(ColType) <= column_count,
"too many columns specified");
try {
try {
char* line;
do {
line = in.next_line();
if (!line)
return false;
} while (comment_policy::is_comment(line));
detail::parse_line<trim_policy, quote_policy>
(line, row, col_order);
parse_helper(0, cols...);
}
catch (csv_error::with_file_name& err) {
err.set_file_name(in.get_truncated_file_name());
throw;
}
}
catch (csv_error::with_file_line& err) {
err.set_file_line(in.get_file_line());
throw;
}
return true;
}
};
}
//bindding
namespace cpps {
struct cpps_csvreader
{
virtual ~cpps_csvreader() {
close();
}
void open(std::string csvpath) {
reader = new CSVReader<>(csvpath.c_str());
}
void read_header(C*c,cpps::object headers,object _ignore_column)
{
ignore_column v = ignore_extra_column;
if (_ignore_column.isint()) v = (ignore_column) _ignore_column.toint();
std::vector<std::string> headers_vct;
for (cpps::object cv : cpps::object::vector(headers)) {
headers_vct.push_back(cv.tostring());
}
try{
reader->read_header(v, headers_vct);
}
catch (...){
cpps::error(c, "csverror");
}
}
bool has_column(std::string columnname) {
return reader->has_column(columnname);
}
void write_header(C* c, cpps::object headers)
{
std::vector<std::string> headers_vct;
for (cpps::object cv : cpps::object::vector(headers)) {
headers_vct.push_back(cv.tostring());
}
try {
reader->set_header(headers_vct);
}
catch (...) {
cpps::error(c, "csverror");
}
}
cpps_integer get_file_line() {
return (cpps_integer)reader->get_file_line();
}
void set_file_line(cpps_integer line) {
reader->set_file_line((usint32)line);
}
cpps::tuple read_row(C*c)
{
std::vector<std::string> row_values;
bool has = reader->read_row(row_values);
cpps::tuple ret;
ret.push_back(object::create(c, has));
for (auto& v : row_values)
ret.push_back(object::create(c, v));
return ret;
}
void close() {
if (reader) {
delete reader;
reader = NULL;
}
}
CSVReader<> *reader;
};
}
#endif
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

CPPS是一种轻量级的嵌入式脚本语言,其语法类似于C++。它具有当前主流语言的许多特性,包括协程、面向对象、lambda、闭包、泛型变量、自定义模块支持、GC垃圾收集和跨平台。CPPS将程序解释为字节码,通过内置语法解析在虚拟机中运行
取消

发行版 (3)

全部

开源评估指数源自 OSS-Compass 评估体系,评估体系围绕以下三个维度对项目展开评估:

1. 开源生态

  • 生产力:来评估开源项目输出软件制品和开源价值的能力。
  • 创新力:用于评估开源软件及其生态系统的多样化程度。
  • 稳健性:用于评估开源项目面对多变的发展环境,抵御内外干扰并自我恢复的能力。

2. 协作、人、软件

  • 协作:代表了开源开发行为中协作的程度和深度。
  • 人:观察开源项目核心人员在开源项目中的影响力,并通过第三方视角考察用户和开发者对开源项目的评价。
  • 软件:从开源项目对外输出的制品评估其价值最终落脚点。也是开源评估最"古老"的主流方向之一"开源软件" 的具体表现。

3. 评估模型

    基于"开源生态"与"协作、人、软件"的维度,找到与该目标直接或间接相关的可量化指标,对开源项目健康与生态进行量化评估,最终形成开源评估指数。

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cppscript/cpps.git
git@gitee.com:cppscript/cpps.git
cppscript
cpps
cpps
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /