Annotation of researchv10no/cmd/asd++/path.c, revision 1.1.1.1

1.1       root        1: #include "decl.h"
                      2: 
                      3: Path
                      4: mkpath (String st)
                      5: {
                      6:        Path p;
                      7: 
                      8:        p.s = st;
                      9:        return p;
                     10: }
                     11: 
                     12: Path
                     13: Path::operator & (const Path& sub) const
                     14: {
                     15:        String dir = s;
                     16:        String subdir = sub.s;
                     17: 
                     18:        if (subdir.length() == 0)
                     19:                return *this;
                     20: 
                     21:        if (subdir[0] == '/' || dir.length() == 0)
                     22:                return sub;
                     23: 
                     24:        if (dir == "/")
                     25:                return mkpath ('/' + subdir);
                     26: 
                     27:        return mkpath (dir + '/' + subdir);
                     28: }
                     29: 
                     30: Path
                     31: Path::first() const
                     32: {
                     33:        int n = s.strchr('/');
                     34: 
                     35:        if (n < 0)
                     36:                return s;
                     37: 
                     38:        if (n == 0)
                     39:                return "/";
                     40: 
                     41:        return mkpath (s(0,n));
                     42: }
                     43: 
                     44: Path
                     45: Path::last() const
                     46: {
                     47:        int n = s.strrchr('/');
                     48: 
                     49:        if (n < 0)
                     50:                return mkpath (s);
                     51: 
                     52:        return mkpath (s(n+1,s.length()-n-1));
                     53: }
                     54: 
                     55: Path
                     56: Path::rmfirst() const
                     57: {
                     58:        int n = s.strchr('/');
                     59: 
                     60:        if (n < 0)
                     61:                return mkpath ("");
                     62: 
                     63:        return mkpath (s(n+1,s.length()-n-1));
                     64: }
                     65: 
                     66: Path
                     67: Path::rmlast() const
                     68: {
                     69:        int n = s.strrchr('/');
                     70: 
                     71:        if (n < 0)
                     72:                return mkpath ("");
                     73: 
                     74:        if (n == 0)
                     75:                return mkpath ("/");
                     76: 
                     77:        return mkpath (s(0,n));
                     78: }
                     79: 
                     80: Path homedir()
                     81: {
                     82:        char *p = getenv ("HOME");
                     83:        return p? p: "";
                     84: }
                     85: 
                     86: ostream& operator << (ostream& o, const Path& p)
                     87: {
                     88:        o << p.s;
                     89:        return o;
                     90: }
                     91: 
                     92: // remove multiple or trailing slashes
                     93: String
                     94: pathnorm (String s)
                     95: {
                     96:        String r;
                     97:        int l = s.length();
                     98:        int state = 0;
                     99: 
                    100:        for (int i = 0; i < l; i++) {
                    101:                char c = s[i];
                    102:                switch (state) {
                    103:                case 0:                 // initial state
                    104:                        r += c;
                    105:                        if (c == '/')
                    106:                                state = 1;
                    107:                        else
                    108:                                state = 2;
                    109:                        break;
                    110: 
                    111:                case 1:                 // seen initial slashes only
                    112:                        if (c != '/') {
                    113:                                r += c;
                    114:                                state = 2;
                    115:                        }
                    116:                        break;
                    117: 
                    118:                case 2:                 // seen a component character
                    119:                        if (c != '/')
                    120:                                r += c;
                    121:                        else
                    122:                                state = 3;
                    123:                        break;
                    124: 
                    125:                case 3:                 // seen a non-leading slash
                    126:                        if (c != '/') {
                    127:                                r += '/';
                    128:                                r += c;
                    129:                                state = 2;
                    130:                        }
                    131:                        break;
                    132:                }
                    133:        }
                    134: 
                    135:        return r;
                    136: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.