~ubuntu-branches/ubuntu/quantal/openmsx-debugger/quantal

« back to all changes in this revision

Viewing changes to src/Convert.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Joost Yervante Damad
  • Date: 2009-12-06 07:40:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091206074002-kssfkg1d6xbp6w9e
Tags: 0.0.0.svn20091206-1
New svn snapshot (Closes: #559612)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "Convert.h"
2
2
#include <QString>
3
3
 
4
 
int stringToValue( const QString& str )
 
4
int stringToValue(const QString& str)
5
5
{
6
6
        QString s = str.trimmed();
7
7
        int base = 10;
8
 
        
9
 
        // find base prefix or postfix
10
 
        if( s.startsWith("&" ) ) {
11
 
                switch( s[1].toUpper().toAscii() ) {
12
 
                        case 'H':
13
 
                                base = 16;
14
 
                                break;
15
 
                        case 'B':
16
 
                                base = 2;
17
 
                                break;
18
 
                        case 'O':
19
 
                                base = 8;
20
 
                                break;
 
8
 
 
9
        // find base (prefix or postfix)
 
10
        if (s.startsWith("&") && s.size() >= 2) {
 
11
                switch (s[1].toUpper().toAscii()) {
 
12
                case 'H':
 
13
                        base = 16;
 
14
                        break;
 
15
                case 'B':
 
16
                        base = 2;
 
17
                        break;
 
18
                case 'O':
 
19
                        base = 8;
 
20
                        break;
21
21
                }
22
22
                s = s.remove(0, 2);
23
 
        } else if( s.startsWith("#") || s.startsWith("$") ) {
 
23
        } else if (s.startsWith("#") || s.startsWith("$")) {
24
24
                base = 16;
25
25
                s = s.remove(0, 1);
26
 
        } else if( s.startsWith("0x") ) {
 
26
        } else if (s.startsWith("0x")) {
27
27
                base = 16;
28
28
                s = s.remove(0, 2);
29
 
        } else if( s.startsWith("%") ) {
 
29
        } else if (s.startsWith("%")) {
30
30
                base = 2;
31
31
                s = s.remove(0, 1);
32
 
        } else {
33
 
                switch( s.right(1)[0].toUpper().toAscii() ) {
 
32
        } else if (!s.isEmpty()) {
 
33
                switch (s.right(1)[0].toUpper().toAscii()) {
34
34
                        case 'H':
35
35
                        case '#':
36
36
                                base = 16;
42
42
                                base = 8;
43
43
                                break;
44
44
                }
45
 
                if( base != 10 ) s.chop(1);
 
45
                if (base != 10) s.chop(1);
46
46
        }
 
47
 
47
48
        // convert value
48
49
        bool ok;
49
 
        int value = s.toInt( &ok, base );
50
 
        if( ok )
51
 
                return value;
52
 
        else
53
 
                return -1;
 
50
        int value = s.toInt(&ok, base);
 
51
        if (!ok) return -1;
 
52
        return value;
54
53
}