Skip to content

Commit

Permalink
Merge pull request BlueQuartzSoftware#57 from imikejackson/develop
Browse files Browse the repository at this point in the history
Reverting previous commits to LineCounterObject.
  • Loading branch information
imikejackson authored May 21, 2017
2 parents e5cf53b + d0c9b7f commit bb16dbf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 172 deletions.
183 changes: 51 additions & 132 deletions Source/SVWidgetsLib/Widgets/ImportASCIIDataWizard/LineCounterObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@

#include "LineCounterObject.h"

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <fstream>
#include <iostream>
#include <istream>
#include <string>

#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QTextStream>

Expand Down Expand Up @@ -84,6 +72,10 @@ LineCounterObject::~LineCounterObject()
// -----------------------------------------------------------------------------
void LineCounterObject::run()
{
// Validate that the file is an ASCII file
int64_t bufferSize = 262144;
char* buffer;
int64_t result;

// Obtain the file size
if(m_FilePath.isEmpty())
Expand All @@ -95,149 +87,76 @@ void LineCounterObject::run()
QFile qFile(m_FilePath);
int64_t fileSize = qFile.size();

// auto start = std::chrono::system_clock::now();

m_NumOfLines = 0;

FILE* fp = nullptr;
std::vector<char> line;
size_t length = 0;
int64_t read = 0;

fp = fopen(m_FilePath.toStdString().c_str(), "r");
if(fp == NULL)
// Open the file
if(qFile.open(QIODevice::ReadOnly) == false)
{
QString errorStr = "Error: Unable to open file \"" + m_FilePath + "\"";
qDebug() << errorStr;
m_NumOfLines = -1;
emit finished();
fputs(errorStr.toStdString().c_str(), stderr);
return;
}

int64_t currentByte = 0;
int64_t progIncrement = fileSize / 1000;
int64_t currentThresh = progIncrement;

while((read = parseLine(line, length, fp, '\n')) != -1)
int64_t actualSize;
if(fileSize <= bufferSize)
{
currentByte += read;
m_NumOfLines++;

if(currentByte > currentThresh)
{
double progress = static_cast<double>(currentByte) / static_cast<double>(fileSize) * 100;
emit progressUpdateGenerated(progress);
currentThresh = currentThresh + progIncrement;
}
actualSize = fileSize;
}

fclose(fp);
// std::cout << "Number of Lines: " << m_NumOfLines << std::endl;
// auto end = std::chrono::system_clock::now();

// std::chrono::duration<double> diff = end - start;
// std::cout << "Millis to Read: " << diff.count() << std::endl;

emit finished();
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int LineCounterObject::getNumberOfLines()
{
return m_NumOfLines;
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int LineCounterObject::parseLine(std::vector<char> &line, size_t &n, FILE* stream, char terminator)
{
static const int k_MinChunk = 64;
int ncharsAvailable;
char* readPos;
int ret;

if(nullptr == stream)
else
{
errno = EINVAL;
return -1;
actualSize = bufferSize;
}

if(line.size() == 0)
// Allocate the buffer
buffer = (char*)malloc(sizeof(char) * actualSize);
if(buffer == nullptr)
{
n = k_MinChunk;
line.resize(n);
QString errorStr = "Error: Unable to allocate memory to read in data from \"" + m_FilePath + "\"";
fputs(errorStr.toStdString().c_str(), stderr);
return;
}

ncharsAvailable = n;
readPos = line.data();

for(;;)
m_NumOfLines = 0;
int64_t currentByte = 0;
while(qFile.atEnd() == false)
{
int save_errno;
int c = getc(stream);

save_errno = errno;
// Copy the file contents into the buffer
result = qFile.read(buffer, actualSize);

/* We always want at least one char left in the buffer, since we
always (unless we get an error while reading the first char)
NUL-terminate the line buffer. */

assert((line.data() + n) == (readPos + ncharsAvailable));
if(ncharsAvailable < 2)
// Check the buffer for new lines and carriage returns
int64_t fiveThresh = fileSize / 20.0;
int64_t currentThresh = fiveThresh;
for(int i = 0; i < result; i++)
{
if(n > k_MinChunk)
currentByte++;
if(currentByte > currentThresh)
{
n *= 2;
}
else {
n += k_MinChunk;
double progress = static_cast<double>(currentByte) / static_cast<double>(fileSize) * 100;
emit progressUpdateGenerated(progress);
currentThresh = currentThresh + fiveThresh;
}
ncharsAvailable = n + line.data() - readPos;
line.resize(n);
if(line.size())
{
errno = ENOMEM;
return -1;
}
readPos = n - ncharsAvailable + line.data();
assert((line.data() + n) == (readPos + ncharsAvailable));
}

if(ferror(stream))
{
/* Might like to return partial line, but there is no
place for us to store errno. And we don't want to just
lose errno. */
errno = save_errno;
return -1;
}
char currentChar = buffer[i];

if(c == EOF)
{
/* Return partial line, if any. */
if(readPos == line.data())
if(currentChar == '\n')
{
return -1;
} else {
break;
m_NumOfLines++;
}
else if(qFile.atEnd() && currentByte == fileSize)
{
m_NumOfLines++;
}
}

*readPos++ = c;
ncharsAvailable--;

if(c == terminator)
{
break;
}
}

/* Done - NUL terminate and return the number of chars read. */
*readPos = '\0';
// Close the file and free the memory from the buffer
qFile.close();
free(buffer);

emit finished();
}

ret = readPos - (line.data());
return ret;
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int LineCounterObject::getNumberOfLines()
{
return m_NumOfLines;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,54 +43,58 @@

class LineCounterObject : public QObject
{
Q_OBJECT

public:

/**
* @brief LineCounterObject
* @param filePath
* @param parent
*/
LineCounterObject(const QString &filePath, QObject *parent = nullptr);

/**
* @brief LineCounterObject
* @param filePath
* @param numLines
* @param parent
Q_OBJECT

public:
/**
* @brief LineCounterObject
* @param filePath
* @param parent
*/
LineCounterObject(const QString& filePath, QObject* parent = nullptr);

/**
* @brief LineCounterObject
* @param filePath
* @param numLines
* @param parent
*/
LineCounterObject(const QString& filePath, int numLines, QObject* parent = nullptr);

virtual ~LineCounterObject();

/**
* @brief getNumberOfLines
* @return
*/
LineCounterObject(const QString &filePath, int numLines, QObject *parent = nullptr);
int getNumberOfLines();

virtual ~LineCounterObject();
protected slots:

/**
* @brief run
*/
void run();

int getNumberOfLines();

protected slots:
void run();
signals:

signals:
void finished();
void progressUpdateGenerated(double percentage);
/**
* @brief finished
*/
void finished();

private:
QString m_FilePath;
int m_NumOfLines;
/**
* @brief progressUpdateGenerated
* @param percentage
*/
void progressUpdateGenerated(double percentage);

/**
* @brief parseLine
* @param line buffer of char to read into
* @param n
* @param stream A FILE* pointer
* @param terminator How to chop the lines up
* @param offset
* @return Return the number of characters read (not including the null terminator), or -1 on error or EOF.
*/
int parseLine(std::vector<char> &line, size_t &n, FILE* stream, char terminator);
private:
QString m_FilePath;
int m_NumOfLines;

LineCounterObject(const LineCounterObject&); // Copy Constructor Not Implemented
void operator=(const LineCounterObject&); // Operator '=' Not Implemented
LineCounterObject(const LineCounterObject&); // Copy Constructor Not Implemented
void operator=(const LineCounterObject&); // Operator '=' Not Implemented
};

#endif /* _linecounterobject_h_ */

0 comments on commit bb16dbf

Please sign in to comment.