File: ConfigData.cpp

package info (click to toggle)
subcommander 2.0.0~b5p1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 14,880 kB
  • ctags: 9,003
  • sloc: cpp: 63,591; sh: 3,904; xml: 1,984; makefile: 1,166; ansic: 786; ruby: 251; lisp: 24
file content (127 lines) | stat: -rw-r--r-- 2,707 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* ====================================================================
 * Copyright (c) 2003-2008  Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "ConfigData.h"
#include "Config.h"

// qt
#include <QtCore/QString>
#include <QtCore/QRegExp>

// sys
#include <algorithm>


static ConfigValue NullValue( sc::String(""), sc::String("") );


ConfigData::ConfigData( Config* cfg )
: _cfg(cfg)
{
}

void ConfigData::load()
{
  _cfg->get(_data);
}

static bool lessConfigValue( const ConfigValue& left, const ConfigValue& right )
{
  return left.getKey() < right.getKey();
}

void ConfigData::save()
{
  std::sort(_data.begin(),_data.end(), lessConfigValue );
  _cfg->set(_data);
}

void ConfigData::getValues( const sc::String& key, ConfigValues& values )
{
  getValues( key, _data, values, true );
}

void ConfigData::delValues( const sc::String& key )
{
  delValues( key, _data, true );
}

void ConfigData::setValues( const sc::String& key, const ConfigValues& values )
{
  delValues( key, _data, true );
  _data.insert( _data.end(), values.begin(), values.end() );
}

ConfigValue ConfigData::getValue( const sc::String& key )
{
  ConfigValues values;
  getValues(key,_data,values,false);

  if( values.size() != 1 )
  {
    return NullValue;
  }

  return values.front();
}

void ConfigData::delValue( const sc::String& key )
{
  delValues( key, _data, false );
}

void  ConfigData::setValue( const sc::String& key, const ConfigValue& value )
{
  delValues( key, _data, false );
  _data.push_back(value);
}

void ConfigData::getValues( const sc::String& key, const ConfigValues& srcValues, ConfigValues& values, bool all )
{
  QString reStr = QString::fromUtf8(key).replace( ".", "\\." );
  if( all )
  {
    reStr += ".*";
  }
  QRegExp re(reStr);

  for( ConfigValues::const_iterator it = srcValues.begin(); it != srcValues.end(); it++ )
  {
    const ConfigValue& val = *it;

    if( re.exactMatch(QString::fromUtf8(val.getKey())) )
    {
      values.push_back(val);
    }
  }
}

void ConfigData::delValues( const sc::String& key, ConfigValues& values, bool all )
{
  ConfigValues tmp;
  values.swap(tmp);

  QString reStr = QString::fromUtf8(key).replace( ".", "\\." );
  if( all )
  {
    reStr += ".*";
  }
  QRegExp re(reStr);

  for( ConfigValues::const_iterator it = tmp.begin(); it != tmp.end(); it++ )
  {
    const ConfigValue& val = *it;

    if( ! re.exactMatch(QString::fromUtf8(val.getKey())) )
    {
      values.push_back(val);
    }
  }
}