summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/IdentityUtil.cpp
blob: bf089c9b8c00873467f0a3e6dfea13e749b04c36 (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
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/IdentityUtil.h>
#include <Ice/StringUtil.h>
#include <Ice/LocalException.h>

using namespace std;
using namespace Ice;
using namespace IceInternal;

ostream&
Ice::operator<<(ostream& out, const Identity& ident)
{
    return out << identityToString(ident);
}

Identity
Ice::stringToIdentity(const string& s)
{
    Identity ident;

    //
    // Find unescaped separator
    //
    string::size_type slash = 0;
    while((slash = s.find('/', slash)) != string::npos)
    {
        if(slash == 0 || s[slash - 1] != '\\')
        {
            break;
        }
        slash++;
    }

    if(slash == string::npos)
    {
        if(!decodeString(s, 0, 0, ident.name))
        {
            throw SyscallException(__FILE__, __LINE__);
        }
    }
    else
    {
        if(!decodeString(s, 0, slash, ident.category))
        {
            throw SyscallException(__FILE__, __LINE__);
        }
        if(slash + 1 < s.size())
        {
            if(!decodeString(s, slash + 1, 0, ident.name))
            {
                throw SyscallException(__FILE__, __LINE__);
            }
        }
    }

    return ident;
}

string
Ice::identityToString(const Identity& ident)
{
    if(ident.category.empty())
    {
	return encodeString(ident.name, "/");
    }
    else
    {
	return encodeString(ident.category, "/") + '/' + encodeString(ident.name, "/");
    }
}