MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / loadFile

Method loadFile

src/csvwindow.cpp:332–464  ·  view source on GitHub ↗

* Loads file from 'filename'. Any checks if window objects is in use and so on has to be done before. * Returns true, when the file could be loaded. */

Source from the content-addressed store, hash-verified

330 * Returns true, when the file could be loaded.
331 */
332bool CsvWindow::loadFile(std::string filename, bool askUser, bool reopen) {
333 std::ifstream input;
334 long fileLength;
335 std::stringstream sstr;
336 CsvParser *parser = new CsvParser();
337 std::pair<CsvDefinition::Encodings, int> guessedEncoding;
338 std::pair<CsvDefinition, float> guessedDefinition;
339 CsvDefinition definition;
340 std::map<long,long> histogram;
341
342 if( app.isAlreadyOpened(filename) && !reopen ) {
343 CsvApplication::myFlChoice("", "File is already open!", {"Okay"});
344 return false;
345 }
346
347 input.open(filename);
348 if( !input ) {
349 CsvApplication::myFlChoice("", "Could not open file!", {"Okay"});
350 return false;
351 }
352
353 // Length of file: needed for guessEncoding
354 fileLength = Helper::getFileSize(filename);
355
356 // guess properties
357 guessedDefinition = app.guessDefinition(&input);
358 definition = guessedDefinition.first;
359 guessedEncoding = CsvApplication::guessEncoding(&input, fileLength);
360 definition.encoding = guessedEncoding.first;
361 definition.bomBytes = guessedEncoding.second;
362
363 if( guessedEncoding.first == CsvDefinition::ENC_NONE || guessedDefinition.second < 0.6 || askUser ) {
364 definition = CsvApplication::setTypeByUser(definition, &input, "Open");
365 if( definition.cancelled ) {
366 return false;
367 }
368 }
369
370 // Switch off custom header row
371 if( table->customHeaderRowShown() ) {
372 showHeaderCheckbox->clear();
373 table->switchHeader();
374 updateTable();
375 }
376
377 // Start timing
378 time_t startTime = std::time(0);
379
380 // Tabelle leeren und geparste Daten laden
381 table->clearTable();
382 app.showImWorkingWindow("Opening file ...", true);
383 histogram = parser->parseCsvStream(&input, table->getStorage(), &definition);
384 app.hideImWorkingWindow();
385 table->updateInternals();
386 if( table->getNumberRows() == 0 || table->getNumberCols() == 0 ) {
387 if( askUser ) {
388 CsvApplication::myFlChoice("", "Could not open file with the choosen CSV definition!", {"Okay"});
389 } else {

Callers 1

openFileMethod · 0.80

Calls 15

checkFunction · 0.85
to_stringFunction · 0.85
isAlreadyOpenedMethod · 0.80
openMethod · 0.80
guessDefinitionMethod · 0.80
customHeaderRowShownMethod · 0.80
switchHeaderMethod · 0.80
clearTableMethod · 0.80
showImWorkingWindowMethod · 0.80
parseCsvStreamMethod · 0.80
hideImWorkingWindowMethod · 0.80
updateInternalsMethod · 0.80

Tested by

no test coverage detected