summaryrefslogtreecommitdiff
path: root/ept/token.h
blob: ecacc16de6191a333e0d54fd8ec26a51030ee12c (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
// -*- C++ -*-
#include <wibble/mixin.h>
#include <string>

#ifndef EPT_TOKEN_H
#define EPT_TOKEN_H

namespace ept {

struct Token : wibble::mixin::Comparable< Token > {
    std::string _id; // formatted as package[_version]
    std::string id() const { return _id; }

    Token() : _id( "" ) {}
    Token( std::string s ) : _id( s ) {}

    std::string version() const {
        return _id.find( '_' ) == std::string::npos ? "" :
            std::string( _id, _id.find( '_' ) + 1, _id.size() );
    }

    std::string package() const {
        return std::string( _id, 0,
                            _id.find( '_' ) == std::string::npos ?
                            _id.size() : _id.find( '_' ) );
    }

    bool isDesktop() const {
        return std::string( _id, 0, 8 ) == "desktop:";
    }

    std::string desktop() const {
        return isDesktop() ? std::string( _id, 8, _id.size() ) : "";
    }

    bool hasVersion() const {
        return version() != "";
    }

    bool valid() const {
        return _id != "";
    }

    bool operator<=( const Token &o ) const {
        return _id <= o._id;
    }
};

}

inline std::ostream &operator<<( std::ostream &o, const ept::Token &t ) {
    return o << t.id();
}

#endif