Skip to content

Commit

Permalink
bookmarks: update CSV reader/writer
Browse files Browse the repository at this point in the history
  • Loading branch information
vladisslav2011 committed Aug 14, 2023
1 parent 69c4760 commit 941fbf1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
40 changes: 29 additions & 11 deletions src/qtgui/bookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
const QColor TagInfo::DefaultColor(Qt::lightGray);
const QString TagInfo::strUntagged("Untagged");

CommaSeparated::CommaSeparated(bool useCaptions, QChar quote, QChar fieldDelimiter, QChar lineDelimiter):
m_UseCaptions(useCaptions), m_Quote(quote), m_FieldDelimiter(fieldDelimiter), m_LineDelimiter(lineDelimiter)
CommaSeparated::CommaSeparated(QChar quote, QChar fieldDelimiter, QChar lineDelimiter):
m_Quote(quote), m_FieldDelimiter(fieldDelimiter), m_LineDelimiter(lineDelimiter)
{
}

Expand Down Expand Up @@ -87,22 +87,22 @@ bool CommaSeparated::write(QStringList & row)
return m_FD.error()==QFileDevice::NoError;
}

bool CommaSeparated::read()
bool CommaSeparated::read(QStringList & row)
{
int ps=0;
int pc=0;
bool in_quotes=false;
bool found_quote=false;
QString buf{""};
m_Row.clear();
row.clear();
while(true)
{
buf+=QString::fromUtf8(m_FD.readLine());
if(buf.length()==0)
return false;
if((buf.length()==1)&&(buf[0]==m_LineDelimiter))
{
m_Row.append("");
row.append("");
return true;
}
while(true)
Expand Down Expand Up @@ -132,29 +132,29 @@ bool CommaSeparated::read()
{
if(buf[ps]==m_Quote)
{
m_Row.append(buf.mid(ps+1,pc-ps-2).replace(QString(m_Quote)+m_Quote,QString(m_Quote)));
row.append(buf.mid(ps+1,pc-ps-2).replace(QString(m_Quote)+m_Quote,QString(m_Quote)));
}else{
m_Row.append(buf.mid(ps,pc-ps));
row.append(buf.mid(ps,pc-ps));
}
return true;
}
if(buf[pc]==m_FieldDelimiter)
{
if(buf[ps]==m_Quote)
{
m_Row.append(buf.mid(ps+1,pc-ps-2).replace(QString(m_Quote)+m_Quote,QString(m_Quote)));
row.append(buf.mid(ps+1,pc-ps-2).replace(QString(m_Quote)+m_Quote,QString(m_Quote)));
}else{
m_Row.append(buf.mid(ps,pc-ps));
row.append(buf.mid(ps,pc-ps));
}
ps=pc+1;
}
if(buf[pc]==m_LineDelimiter)
{
if(buf[ps]==m_Quote)
{
m_Row.append(buf.mid(ps+1,pc-ps-2).replace(QString(m_Quote)+m_Quote,QString(m_Quote)));
row.append(buf.mid(ps+1,pc-ps-2).replace(QString(m_Quote)+m_Quote,QString(m_Quote)));
}else{
m_Row.append(buf.mid(ps,pc-ps));
row.append(buf.mid(ps,pc-ps));
}
return true;
}
Expand All @@ -166,6 +166,24 @@ bool CommaSeparated::read()
}
}

bool CommaSeparated::read(const QStringList & captions, QMap<QString,QString> & row)
{
if(!read(m_Row))
return false;
row.clear();
const auto rowCount=m_Row.length();
const auto captCount=captions.length();
for(int k=0;k<captCount;k++)
if(k<rowCount)
row[captions[k]]=m_Row[k];
return true;
}

bool CommaSeparated::read()
{
return read(m_Row);
}

Bookmarks::Bookmarks()
{
TagInfo::sptr tag = TagInfo::make(TagInfo::strUntagged);
Expand Down
5 changes: 3 additions & 2 deletions src/qtgui/bookmarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ class BookmarkInfo:public vfo_s
class CommaSeparated
{
public:
CommaSeparated(bool useCaptions=true, QChar quote='\"', QChar fieldDelimiter=';', QChar lineDelimiter='\n');
CommaSeparated(QChar quote='\"', QChar fieldDelimiter=';', QChar lineDelimiter='\n');
~CommaSeparated();
bool open(const QString filename, bool write);
void close();
QStringList & getRowRef(){return m_Row;}
bool write(QStringList & row);
bool read();
bool read(QStringList & row);
bool read(const QStringList & captions, QMap<QString,QString> & row);
private:
bool m_UseCaptions;
QChar m_Quote;
QChar m_FieldDelimiter;
QChar m_LineDelimiter;
Expand Down

0 comments on commit 941fbf1

Please sign in to comment.