| 555 | |
| 556 | |
| 557 | bool CsvApplication::splitCsvFiles() { |
| 558 | int winIndex = getTopWindow(); |
| 559 | bool retOkay, mySaveState = false; |
| 560 | long numOfRowsPerFile = 0; |
| 561 | long numOfRows = windows[winIndex].table->getNumberRows(); |
| 562 | long fromRow, toRow; |
| 563 | std::ifstream fileExistsTest; |
| 564 | std::string numOfRowsPerFileStr, filename, msg; |
| 565 | std::string pathWithoutExtension,extension; |
| 566 | // TODO Return if no file has been opened or saved so far |
| 567 | // TODO Ask if numOfFiles gets to big |
| 568 | |
| 569 | std::tie(retOkay, numOfRowsPerFileStr) = myFlAskString("Number of lines per file", "Split"); |
| 570 | if( retOkay ) { |
| 571 | try { |
| 572 | if( numOfRowsPerFileStr.find_first_not_of("0123456789") == std::string::npos ) { |
| 573 | numOfRowsPerFile = std::stoi(numOfRowsPerFileStr); |
| 574 | } else { |
| 575 | numOfRowsPerFile = 0; |
| 576 | } |
| 577 | } catch(...) {} // intentional empty clause |
| 578 | if( numOfRowsPerFile > 0 ) { |
| 579 | int numOfFiles = (int) std::ceil((double)numOfRows / numOfRowsPerFile); |
| 580 | int digitalExtensionLength = std::ceil(std::log10(numOfFiles)); |
| 581 | if( numOfFiles > 1 ) { |
| 582 | std::tie(pathWithoutExtension,extension) = Helper::getPathWithoutExtension(windows[winIndex].getPath()); |
| 583 | // Test if one of the splitted files already exists |
| 584 | bool splitFileExists = false; |
| 585 | for(int i=0; i<numOfFiles; ++i) { |
| 586 | filename = splittedFileName(pathWithoutExtension, extension, i, digitalExtensionLength); |
| 587 | fileExistsTest.open(filename.c_str()); |
| 588 | if( fileExistsTest.good() ) { |
| 589 | splitFileExists = true; |
| 590 | msg = "File " + Helper::getBasename(filename) + " does already exist!"; |
| 591 | myFlChoice("Error", msg, {"Okay"}); |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | if( !splitFileExists ) { |
| 596 | showImWorkingWindow("Splitting CSV file ..."); |
| 597 | for(int i=0; i<numOfFiles; ++i) { |
| 598 | fromRow = i * numOfRowsPerFile; |
| 599 | if( i < numOfFiles -1 ) { |
| 600 | toRow = fromRow + numOfRowsPerFile - 1; |
| 601 | } else { |
| 602 | toRow = -1; |
| 603 | } |
| 604 | filename = splittedFileName(pathWithoutExtension, extension, i, digitalExtensionLength); |
| 605 | mySaveState = windows[winIndex].table->saveCsv(filename, &CsvWindow::updateStatusbarCB, &windows[winIndex], false, fromRow, toRow); |
| 606 | } |
| 607 | hideImWorkingWindow(); |
| 608 | msg = "The splitted CSV files have been stored. (No of Files: "+std::to_string(numOfFiles)+")"; |
| 609 | myFlChoice("Files stored", msg, {"Okay"}); |
| 610 | } |
| 611 | } else { |
| 612 | myFlChoice("Error", "The number of rows per splitted file has to be lower than the number of rows in the original file.", {"Okay"}); |
| 613 | } |
| 614 |
no test coverage detected