![]() |
Mixxx
|
00001 // 00002 // C++ Implementation: parser 00003 // 00004 // Description: superclass for external formats parsers 00005 // 00006 // 00007 // Author: Ingo Kossyk <kossyki@cs.tu-berlin.de>, (C) 2004 00008 // Author: Tobias Rafreider trafreider@mixxx.org, (C) 2011 00009 // 00010 // Copyright: See COPYING file that comes with this distribution 00011 // 00012 // 00013 00014 #include <QtDebug> 00015 #include <QStringList> 00016 #include "parser.h" 00017 00023 Parser::Parser() : QObject() 00024 { 00025 } 00026 00027 Parser::~Parser() 00028 { 00029 00030 00031 } 00032 00033 void Parser::clearLocations() 00034 { 00035 while(!m_sLocations.isEmpty()) 00036 m_sLocations.removeFirst(); 00037 } 00038 00039 long Parser::countParsed() 00040 { 00041 return (long)m_sLocations.count(); 00042 } 00043 00044 bool Parser::isFilepath(QString sFilepath){ 00045 QFile file(sFilepath); 00046 bool exists = file.exists(); 00047 file.close(); 00048 return exists; 00049 } 00050 00051 bool Parser::isBinary(QString filename){ 00052 QFile file(filename); 00053 00054 if(file.open(QIODevice::ReadOnly)){ 00055 char c; 00056 unsigned char uc; 00057 00058 if(!file.getChar(&c)) 00059 { 00060 qDebug() << "Parser: Error reading stream on " << filename; 00061 return true; //should this raise an exception? 00062 } 00063 00064 uc = uchar(c); 00065 00066 if(!(33<=uc && uc<=127)) //Starting byte is no character 00067 { 00068 file.close(); 00069 return true; 00070 } 00071 00072 } else{ 00073 qDebug() << "Parser: Could not open file: " << filename; 00074 } 00075 //qDebug(QString("Parser: textstream starting character is: %1").arg(i)); 00076 file.close(); 00077 return false; 00078 } 00079 00080 // The following public domain code is taken from 00081 // http://stackoverflow.com/questions/1031645/how-to-detect-utf-8-in-plain-c 00082 // Thank you Christoph! 00083 // static 00084 bool Parser::isUtf8(const char* string) { 00085 if (!string) { 00086 return false; 00087 } 00088 00089 const unsigned char* bytes = (const unsigned char *)string; 00090 while(*bytes) { 00091 if( (// ASCII 00092 bytes[0] == 0x09 || 00093 bytes[0] == 0x0A || 00094 bytes[0] == 0x0D || 00095 (0x20 <= bytes[0] && bytes[0] <= 0x7E) 00096 ) 00097 ) { 00098 bytes += 1; 00099 continue; 00100 } 00101 00102 if( (// non-overlong 2-byte 00103 (0xC2 <= bytes[0] && bytes[0] <= 0xDF) && 00104 (0x80 <= bytes[1] && bytes[1] <= 0xBF) 00105 ) 00106 ) { 00107 bytes += 2; 00108 continue; 00109 } 00110 00111 if( (// excluding overlongs 00112 bytes[0] == 0xE0 && 00113 (0xA0 <= bytes[1] && bytes[1] <= 0xBF) && 00114 (0x80 <= bytes[2] && bytes[2] <= 0xBF) 00115 ) || 00116 (// straight 3-byte 00117 ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) || 00118 bytes[0] == 0xEE || 00119 bytes[0] == 0xEF) && 00120 (0x80 <= bytes[1] && bytes[1] <= 0xBF) && 00121 (0x80 <= bytes[2] && bytes[2] <= 0xBF) 00122 ) || 00123 (// excluding surrogates 00124 bytes[0] == 0xED && 00125 (0x80 <= bytes[1] && bytes[1] <= 0x9F) && 00126 (0x80 <= bytes[2] && bytes[2] <= 0xBF) 00127 ) 00128 ) { 00129 bytes += 3; 00130 continue; 00131 } 00132 00133 if( (// planes 1-3 00134 bytes[0] == 0xF0 && 00135 (0x90 <= bytes[1] && bytes[1] <= 0xBF) && 00136 (0x80 <= bytes[2] && bytes[2] <= 0xBF) && 00137 (0x80 <= bytes[3] && bytes[3] <= 0xBF) 00138 ) || 00139 (// planes 4-15 00140 (0xF1 <= bytes[0] && bytes[0] <= 0xF3) && 00141 (0x80 <= bytes[1] && bytes[1] <= 0xBF) && 00142 (0x80 <= bytes[2] && bytes[2] <= 0xBF) && 00143 (0x80 <= bytes[3] && bytes[3] <= 0xBF) 00144 ) || 00145 (// plane 16 00146 bytes[0] == 0xF4 && 00147 (0x80 <= bytes[1] && bytes[1] <= 0x8F) && 00148 (0x80 <= bytes[2] && bytes[2] <= 0xBF) && 00149 (0x80 <= bytes[3] && bytes[3] <= 0xBF) 00150 ) 00151 ) { 00152 bytes += 4; 00153 continue; 00154 } 00155 00156 return false; 00157 } 00158 00159 return true; 00160 }