summaryrefslogtreecommitdiff
path: root/logbook/countrydat.cpp
blob: 1d88e059e99cef9c863875fce30648764e1c964b (plain)
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
128
129
130
131
132
133
134
135
136
/*
#Sov Mil Order of Malta:   15:  28:  EU:   41.90:   -12.43:    -1.0:  1A:
    #1A;
#Spratly Islands:          26:  50:  AS:    9.88:  -114.23:    -8.0:  1S:
    #1S,9M0,BV9S;
#Monaco:                   14:  27:  EU:   43.73:    -7.40:    -1.0:  3A:
    #3A;
#Heard Island:             39:  68:  AF:  -53.08:   -73.50:    -5.0:  VK0H:
    #=VK0IR;
#Macquarie Island:         30:  60:  OC:  -54.60:  -158.88:   -10.0:  VK0M:
    #=VK0KEV;
#Cocos-Keeling:            29:  54:  OC:  -12.15:   -96.82:    -6.5:  VK9C:
    #AX9C,AX9Y,VH9C,VH9Y,VI9C,VI9Y,VJ9C,VJ9Y,VK9C,VK9Y,VL9C,VL9Y,VM9C,VM9Y,
    #VN9C,VN9Y,VZ9C,VZ9Y,=VK9AA;
*/


#include "countrydat.h"
#include <QFile>
#include <QTextStream>


void CountryDat::init(const QString filename)
{
    _filename = filename;
    _data.clear();
}

QString CountryDat::_extractName(const QString line)
{
    int s1 = line.indexOf(':');
    if (s1>=0)
    {
        QString name = line.mid(0,s1);
        return name;
    }
    return "";
}

void CountryDat::_removeBrackets(QString &line, const QString a, const QString b)
{
    int s1 = line.indexOf(a);
    while (s1 >= 0)
    {
      int s2 = line.indexOf(b);
      line = line.mid(0,s1) + line.mid(s2+1,-1);
      s1 = line.indexOf(a);
    }
}    

QStringList CountryDat::_extractPrefix(QString &line, bool &more)
{
    line = line.remove(" \n");
    line = line.replace("=","");
    line = line.replace(" ","");

    _removeBrackets(line,"(",")");
    _removeBrackets(line,"[","]");
    _removeBrackets(line,"<",">");
    _removeBrackets(line,"~","~");

    int s1 = line.indexOf(';');
    more = true;
    if (s1 >= 0)
    {
      line = line.mid(0,s1);
      more = false;
    }

    QStringList r = line.split(',');

    return r;
}


void CountryDat::load()
{
    _data.clear();
    _countryNames.clear(); //used by countriesWorked
  
    QFile inputFile(_filename);
    if (inputFile.open(QIODevice::ReadOnly))
    {
       QTextStream in(&inputFile);
       while ( !in.atEnd() )
       {
          QString line1 = in.readLine();
          if ( !in.atEnd() )
          {
            QString line2 = in.readLine();
              
            QString name = _extractName(line1);
            if (name.length()>0)
            {
                _countryNames << name;
                bool more = true;
                QStringList prefixs;
                while (more)
                {
                    QStringList p = _extractPrefix(line2,more);
                    prefixs += p;
                    if (more)
                        line2 = in.readLine();
                }

                QString p;
                foreach(p,prefixs)
                {
                    if (p.length() > 0)
                        _data.insert(p,name);
                }
            }
          }
       }
    inputFile.close();
    }
}

// return country name else ""
QString CountryDat::find(const QString prefix)
{
    QString pf = prefix.toUpper();
    while(pf.length() >= 1)
  	{
      if (_data.contains(pf))
	  {
        QString country = _data.value(pf);
	    return country;
	   }
       pf = pf.left(pf.length()-1);
	 }
	 return "";
}