Skip to content
Permalink
Browse files

Fixes #9991. Replaces csv parser in icon downloader.

The existing code was overly complicated and didn't account for quotes.
  • Loading branch information
daniel-kristjansson committed Nov 24, 2011
1 parent f53cc94 commit f0deca75d6024ca36c0c5c26600eda4f846a8644
Showing with 30 additions and 48 deletions.
  1. +30 −48 mythtv/programs/mythtv-setup/importicons.cpp
@@ -463,60 +463,42 @@ QString ImportIconsWizard::escape_csv(const QString& str)
return "\""+str2+"\"";
}

QStringList ImportIconsWizard::extract_csv(const QString& strLine)
QStringList ImportIconsWizard::extract_csv(const QString &line)
{
QStringList ret;
//Clean up the line and split out the fields
QString str = strLine;

int pos = 0;
bool fFinish = false;
while(!fFinish)
QString str;
bool in_comment = false;
bool in_escape = false;
int comma_count = 0;
for (int i = 0; i < line.length(); i++)
{
str = str.trimmed();
while(!fFinish)
QChar cur = line[i];
if (in_escape)
{
QString strLeft;
switch (str.at(pos).unicode())
{
case '\\':
if (pos>=1)
str.left(pos-1)+str.mid(pos+1);
else
str=str.mid(pos+1);
pos+=2;
if (pos > str.length())
{
strLeft = str.left(pos);
if (strLeft.startsWith("\"") && strLeft.endsWith("\""))
strLeft=strLeft.mid(1,strLeft.length()-2);
ret.append(strLeft);
fFinish = true;
}
break;
case ',':
strLeft = str.left(pos);
if (strLeft.startsWith("\"") && strLeft.endsWith("\""))
strLeft=strLeft.mid(1,strLeft.length()-2);
ret.append(strLeft);
if ((pos+1) > str.length())
fFinish = true;
str=str.mid(pos+1);
pos=0;
break;
default:
pos++;
if (pos >= str.length())
{
strLeft = str.left(pos);
if (strLeft.startsWith("\"") && strLeft.endsWith("\""))
strLeft=strLeft.mid(1,strLeft.length()-2);
ret.append(strLeft);
fFinish = true;
}
}
str += cur;
in_escape = false;
}
else if (cur == '"')
{
in_comment = !in_comment;
}
else if (cur == '\\')
{
in_escape = true;
}
else if (!in_comment && (cur == ','))
{
ret += str;
str.clear();
++comma_count;
}
else
{
str += cur;
}
}
if (comma_count)
ret += str;

// This is just to avoid segfaulting, we should add some error recovery
while (ret.size() < 5)

0 comments on commit f0deca7

Please sign in to comment.
You can’t perform that action at this time.