File: addressing.cpp

package info (click to toggle)
kascade 1.0-beta10-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 964 kB
  • ctags: 815
  • sloc: cpp: 7,780; makefile: 39
file content (209 lines) | stat: -rw-r--r-- 5,640 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <string>
#include <vector>
#include <iostream>
using namespace std;

#include "addressing.h"
#include "utility.h"
#include "globals.h"
#include "errors.h"

int parseAddress( string address, int &p_dii, string &p_extdir, string &p_file, string &p_intdir )
{
//	if( !checkingstructure ) cout << "PARSE |" << address << "|" << endl;

        p_dii = FALSE;
        p_extdir = p_file = p_intdir = "";
        string site;
        string rest;
        vector<string> mirrorv;
        int i;
	unsigned int j;
        vector<string> dirs;

        // save mirror locations

        if( ( i = address.find_last_of( ',' ) ) != -1 )
        {
		dirToVec( address.substr( 0, i ), mirrorv, ',', FALSE );
                address = trim( address.substr( i+1, address.length()-i-1 ) );
        }

        // internal absolute address

        if( address.find_first_of( '/' ) == 0 )
        {
                dirToVec( address.substr( 1, address.length()-1 ), dirs, '/', FALSE );

		if( !vecHasDot( dirs ) )
		{	
			p_intdir = address;
			return TRUE;
		}	
        } 

        // check if address starts with absolute 'site'

        if( address.find( "http://" ) == 0 )
        {
                // http://ms.com
                if( ( i = address.find_first_of( '/', 7 ) ) == -1 )
                {
                        p_extdir = address;
                        return TRUE;
                }

                site = address.substr( 0, i+1 );
                rest = address.substr( i+1, address.length()-i-1 );
        }
        else if( ( address.find( ":/" ) == 1 ) &&
                 ( address[0] >= 'A' ) &&
                 ( address[0] <= 'z' ) )
        {
                site = address.substr( 0, 3 );
                rest = address.substr( 3, address.length()-3 );
        }
        else if( address.find( "/" ) == 0 )
        {
                site = "/";
                rest = address.substr( 1, address.length()-1 );
        }

	// handle absolute addresses
	
        if( site != "" )
        {
                dirToVec( rest, dirs, '/', FALSE );

                if( !vecHasDii( dirs, j ) )
                {
                        p_extdir = address;
                        return TRUE;
                }

        	if( mirrorv.size() )
			for( unsigned int n = 0; n < mirrorv.size(); n++ )
			{
				if( !compactPath( mirrorv[n], dirs[j] ) )
					return FALSE;
				p_extdir += mirrorv[n] + ",";
        		}
		
		string location = slashConcat( site, vecToDir( dirs, 0, j, "/" ) );
		if( !compactPath( location, dirs[j] ) )
			return FALSE;

		p_dii = TRUE;
                p_extdir += location;
                p_file = dirs[j];
                p_intdir = vecToDir( dirs, j+1, dirs.size()-j-1, "/" );

                return TRUE;
        }

        // handle relative addresses

        dirToVec( address, dirs, '/', FALSE );

        // internal relative

        if( !vecHasDot( dirs ) )
        {
                p_intdir = address;
                return TRUE;
        }

        // external relative

	if( !vecHasDii( dirs, j ) )
	{
		p_extdir = slashConcat( cPath, address );
		return TRUE;
	}
	
        if( mirrorv.size() )
		for( unsigned int n = 0; n < mirrorv.size(); n++ )
		{	
			string mirror = slashConcat( cPath, mirrorv[n] );
			if( !compactPath( mirror, dirs[j] ) )
				return FALSE;
                        p_extdir += mirror + ",";
                }
	
        p_dii = TRUE;
        p_extdir += slashConcat( cPath, vecToDir( dirs, 0, j, "/" ) );
        p_file = dirs[j];
        p_intdir = vecToDir( dirs, j+1, dirs.size()-j-1, "/" );

        return compactPath( p_extdir, p_file );
}

int compactPath( string &path, string file )
{
//        if( !checkingstructure ) cout << "COMPACT PATH " + path << endl;

        string site;
        string rest = path;
        int i;

        // first, remove site

        if( path.find( "http://" ) == 0 )
        {
                i = path.find_first_of( '/', 7 );

                site = path.substr( 0, i+1 );
                rest = path.substr( i+1, path.length()-i-1 );
        }
        else if( ( path.find( ":/" ) == 1 ) &&
                 ( path[0] >= 'A' ) &&
                 ( path[0] <= 'z' ) )
        {
                site = path.substr( 0, 3 );
                rest = path.substr( 3, path.length()-3 );
        }
        else if( path.find( "/" ) == 0 )
        {
                site = "/";
                rest = path.substr( 1, path.length()-1 );
        }

        // compact rest

        vector<string> dirv;
        dirToVec( rest, dirv, '/', FALSE );

        int foundone;
        do
        {
                foundone = FALSE;

                for( unsigned int n = 0; n < dirv.size(); n++ )
                        if( dirv[n] == "." )
                        {
                                dirv.erase( dirv.begin()+n, dirv.begin()+n+1 );
                                foundone = TRUE;
                        }
                        else if( ( dirv[n] == ".." ) &&
                                 ( n != 0 ) &&
                                 ( dirv[n-1] != ".." ) )
                        {
                                dirv.erase( dirv.begin()+n-1, dirv.begin()+n+1 );
                                foundone = TRUE;
                        }
        }
        while( foundone );

        path = site + vecToDir( dirv, 0, dirv.size(), "/" );

        if( ( dirv.size() != 0 ) &&
            ( dirv[0] == ".." ) &&
            ( site != "" ) )
        {
                showNavigationError( slashConcat( path, file ) + ":\n" + "nonsensical address" );
                return FALSE;
        }

        return TRUE;
}