summaryrefslogtreecommitdiffabout
path: root/microkde/kidmanager.cpp
blob: 8cf486a6475666196a2613ac8c449be0892b97eb (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
#include "kidmanager.h"

KIdManager::KIdManager()
{
   
}
// :profilename;12;id_withLen12;123456:
// 123456 is the csum
QString KIdManager::setId (const QString& idString,const QString& prof,const QString& idvalue )
{
    int startProf;
    int startIDnum;
    int startIDnumlen;
    int startID;
    int lenID;
    int startCsum;
    int lenCsum;
    int endall;
    QString newIDString;
    if ( KIdManager::getNumbers (idString, prof, startProf, startIDnum, startIDnumlen,startID, lenID, startCsum, lenCsum, endall) ) {
        newIDString = idString.left(startIDnum ) + QString::number( idvalue.length() ) +";"+idvalue+ ";"+idString.mid(  startCsum );
    } else {
        newIDString = idString + prof+";"+ QString::number( idvalue.length() ) +";"+idvalue +";0:";
    }
    //qDebug("setID:profile:%s*retval:%s*idvalue:%s* ", prof.latin1(), newIDString.latin1() ,idvalue.latin1() );
    return newIDString;
}
QString KIdManager::getId (const QString& idString,const QString& prof )
{
    int startProf;
    int startIDnum;
    int startIDnumlen;
    int startID;
    int lenID;
    int startCsum;
    int lenCsum;
    int endall;
    QString idval = "";
    if ( KIdManager::getNumbers (idString, prof, startProf, startIDnum, startIDnumlen,startID, lenID, startCsum, lenCsum, endall) )
        idval = idString.mid(  startID, lenID );

    //qDebug("getID:profile:%s*retval:%s*idstring:%s* ", prof.latin1(), idval.latin1() ,idString.latin1() );
    return idval;

}

QString KIdManager::removeId (const QString& idString,const QString& prof )
{ 
    int startProf;
    int startIDnum;
    int startIDnumlen;
    int startID;
    int lenID;
    int startCsum;
    int lenCsum;
    int endall;
    QString newIDString;
    if ( KIdManager::getNumbers (idString, prof, startProf, startIDnum, startIDnumlen,startID, lenID, startCsum, lenCsum, endall) ) {
        newIDString = idString.left(startProf) + idString.mid(  endall+1 );
    } else {
        newIDString = idString;
    }
    //qDebug("removeID:profile:%s*retval:%s*oldidstring:%s* ", prof.latin1(), newIDString.latin1() ,idString.latin1() );
    return newIDString;
}

QString KIdManager::setCsum (const QString& idString,const QString& prof,const QString& idCsum )
{
    int startProf;
    int startIDnum;
    int startIDnumlen;
    int startID;
    int lenID;
    int startCsum;
    int lenCsum;
    int endall;
    QString newIDString;
    if ( KIdManager::getNumbers (idString, prof, startProf, startIDnum, startIDnumlen,startID, lenID, startCsum, lenCsum, endall) ) {
        newIDString = idString.left(startCsum) + idCsum+ idString.mid(  endall +1);
    } else {
        newIDString = idString + prof + ";3;_u_;"+ idCsum + ":";
    }
    //qDebug("setCsum:profile:%s*retval:%s*idCsum:%s* ", prof.latin1(), newIDString.latin1() ,idCsum.latin1() );
    return newIDString;
}
QString KIdManager::getCsum (const QString& idString,const QString& prof )
{
    int startProf;
    int startIDnum;
    int startIDnumlen;
    int startID;
    int lenID;
    int startCsum;
    int lenCsum;
    int endall;
    QString idval = "";
    if ( KIdManager::getNumbers (idString, prof, startProf, startIDnum, startIDnumlen,startID, lenID, startCsum, lenCsum, endall) )
        idval = idString.mid(  startCsum, lenCsum );

    //qDebug("getCsum:profile:%s*retval:%s*idstring:%s* ", prof.latin1(), idval.latin1() ,idString.latin1() );
    return idval; 
}
// :profilename;12;id_withLen12;123456:
bool KIdManager::getNumbers (const QString& idString,const QString& prof, int &startProf, int &startIDnum, int &startIDnumlen,int &startID, int& lenID, int &startCsum, int &lenCsum, int & endall)
{
    startProf = idString.find( ":"+prof+";" );
    if ( startProf >= 0 ) {
        startIDnum = prof.length()+2+startProf;
        startID = idString.find( ";", startIDnum ) +1;
        startIDnumlen = startID - startIDnum - 1;
        if ( startIDnum > 0 ) {
            bool ok;
            lenID = idString.mid ( startIDnum,startIDnumlen).toInt( &ok );
            if (ok) {
                startCsum = startID+lenID+1;
                endall =  idString.find( ":", startCsum )-1;
                if ( endall < 0 ) {
                    qDebug("Error getNumbers: andall not found ");
                    return false;
                }
                lenCsum = endall-startCsum+1;
            }
            else {
                qDebug("Error getNumbers:length is no number:*%s* ", idString.mid ( startIDnum,startIDnumlen).toInt( &ok ));
                return false;
            }
        } else {
            qDebug("Error in KIdManager::getNumbers.startIDnum <= 0");
            return false;
        }
    } else {
        //qDebug("getnumbers: profile not found *%s* ",prof.latin1() );
        return false;
    }
    return true;
}