annotate kdiff3/src-QT4/common.cpp @ 113:7bca1f1340f6 tip

Build fixes for Xcode 10 / Qt 5.12
author Chris Cannam
date Mon, 17 Dec 2018 11:13:01 +0000
parents fcd146072e0c
children
rev   line source
joachim99@69 1 /***************************************************************************
joachim99@77 2 * Copyright (C) 2004-2007 by Joachim Eibl *
joachim99@69 3 * joachim.eibl at gmx.de *
joachim99@69 4 * *
joachim99@69 5 * This program is free software; you can redistribute it and/or modify *
joachim99@69 6 * it under the terms of the GNU General Public License as published by *
joachim99@69 7 * the Free Software Foundation; either version 2 of the License, or *
joachim99@69 8 * (at your option) any later version. *
joachim99@69 9 * *
joachim99@69 10 * This program is distributed in the hope that it will be useful, *
joachim99@69 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
joachim99@69 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
joachim99@69 13 * GNU General Public License for more details. *
joachim99@69 14 * *
joachim99@69 15 * You should have received a copy of the GNU General Public License *
joachim99@69 16 * along with this program; if not, write to the *
joachim99@69 17 * Free Software Foundation, Inc., *
joachim99@69 18 * 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
joachim99@69 19 ***************************************************************************/
joachim99@69 20
joachim99@69 21 #include "common.h"
joachim99@69 22 #include <map>
joachim99@69 23 #include <qfont.h>
joachim99@69 24 #include <qcolor.h>
joachim99@69 25 #include <qsize.h>
joachim99@69 26 #include <qpoint.h>
joachim99@69 27 #include <qstringlist.h>
joachim99@69 28 #include <qtextstream.h>
joachim99@69 29
joachim99@69 30 ValueMap::ValueMap()
joachim99@69 31 {
joachim99@69 32 }
joachim99@69 33
joachim99@69 34 ValueMap::~ValueMap()
joachim99@69 35 {
joachim99@69 36 }
joachim99@69 37
joachim99@69 38 void ValueMap::save( QTextStream& ts )
joachim99@69 39 {
joachim99@69 40 std::map<QString,QString>::iterator i;
joachim99@69 41 for( i=m_map.begin(); i!=m_map.end(); ++i)
joachim99@69 42 {
joachim99@69 43 QString key = i->first;
joachim99@69 44 QString val = i->second;
joachim99@69 45 ts << key << "=" << val << "\n";
joachim99@69 46 }
joachim99@69 47 }
joachim99@69 48
joachim99@69 49 QString ValueMap::getAsString()
joachim99@69 50 {
joachim99@69 51 QString result;
joachim99@69 52 std::map<QString,QString>::iterator i;
joachim99@69 53 for( i=m_map.begin(); i!=m_map.end(); ++i)
joachim99@69 54 {
joachim99@69 55 QString key = i->first;
joachim99@69 56 QString val = i->second;
joachim99@69 57 result += key + "=" + val + "\n";
joachim99@69 58 }
joachim99@69 59 return result;
joachim99@69 60 }
joachim99@69 61
joachim99@69 62 void ValueMap::load( QTextStream& ts )
joachim99@69 63 {
joachim99@70 64 while ( !ts.atEnd() )
joachim99@69 65 { // until end of file...
joachim99@69 66 QString s = ts.readLine(); // line of text excluding '\n'
joachim99@75 67 int pos = s.indexOf('=');
joachim99@69 68 if( pos > 0 ) // seems not to have a tag
joachim99@69 69 {
joachim99@69 70 QString key = s.left(pos);
joachim99@69 71 QString val = s.mid(pos+1);
joachim99@69 72 m_map[key] = val;
joachim99@69 73 }
joachim99@69 74 }
joachim99@69 75 }
joachim99@69 76 /*
joachim99@69 77 void ValueMap::load( const QString& s )
joachim99@69 78 {
joachim99@69 79 int pos=0;
joachim99@69 80 while ( pos<(int)s.length() )
joachim99@69 81 { // until end of file...
joachim99@69 82 int pos2 = s.find('=', pos);
joachim99@69 83 int pos3 = s.find('\n', pos2 );
joachim99@69 84 if (pos3<0)
joachim99@69 85 pos3=s.length();
joachim99@69 86 if( pos2 > 0 ) // seems not to have a tag
joachim99@69 87 {
joachim99@69 88 QString key = s.mid(pos, pos2-pos);
joachim99@69 89 QString val = s.mid(pos2+1, pos3-pos2-1);
joachim99@69 90 m_map[key] = val;
joachim99@69 91 }
joachim99@69 92 pos = pos3;
joachim99@69 93 }
joachim99@69 94 }
joachim99@69 95 */
joachim99@69 96
joachim99@69 97 // safeStringJoin and safeStringSplit allow to convert a stringlist into a string and back
joachim99@69 98 // safely, even if the individual strings in the list contain the separator character.
joachim99@69 99 QString safeStringJoin(const QStringList& sl, char sepChar, char metaChar )
joachim99@69 100 {
joachim99@69 101 // Join the strings in the list, using the separator ','
joachim99@69 102 // If a string contains the separator character, it will be replaced with "\,".
joachim99@69 103 // Any occurances of "\" (one backslash) will be replaced with "\\" (2 backslashes)
joachim99@69 104
joachim99@69 105 assert(sepChar!=metaChar);
joachim99@69 106
joachim99@69 107 QString sep;
joachim99@69 108 sep += sepChar;
joachim99@69 109 QString meta;
joachim99@69 110 meta += metaChar;
joachim99@69 111
joachim99@69 112 QString safeString;
joachim99@69 113
joachim99@69 114 QStringList::const_iterator i;
joachim99@69 115 for (i=sl.begin(); i!=sl.end(); ++i)
joachim99@69 116 {
joachim99@69 117 QString s = *i;
joachim99@69 118 s.replace(meta, meta+meta); // "\" -> "\\"
joachim99@69 119 s.replace(sep, meta+sep); // "," -> "\,"
joachim99@69 120 if ( i==sl.begin() )
joachim99@69 121 safeString = s;
joachim99@69 122 else
joachim99@69 123 safeString += sep + s;
joachim99@69 124 }
joachim99@69 125 return safeString;
joachim99@69 126 }
joachim99@69 127
joachim99@69 128 // Split a string that was joined with safeStringJoin
joachim99@69 129 QStringList safeStringSplit(const QString& s, char sepChar, char metaChar )
joachim99@69 130 {
joachim99@69 131 assert(sepChar!=metaChar);
joachim99@69 132 QStringList sl;
joachim99@69 133 // Miniparser
joachim99@69 134 int i=0;
joachim99@69 135 int len=s.length();
joachim99@69 136 QString b;
joachim99@69 137 for(i=0;i<len;++i)
joachim99@69 138 {
joachim99@69 139 if ( i+1<len && s[i]==metaChar && s[i+1]==metaChar ){ b+=metaChar; ++i; }
joachim99@69 140 else if ( i+1<len && s[i]==metaChar && s[i+1]==sepChar ){ b+=sepChar; ++i; }
joachim99@69 141 else if ( s[i]==sepChar ) // real separator
joachim99@69 142 {
joachim99@69 143 sl.push_back(b);
joachim99@69 144 b="";
joachim99@69 145 }
joachim99@69 146 else { b+=s[i]; }
joachim99@69 147 }
joachim99@69 148 if ( !b.isEmpty() )
joachim99@69 149 sl.push_back(b);
joachim99@69 150
joachim99@69 151 return sl;
joachim99@69 152 }
joachim99@69 153
joachim99@69 154
joachim99@69 155
joachim99@69 156 static QString numStr(int n)
joachim99@69 157 {
joachim99@69 158 QString s;
joachim99@69 159 s.setNum( n );
joachim99@69 160 return s;
joachim99@69 161 }
joachim99@69 162
joachim99@69 163 static QString subSection( const QString& s, int idx, char sep )
joachim99@69 164 {
joachim99@69 165 int pos=0;
joachim99@69 166 while( idx>0 )
joachim99@69 167 {
joachim99@75 168 pos = s.indexOf( sep, pos );
joachim99@69 169 --idx;
joachim99@69 170 if (pos<0) break;
joachim99@69 171 ++pos;
joachim99@69 172 }
joachim99@69 173 if ( pos>=0 )
joachim99@69 174 {
joachim99@75 175 int pos2 = s.indexOf( sep, pos );
joachim99@69 176 if ( pos2>0 )
joachim99@69 177 return s.mid(pos, pos2-pos);
joachim99@69 178 else
joachim99@69 179 return s.mid(pos);
joachim99@69 180 }
joachim99@69 181
joachim99@69 182 return "";
joachim99@69 183 }
joachim99@69 184
joachim99@69 185 static int num( QString& s, int idx )
joachim99@69 186 {
joachim99@69 187 return subSection( s, idx, ',').toInt();
joachim99@69 188 }
joachim99@69 189
joachim99@69 190 void ValueMap::writeEntry(const QString& k, const QFont& v )
joachim99@69 191 {
joachim99@69 192 m_map[k] = v.family() + "," + QString::number(v.pointSize()) + "," + (v.bold() ? "bold" : "normal");
joachim99@69 193 }
joachim99@69 194
joachim99@69 195 void ValueMap::writeEntry(const QString& k, const QColor& v )
joachim99@69 196 {
joachim99@69 197 m_map[k] = numStr(v.red()) + "," + numStr(v.green()) + "," + numStr(v.blue());
joachim99@69 198 }
joachim99@69 199
joachim99@69 200 void ValueMap::writeEntry(const QString& k, const QSize& v )
joachim99@69 201 {
joachim99@69 202 m_map[k] = numStr(v.width()) + "," + numStr(v.height());
joachim99@69 203 }
joachim99@69 204
joachim99@69 205 void ValueMap::writeEntry(const QString& k, const QPoint& v )
joachim99@69 206 {
joachim99@69 207 m_map[k] = numStr(v.x()) + "," + numStr(v.y());
joachim99@69 208 }
joachim99@69 209
joachim99@69 210 void ValueMap::writeEntry(const QString& k, int v )
joachim99@69 211 {
joachim99@69 212 m_map[k] = numStr(v);
joachim99@69 213 }
joachim99@69 214
joachim99@69 215 void ValueMap::writeEntry(const QString& k, bool v )
joachim99@69 216 {
joachim99@69 217 m_map[k] = numStr(v);
joachim99@69 218 }
joachim99@69 219
joachim99@69 220 void ValueMap::writeEntry(const QString& k, const QString& v )
joachim99@69 221 {
joachim99@69 222 m_map[k] = v;
joachim99@69 223 }
joachim99@69 224
joachim99@69 225 void ValueMap::writeEntry(const QString& k, const char* v )
joachim99@69 226 {
joachim99@69 227 m_map[k] = v;
joachim99@69 228 }
joachim99@69 229
joachim99@69 230 void ValueMap::writeEntry(const QString& k, const QStringList& v, char separator )
joachim99@69 231 {
joachim99@69 232 m_map[k] = safeStringJoin(v, separator);
joachim99@69 233 }
joachim99@69 234
joachim99@69 235
joachim99@80 236 QFont ValueMap::readFontEntry(const QString& k, const QFont* defaultVal )
joachim99@69 237 {
joachim99@69 238 QFont f = *defaultVal;
joachim99@69 239 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 240 if ( i!=m_map.end() )
joachim99@69 241 {
joachim99@69 242 f.setFamily( subSection( i->second, 0, ',' ) );
joachim99@69 243 f.setPointSize( subSection( i->second, 1, ',' ).toInt() );
joachim99@69 244 f.setBold( subSection( i->second, 2, ',' )=="bold" );
joachim99@69 245 //f.fromString(i->second);
joachim99@69 246 }
joachim99@69 247
joachim99@69 248 return f;
joachim99@69 249 }
joachim99@69 250
joachim99@80 251 QColor ValueMap::readColorEntry(const QString& k, const QColor* defaultVal )
joachim99@69 252 {
joachim99@69 253 QColor c= *defaultVal;
joachim99@69 254 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 255 if ( i!=m_map.end() )
joachim99@69 256 {
joachim99@69 257 QString s = i->second;
joachim99@69 258 c = QColor( num(s,0),num(s,1),num(s,2) );
joachim99@69 259 }
joachim99@69 260
joachim99@69 261 return c;
joachim99@69 262 }
joachim99@69 263
joachim99@80 264 QSize ValueMap::readSizeEntry(const QString& k, const QSize* defaultVal )
joachim99@69 265 {
joachim99@69 266 QSize size = defaultVal ? *defaultVal : QSize(600,400);
joachim99@69 267 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 268 if ( i!=m_map.end() )
joachim99@69 269 {
joachim99@69 270
joachim99@69 271 QString s = i->second;
joachim99@69 272 size = QSize( num(s,0),num(s,1) );
joachim99@69 273 }
joachim99@69 274
joachim99@69 275 return size;
joachim99@69 276 }
joachim99@69 277
joachim99@80 278 QPoint ValueMap::readPointEntry(const QString& k, const QPoint* defaultVal)
joachim99@69 279 {
joachim99@69 280 QPoint point = defaultVal ? *defaultVal : QPoint(0,0);
joachim99@69 281 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 282 if ( i!=m_map.end() )
joachim99@69 283 {
joachim99@69 284 QString s = i->second;
joachim99@69 285 point = QPoint( num(s,0),num(s,1) );
joachim99@69 286 }
joachim99@69 287
joachim99@69 288 return point;
joachim99@69 289 }
joachim99@69 290
joachim99@69 291 bool ValueMap::readBoolEntry(const QString& k, bool bDefault )
joachim99@69 292 {
joachim99@69 293 bool b = bDefault;
joachim99@69 294 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 295 if ( i!=m_map.end() )
joachim99@69 296 {
joachim99@69 297 QString s = i->second;
joachim99@69 298 b = (bool)num(s,0);
joachim99@69 299 }
joachim99@69 300
joachim99@69 301 return b;
joachim99@69 302 }
joachim99@69 303
joachim99@69 304 int ValueMap::readNumEntry(const QString& k, int iDefault )
joachim99@69 305 {
joachim99@69 306 int ival = iDefault;
joachim99@69 307 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 308 if ( i!=m_map.end() )
joachim99@69 309 {
joachim99@69 310 QString s = i->second;
joachim99@69 311 ival = num(s,0);
joachim99@69 312 }
joachim99@69 313
joachim99@69 314 return ival;
joachim99@69 315 }
joachim99@69 316
joachim99@80 317 QString ValueMap::readStringEntry(const QString& k, const QString& sDefault )
joachim99@69 318 {
joachim99@69 319 QString sval = sDefault;
joachim99@69 320 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 321 if ( i!=m_map.end() )
joachim99@69 322 {
joachim99@69 323 sval = i->second;
joachim99@69 324 }
joachim99@69 325
joachim99@69 326 return sval;
joachim99@69 327 }
joachim99@69 328
joachim99@69 329 QStringList ValueMap::readListEntry(const QString& k, const QStringList& defaultVal, char separator )
joachim99@69 330 {
joachim99@69 331 QStringList strList;
joachim99@69 332
joachim99@69 333 std::map<QString,QString>::iterator i = m_map.find( k );
joachim99@69 334 if ( i!=m_map.end() )
joachim99@69 335 {
joachim99@69 336 strList = safeStringSplit( i->second, separator );
joachim99@69 337 return strList;
joachim99@69 338 }
joachim99@69 339 else
joachim99@69 340 return defaultVal;
joachim99@69 341 }
joachim99@80 342
joachim99@80 343 QString ValueMap::readEntry (const QString& s, const QString& defaultVal ) { return readStringEntry(s, defaultVal); }
joachim99@80 344 QString ValueMap::readEntry (const QString& s, const char* defaultVal ) { return readStringEntry(s, QString::fromLatin1(defaultVal) ); }
joachim99@80 345 QFont ValueMap::readEntry (const QString& s, const QFont& defaultVal ){ return readFontEntry(s,&defaultVal); }
joachim99@80 346 QColor ValueMap::readEntry(const QString& s, const QColor defaultVal ){ return readColorEntry(s,&defaultVal); }
joachim99@80 347 QSize ValueMap::readEntry (const QString& s, const QSize defaultVal ){ return readSizeEntry(s,&defaultVal); }
joachim99@80 348 QPoint ValueMap::readEntry(const QString& s, const QPoint defaultVal ){ return readPointEntry(s,&defaultVal); }
joachim99@80 349 bool ValueMap::readEntry (const QString& s, bool bDefault ){ return readBoolEntry(s,bDefault); }
joachim99@80 350 int ValueMap::readEntry (const QString& s, int iDefault ){ return readNumEntry(s,iDefault); }
joachim99@80 351 QStringList ValueMap::readEntry (const QString& s, const QStringList& defaultVal, char separator ){ return readListEntry(s,defaultVal,separator); }