Moved Merge subsystem into its own subdirectory.

This commit is contained in:
Jim Evins
2016-05-22 13:01:48 -04:00
parent 691353e27b
commit b1e7a6507c
26 changed files with 141 additions and 30 deletions
+60
View File
@@ -0,0 +1,60 @@
cmake_minimum_required (VERSION 2.8)
###############################################################################
# gLabels Merge subsystem
###############################################################################
project (Merge CXX)
#=======================================
# Sources
#=======================================
set (merge_sources
Merge.cpp
MergeFactory.cpp
MergeRecord.cpp
MergeNone.cpp
MergeText.cpp
MergeTextCsv.cpp
)
set (merge_qobject_headers
Merge.h
)
set (merge_forms
)
set (merge_resource_files
)
qt4_wrap_cpp (merge_moc_sources ${merge_qobject_headers})
qt4_wrap_ui (merge_forms_headers ${merge_forms})
qt4_add_resources (merge_qrc_sources ${merge_resource_files})
add_library (Merge STATIC
${merge_sources}
${merge_moc_sources}
${merge_qrc_sources}
${merge_forms_headers}
)
#=======================================
# Where to find stuff
#=======================================
include_directories (
)
link_directories (
)
#=======================================
# Subdirectories
#=======================================
#=======================================
# Install
#=======================================
+203
View File
@@ -0,0 +1,203 @@
/* Merge.cpp
*
* Copyright (C) 2015-2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Merge.h"
///
/// Constructor
///
Merge::Merge()
{
}
///
/// Constructor
///
Merge::Merge( const Merge* merge ) : mSource(merge->mSource)
{
foreach ( MergeRecord* record, merge->mRecordList )
{
mRecordList << record->clone();
}
}
///
/// Destructor
///
Merge::~Merge()
{
foreach ( MergeRecord* record, mRecordList )
{
delete record;
}
mRecordList.clear();
}
///
/// Get id
///
QString Merge::id() const
{
return mId;
}
///
/// Get source
///
QString Merge::source() const
{
return mSource;
}
///
/// Set source
///
void Merge::setSource( const QString& source )
{
mSource = source;
// Clear out any old records
foreach ( MergeRecord* record, mRecordList )
{
delete record;
}
mRecordList.clear();
open();
for ( MergeRecord* record = readNextRecord(); record != 0; record = readNextRecord() )
{
mRecordList.append( record );
}
close();
emit sourceChanged();
}
///
/// Get record list
///
const QList<MergeRecord*>& Merge::recordList( void ) const
{
return mRecordList;
}
///
/// Select matching record
///
void Merge::select( MergeRecord* record )
{
record->setSelected( true );
emit selectionChanged();
}
///
/// Unselect matching record
///
void Merge::unselect( MergeRecord* record )
{
record->setSelected( false );
emit selectionChanged();
}
///
/// Select/unselect i'th record
///
void Merge::setSelected( int i, bool state )
{
if ( (i >= 0) && (i < mRecordList.size()) )
{
mRecordList[i]->setSelected( state );
emit selectionChanged();
}
}
///
/// Select all records
///
void Merge::selectAll()
{
foreach ( MergeRecord* record, mRecordList )
{
record->setSelected( true );
}
emit selectionChanged();
}
///
/// Unselect all records
///
void Merge::unselectAll()
{
foreach ( MergeRecord* record, mRecordList )
{
record->setSelected( false );
}
emit selectionChanged();
}
///
/// Return count of selected records
///
int Merge::nSelectedRecords() const
{
int count = 0;
foreach ( MergeRecord* record, mRecordList )
{
if ( record->isSelected() )
{
count++;
}
}
return count;
}
///
/// Return list of selected records
///
const QList<MergeRecord*> Merge::selectedRecords() const
{
QList<MergeRecord*> list;
foreach ( MergeRecord* record, mRecordList )
{
if ( record->isSelected() )
{
list.append( record );
}
}
return list;
}
+112
View File
@@ -0,0 +1,112 @@
/* Merge.h
*
* Copyright (C) 2015-2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Merge_h
#define Merge_h
#include <QObject>
#include <QString>
#include <QStringList>
#include <QList>
#include "MergeRecord.h"
///
/// Merge Object
///
struct Merge : QObject
{
Q_OBJECT
/////////////////////////////////
// Life Cycle
/////////////////////////////////
protected:
Merge();
Merge( const Merge* merge );
public:
virtual ~Merge();
/////////////////////////////////
// Object duplication
/////////////////////////////////
virtual Merge* clone() const = 0;
/////////////////////////////////
// Properties
/////////////////////////////////
public:
QString id() const;
QString source() const;
void setSource( const QString& source );
const QList<MergeRecord*>& recordList( void ) const;
/////////////////////////////////
// Selection methods
/////////////////////////////////
public:
void select( MergeRecord* record );
void unselect( MergeRecord* record );
void setSelected( int i, bool state = true );
void selectAll();
void unselectAll();
int nSelectedRecords() const;
const QList<MergeRecord*> selectedRecords() const;
/////////////////////////////////
// Virtual methods
/////////////////////////////////
public:
virtual QStringList keys() const = 0;
virtual QString primaryKey() const = 0;
protected:
virtual void open() = 0;
virtual void close() = 0;
virtual MergeRecord* readNextRecord() = 0;
/////////////////////////////////
// Signals
/////////////////////////////////
signals:
void sourceChanged();
void selectionChanged();
/////////////////////////////////
// Private data
/////////////////////////////////
protected:
QString mId;
private:
QString mSource;
QList<MergeRecord*> mRecordList;
};
#endif // Merge_h
+176
View File
@@ -0,0 +1,176 @@
/* MergeFactory.cpp
*
* Copyright (C) 2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MergeFactory.h"
#include "MergeNone.h"
#include "MergeTextCsv.h"
///
/// Static data
///
QMap<QString,MergeFactory::BackendEntry> MergeFactory::mBackendIdMap;
QMap<QString,MergeFactory::BackendEntry> MergeFactory::mBackendNameMap;
///
/// Constructor
///
MergeFactory::MergeFactory()
{
registerBackend( MergeNone::id(),
tr("None"),
NONE,
&MergeNone::create );
registerBackend( MergeTextCsv::id(),
tr("Text: Comma Separated Values (CSV)"),
FILE,
&MergeTextCsv::create );
}
///
/// Initialize
///
void MergeFactory::init()
{
static MergeFactory* singletonInstance = 0;
if ( !singletonInstance )
{
singletonInstance = new MergeFactory();
}
}
///
/// Create Merge object
///
Merge* MergeFactory::createMerge( const QString& id )
{
QMap<QString,BackendEntry>::iterator iBackend = mBackendIdMap.find( id );
if ( iBackend != mBackendIdMap.end() )
{
return iBackend->create();
}
return MergeNone::create();
}
///
/// Get name list
///
QStringList MergeFactory::nameList()
{
QStringList list;
foreach ( BackendEntry backend, mBackendIdMap )
{
list << backend.name;
}
return list;
}
///
/// Convert ID to name
///
QString MergeFactory::idToName( const QString& id )
{
if ( mBackendIdMap.contains( id ) )
{
return mBackendIdMap[id].name;
}
else
{
return tr("None");
}
}
///
/// Convert name to ID
///
QString MergeFactory::nameToId( const QString& name )
{
if ( mBackendNameMap.contains( name ) )
{
return mBackendNameMap[name].id;
}
else
{
return "None";
}
}
///
/// Convert ID to type
///
MergeFactory::SourceType MergeFactory::idToType( const QString& id )
{
if ( mBackendIdMap.contains( id ) )
{
return mBackendIdMap[id].type;
}
else
{
return NONE;
}
}
///
/// Lookup ID from index
///
QString MergeFactory::indexToId( int index )
{
QList<QString> ids = mBackendIdMap.keys();
if ( (index > 0) && (index < ids.size()) )
{
return ids[index];
}
return "None";
}
///
/// Register backend
///
void MergeFactory::registerBackend( const QString& id,
const QString& name,
SourceType type,
CreateFct create )
{
BackendEntry backend;
backend.name = name;
backend.type = type;
backend.create = create;
mBackendIdMap[ id ] = backend;
}
+96
View File
@@ -0,0 +1,96 @@
/* MergeFactory.h
*
* Copyright (C) 2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MergeFactory_h
#define MergeFactory_h
#include "Merge.h"
#include <QCoreApplication>
#include <QStringList>
///
/// MergeFactory
///
struct MergeFactory
{
Q_DECLARE_TR_FUNCTIONS(MergeFactory)
/////////////////////////////////
// Source Type
/////////////////////////////////
public:
enum SourceType { NONE, FIXED, FILE };
/////////////////////////////////
// Life Cycle
/////////////////////////////////
protected:
MergeFactory();
/////////////////////////////////
// Static methods
/////////////////////////////////
public:
static void init();
static Merge* createMerge( const QString& id );
static QStringList nameList();
static QString idToName( const QString& id );
static QString nameToId( const QString& name );
static SourceType idToType( const QString& id );
static QString indexToId( int index );
/////////////////////////////////
// private methods
/////////////////////////////////
private:
typedef Merge* (*CreateFct)();
static void registerBackend( const QString& id,
const QString& name,
SourceType type,
CreateFct create );
/////////////////////////////////
// private data
/////////////////////////////////
class BackendEntry
{
public:
QString id;
QString name;
SourceType type;
CreateFct create;
};
static QMap<QString,BackendEntry> mBackendIdMap;
static QMap<QString,BackendEntry> mBackendNameMap;
};
#endif // MergeFactory_h
+117
View File
@@ -0,0 +1,117 @@
/* MergeNone.cpp
*
* Copyright (C) 2015 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MergeNone.h"
///
/// Constructor
///
MergeNone::MergeNone() : Merge()
{
mId = "None";
}
///
/// Constructor
///
MergeNone::MergeNone( const MergeNone* merge ) : Merge( merge )
{
}
///
/// Destructor
///
MergeNone::~MergeNone()
{
}
///
/// Clone
///
MergeNone* MergeNone::clone() const
{
return new MergeNone( this );
}
///
/// Get ID
///
QString MergeNone::id()
{
return "None";
}
///
/// Create
///
Merge* MergeNone::create()
{
return new MergeNone();
}
///
/// Get key list
///
QStringList MergeNone::keys() const
{
QStringList emptyList;
return emptyList;
}
///
/// Get primary key
///
QString MergeNone::primaryKey() const
{
return "";
}
///
/// Open source
///
void MergeNone::open()
{
}
///
/// Close source
///
void MergeNone::close()
{
}
///
/// Read next record
///
MergeRecord* MergeNone::readNextRecord()
{
return 0;
}
+70
View File
@@ -0,0 +1,70 @@
/* MergeNone.h
*
* Copyright (C) 2015 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MergeNone_h
#define MergeNone_h
#include "Merge.h"
///
/// MergeNone Backend
///
struct MergeNone : public Merge
{
/////////////////////////////////
// Life Cycle
/////////////////////////////////
public:
MergeNone();
MergeNone( const MergeNone* merge );
virtual ~MergeNone();
/////////////////////////////////
// Object duplication
/////////////////////////////////
MergeNone* clone() const;
/////////////////////////////////
// Static methods
/////////////////////////////////
public:
static QString id();
static Merge* create();
/////////////////////////////////
// Implementation of virtual methods
/////////////////////////////////
public:
QStringList keys() const;
QString primaryKey() const;
protected:
void open();
void close();
MergeRecord* readNextRecord();
};
#endif // MergeNone_h
+65
View File
@@ -0,0 +1,65 @@
/* MergeRecord.cpp
*
* Copyright (C) 2013-2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MergeRecord.h"
///
/// Constructor
///
MergeRecord::MergeRecord() : mSelected( true )
{
}
///
/// Constructor
///
MergeRecord::MergeRecord( const MergeRecord* record )
: QMap<QString,QString>(*record), mSelected(record->mSelected)
{
}
///
/// Clone
///
MergeRecord* MergeRecord::clone() const
{
return new MergeRecord( this );
}
///
/// Is record selected?
///
bool MergeRecord::isSelected() const
{
return mSelected;
}
///
/// Set selected on not selected
///
void MergeRecord::setSelected( bool value )
{
mSelected = value;
}
+65
View File
@@ -0,0 +1,65 @@
/* MergeRecord.h
*
* Copyright (C) 2013-2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MergeRecord_h
#define MergeRecord_h
#include <QString>
#include <QMap>
///
/// Merge Record
///
struct MergeRecord : public QMap<QString,QString>
{
/////////////////////////////////
// Life Cycle
/////////////////////////////////
public:
MergeRecord();
MergeRecord( const MergeRecord* record );
/////////////////////////////////
// Object duplication
/////////////////////////////////
MergeRecord* clone() const;
/////////////////////////////////
// Properties
/////////////////////////////////
public:
bool isSelected() const;
void setSelected( bool value );
/////////////////////////////////
// Private data
/////////////////////////////////
private:
bool mSelected;
};
#endif // MergeRecord_h
+412
View File
@@ -0,0 +1,412 @@
/* MergeText.cpp
*
* Copyright (C) 2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MergeText.h"
#include <algorithm>
#include <QtDebug>
///
/// Constructor
///
MergeText::MergeText( QChar delimiter, bool line1HasKeys )
: mNFieldsMax(0), mDelimeter(delimiter), mLine1HasKeys(line1HasKeys)
{
}
///
/// Constructor
///
MergeText::MergeText( const MergeText* merge )
: Merge( merge ),
mNFieldsMax(merge->mNFieldsMax),
mDelimeter(merge->mDelimeter), mLine1HasKeys(merge->mLine1HasKeys)
{
}
///
/// Destructor
///
MergeText::~MergeText()
{
}
///
/// Get key list
///
QStringList MergeText::keys() const
{
QStringList keys;
for ( int iField = 0; iField < mNFieldsMax; iField++ )
{
keys << keyFromIndex(iField);
}
return keys;
}
///
/// Get primary key
///
QString MergeText::primaryKey() const
{
return keyFromIndex(0);
}
///
/// Open source
///
void MergeText::open()
{
mFile.setFileName( source() );
mFile.open( QIODevice::ReadOnly|QIODevice::Text );
mKeys.clear();
mNFieldsMax = 0;
if ( mLine1HasKeys && mFile.isOpen() )
{
mKeys = parseLine();
if ( (mKeys.size() == 1) && (mKeys[0] == "") )
{
mKeys.clear();
}
else
{
mNFieldsMax = mKeys.size();
}
}
}
///
/// Close source
///
void MergeText::close()
{
if ( mFile.isOpen() )
{
mFile.close();
}
}
///
/// Read next record
///
MergeRecord* MergeText::readNextRecord()
{
QStringList values = parseLine();
if ( !values.isEmpty() )
{
MergeRecord* record = new MergeRecord();
int iField = 0;
foreach ( QString value, values )
{
(*record)[ keyFromIndex(iField) ] = value;
iField++;
}
mNFieldsMax = std::max( mNFieldsMax, iField );
return record;
}
return 0;
}
///
/// Key from field index
///
QString MergeText::keyFromIndex( int iField ) const
{
if ( mLine1HasKeys && ( iField < mKeys.size() ) )
{
return mKeys[iField];
}
else
{
return QString::number( iField+1 );
}
}
///
/// Parse line.
///
/// Attempt to be a robust parser of various CSV (and similar) formats.
///
/// Based on CSV format described in RFC 4180 section 2.
///
/// Additions to RFC 4180 rules:
/// - delimeters and other special characters may be "escaped" by a leading
/// backslash (\)
/// - C escape sequences for newline (\n) and tab (\t) are also translated.
/// - if quoted text is not followed by a delimeter, any additional text is
/// concatenated with quoted portion.
///
/// Returns a list of fields. A blank line is considered a line with one
/// empty field. Returns an empty list when done.
///
QStringList MergeText::parseLine()
{
QStringList fields;
enum State
{
DELIM, QUOTED, QUOTED_QUOTE1, QUOTED_ESCAPED, SIMPLE, SIMPLE_ESCAPED, DONE
} state = DELIM;
QByteArray field;
while ( state != DONE )
{
char c;
if ( mFile.getChar( &c ) )
{
switch (state)
{
case DELIM:
switch (c)
{
case '\n':
/* last field is empty. */
fields << "";
state = DONE;
break;
case '\r':
/* ignore */
state = DELIM;
break;
case '"':
/* start a quoted field. */
state = QUOTED;
break;
case '\\':
/* simple field, but 1st character is an escape. */
state = SIMPLE_ESCAPED;
break;
default:
if ( c == mDelimeter )
{
/* field is empty. */
fields << "";
state = DELIM;
}
else
{
/* begining of a simple field. */
field.append( c );
state = SIMPLE;
}
break;
}
break;
case QUOTED:
switch (c)
{
case '"':
/* Possible end of field, but could be 1st of a pair. */
state = QUOTED_QUOTE1;
break;
case '\\':
/* Escape next character, or special escape, e.g. \n. */
state = QUOTED_ESCAPED;
break;
default:
/* Use character literally. */
field.append( c );
break;
}
break;
case QUOTED_QUOTE1:
switch (c)
{
case '\n':
/* line ended after quoted item */
fields << QString( field );
state = DONE;
break;
case '"':
/* second quote, insert and stay quoted. */
field.append( c );
state = QUOTED;
break;
case '\r':
/* ignore and go to fallback */
state = SIMPLE;
break;
default:
if ( c == mDelimeter )
{
/* end of field. */
fields << QString( field );
field.clear();
state = DELIM;
}
else
{
/* fallback if not a delim or another quote. */
field.append( c );
state = SIMPLE;
}
break;
}
break;
case QUOTED_ESCAPED:
switch (c)
{
case 'n':
/* Decode "\n" as newline. */
field.append( '\n' );
state = QUOTED;
break;
case 't':
/* Decode "\t" as tab. */
field.append( '\t' );
state = QUOTED;
break;
default:
/* Use character literally. */
field.append( c );
state = QUOTED;
break;
}
break;
case SIMPLE:
switch (c)
{
case '\n':
/* line ended */
fields << QString( field );
state = DONE;
break;
case '\r':
/* ignore */
state = SIMPLE;
break;
case '\\':
/* Escape next character, or special escape, e.g. \n. */
state = SIMPLE_ESCAPED;
break;
default:
if ( c == mDelimeter )
{
/* end of field. */
fields << QString( field );
field.clear();
state = DELIM;
}
else
{
/* Use character literally. */
field.append( c );
state = SIMPLE;
}
break;
}
break;
case SIMPLE_ESCAPED:
switch (c)
{
case 'n':
/* Decode "\n" as newline. */
field.append( '\n' );
state = SIMPLE;
break;
case 't':
/* Decode "\t" as tab. */
field.append( '\t' );
state = SIMPLE;
break;
default:
/* Use character literally. */
field.append( (char)c );
state = SIMPLE;
break;
}
break;
default:
qWarning( "MergeText::parseLine()::Should not be reached! #1" );
break;
}
}
else
{
/* Handle EOF (could also be an error while reading). */
switch (state)
{
case DELIM:
/* EOF, no more lines. */
break;
case QUOTED:
/* File ended midway through quoted item. Truncate field. */
fields << QString( field );
break;
case QUOTED_QUOTE1:
/* File ended after quoted item. */
fields << QString( field );
break;
case QUOTED_ESCAPED:
/* File ended midway through quoted item. Truncate field. */
fields << QString( field );
break;
case SIMPLE:
/* File ended after simple item. */
fields << QString( field );
break;
case SIMPLE_ESCAPED:
/* File ended midway through escaped item. */
fields << QString( field );
break;
default:
qWarning( "MergeText::parseLine()::Should not be reached! #2" );
break;
}
state = DONE;
}
}
return fields;
}
+76
View File
@@ -0,0 +1,76 @@
/* MergeText.h
*
* Copyright (C) 2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MergeText_h
#define MergeText_h
#include "Merge.h"
#include <QFile>
///
/// MergeText Backend
///
struct MergeText : public Merge
{
/////////////////////////////////
// Life Cycle
/////////////////////////////////
protected:
MergeText( QChar delimiter, bool line1HasKeys );
MergeText( const MergeText* merge );
virtual ~MergeText();
/////////////////////////////////
// Implementation of virtual methods
/////////////////////////////////
public:
QStringList keys() const;
QString primaryKey() const;
protected:
void open();
void close();
MergeRecord* readNextRecord();
/////////////////////////////////
// Private methods
/////////////////////////////////
QString keyFromIndex( int iField ) const;
QStringList parseLine();
/////////////////////////////////
// Private data
/////////////////////////////////
private:
QChar mDelimeter;
bool mLine1HasKeys;
QFile mFile;
QStringList mKeys;
int mNFieldsMax;
};
#endif // MergeText_h
+73
View File
@@ -0,0 +1,73 @@
/* MergeTextCsv.cpp
*
* Copyright (C) 2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MergeTextCsv.h"
///
/// Constructor
///
MergeTextCsv::MergeTextCsv() : MergeText(',',false)
{
mId = "Text/Comma";
}
///
/// Constructor
///
MergeTextCsv::MergeTextCsv( const MergeTextCsv* merge ) : MergeText( merge )
{
}
///
/// Destructor
///
MergeTextCsv::~MergeTextCsv()
{
}
///
/// Clone
///
MergeTextCsv* MergeTextCsv::clone() const
{
return new MergeTextCsv( this );
}
///
/// Get ID
///
QString MergeTextCsv::id()
{
return "Text/Comma";
}
///
/// Create
///
Merge* MergeTextCsv::create()
{
return new MergeTextCsv();
}
+59
View File
@@ -0,0 +1,59 @@
/* MergeTextCsv.h
*
* Copyright (C) 2016 Jim Evins <evins@snaught.com>
*
* This file is part of gLabels-qt.
*
* gLabels-qt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels-qt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MergeTextCsv_h
#define MergeTextCsv_h
#include "MergeText.h"
///
/// MergeTextCsv Backend
///
struct MergeTextCsv : public MergeText
{
/////////////////////////////////
// Life Cycle
/////////////////////////////////
private:
MergeTextCsv();
MergeTextCsv( const MergeTextCsv* merge );
virtual ~MergeTextCsv();
/////////////////////////////////
// Object duplication
/////////////////////////////////
public:
static QString id();
MergeTextCsv* clone() const;
/////////////////////////////////
// Static methods
/////////////////////////////////
public:
static Merge* create();
};
#endif // MergeTextCsv_h