~ubuntu-branches/ubuntu/quantal/config-manager/quantal

« back to all changes in this revision

Viewing changes to src/Path.cc

  • Committer: Bazaar Package Importer
  • Author(s): Anand Kumria
  • Date: 2004-07-19 22:27:50 UTC
  • mto: (3.1.1 dapper)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040719222750-sztqdj1aoj2r6frr
Tags: upstream-0.1p83
ImportĀ upstreamĀ versionĀ 0.1p83

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2002 Robert Collins.
 
3
 *
 
4
 *     This program is free software; you can redistribute it and/or modify
 
5
 *     it under the terms of the GNU General Public License as published by
 
6
 *     the Free Software Foundation; either version 2 of the License, or
 
7
 *     (at your option) any later version.
 
8
 *
 
9
 *     A copy of the GNU General Public License can be found at
 
10
 *     http://www.gnu.org/
 
11
 *
 
12
 * Written by Robert Collins <robertc@hotmail.com>
 
13
 *
 
14
 */
 
15
 
 
16
#include "Path.h"
 
17
 
 
18
Path::Path () :  _name (), _basename(), _dirname ()
 
19
{}
 
20
 
 
21
Path::Path (Path const &old) :  _name (old._name), _basename(old._basename), _dirname (old._dirname)
 
22
{
 
23
}
 
24
 
 
25
Path::Path (string const &aDir)
 
26
{
 
27
    setToString (aDir);
 
28
}
 
29
 
 
30
Path::~Path(){}
 
31
 
 
32
Path& 
 
33
Path::operator= (string const &aDir)
 
34
{
 
35
    setToString (aDir);
 
36
    return *this;
 
37
}
 
38
 
 
39
Path& 
 
40
Path::operator= (Path const &old)
 
41
{
 
42
    _name = old._name;
 
43
    _basename = old._basename;
 
44
    _dirname = old._dirname;
 
45
    return *this;
 
46
}
 
47
 
 
48
Path
 
49
Path::operator + (Path const &rhs) const
 
50
{
 
51
    /* Path a + Path b */
 
52
    /* thoughts:
 
53
     * case 1) b is relative (does not start with /)
 
54
     * case 2) b is absolute, throw exception.
 
55
     * case 1a) b has more .. elements than a has elements. Throw exception.
 
56
     * XXX if needed, implement the above rules.
 
57
     * for now: concat the full names, and set to string.
 
58
     */
 
59
    Path result (_name + "/" + rhs._name);
 
60
    return result;
 
61
}
 
62
 
 
63
void
 
64
Path::StripSlash (string &aString)
 
65
{
 
66
    if (aString[aString.size() - 1] == '/' && aString.size() > 1)
 
67
      aString.erase (aString.size() - 1);
 
68
}
 
69
 
 
70
void
 
71
Path::setToString (string const &aDir)
 
72
{
 
73
    if (!aDir.size())
 
74
        throw (string) "Illegal Path \"\"";
 
75
    _name = aDir;
 
76
    StripSlash (_name);
 
77
    _dirname = _name;
 
78
    unsigned int offset = _dirname.rfind ("/");
 
79
    if (offset != string::npos) {
 
80
        _basename = _dirname.substr (offset + 1);
 
81
        _dirname.erase(offset);
 
82
        /* dirname must be '/' at a minimum when a '/' was provided */
 
83
        if (!_dirname.size())
 
84
            _dirname = '/';
 
85
    } else {
 
86
        _dirname = '.';
 
87
        _basename = _name;
 
88
    }
 
89
}
 
90
 
 
91
/*
 
92
 *   1. If string is //, skip steps 2 to 5.
 
93
 
 
94
   2. If string consists entirely of slash characters, string will be set to a single slash character. In this case, skip steps 3 to 8.
 
95
 
 
96
   3. If there are any trailing slash characters in string, they will be removed.
 
97
 
 
98
   4. If there are no slash characters remaining in string, string will be set to a single period character. In this case, skip steps 5 to 8.
 
99
 
 
100
   5. If there are any trailing non-slash characters in string, they will be removed.
 
101
 
 
102
   6. If the remaining string is //, it is implementation-dependent whether steps 7 and 8 are skipped or processed.
 
103
 
 
104
   7. If there are any trailing slash characters in string, they will be removed.
 
105
 
 
106
   8. If the remaining string is empty, string will be set to a single slash character.
 
107
 
 
108
The resulting string will be written to standard output. 
 
109
*/