File: files.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 (247 lines) | stat: -rw-r--r-- 5,591 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <string>
#include <vector>
#include <fstream>
#include <sys/types.h>
#include <iostream>
using namespace std;

#include "category.h"
#include "currents.h"
#include "files.h"
#include "globals.h"
#include "utility.h"
#include "errors.h"
#include "navigation.h"
#include "parser.h"


#ifdef GUI
#include <qstatusbar.h>
#include <qmessagebox.h>

#include "http.h"
#include "preferences.h"
#include "browser.h"
#endif

// load a dii file, or one of its mirrors and go to the root of it. first check to see 
// if this file, or one of its mirrors is contained in the dii file cache.

int loadDIIFile( string path, string file )
{
        Currents c; c.save();
        
//	cout << "LOAD FILE " + path + "|" + file << endl;

        vector<string> mirrors;
        dirToVec( path, mirrors, ',', TRUE );

        if( mirrors.empty() )
                mirrors.push_back( "" );

	aPath = mirrors[ mirrors.size()-1 ];

        // have we cached one of the mirrors ?

        int entry;
        for( unsigned int n = 0; n < mirrors.size(); n++ )
                if( cached( mirrors[n], file, entry ) )
                {
#ifndef GUI		
                        if( !checkingstructure )
                                cout << "( " << slashConcat( mirrors[n], file ) << " already parsed )" << endl;
#endif				

                        cCategory = cacheCatv[ entry ];
			aPath = cacheAddressv[ 3*entry ];
			cPath = cacheAddressv[ 3*entry+1 ];
			cFile = cacheAddressv[ 3*entry+2 ];

                        return TRUE;
                }

	// when checking structure, we don't load new files

        if( checkingstructure )
                return FALSE;

        // try if we can open the file, or one of the mirrors

        int n;
        string extdir;
        for( n = mirrors.size()-1; n >= 0; n-- )
        {
                extdir = slashConcat( mirrors[n], file );

                if( readFile( extdir ) )
                        break;

		if( aborted )
			BACKTRACK
        }

	// if that fails, try the cache server
	
	if( n == -1 )
	{
		string cacheaddress;
		string path = mirrors[ mirrors.size()-1 ];

	 	if( path.find( "http://" ) == 0 )
			cacheaddress = "http://cache:" + path.substr( 7, path.length()-7 );
	
		aborted = FALSE;
		if( ( cacheaddress == "" ) ||
#ifdef GUI		    
		    ( config->cacheServer() == "" ) ||
#endif    					   
		    ( !readFile( slashConcat( cacheaddress, file ) ) ) ) 				
		{		
			if( aborted )
				BACKTRACK

			string msg = "could not open:";
	        	for( unsigned int n = 0; n < mirrors.size(); n++ )
				msg += "\n" + slashConcat( mirrors[n], file );
			showNavigationError( msg );
	
			browser->statusBar()->message( "Ready.." );

			BACKTRACK
		}	

		cPath = cacheaddress;	
	}
	else
		cPath = mirrors[n];

        cFile = file;

        if( !( cCategory = processDIIFile( extdir ) ) )
		BACKTRACK 

        cacheCatv.push_back( cCategory );
	cacheAddressv.push_back( aPath );
	cacheAddressv.push_back( cPath );
	cacheAddressv.push_back( cFile );
        cacheSourcev.push_back( sourcev );

        return TRUE;
}

// given the address of a file, try to read its contents in vector<string> *sourcev.

int readFile( string address )
{
//	cout << "read " << address << endl;

    	if( address.find( "http://" ) == 0 )
    	{
#ifdef GUI
		if ( address.find( "cache:" ) == 7 )
			address = slashConcat( config->cacheServer(), "requestfile.php?url=http://" +
				  address.substr( 13, address.length()-13 ) ); 	
		
		Http http;
		return http.getFile( address );
#else		
		if( address.find( "cache:" ) == 7 )
			address = "c:/cache/" + address.substr( 13, address.length()-13 );
		else
	    		address = "c:/" + address.substr( 7, address.length()-7 );
#endif
	}

    	ifstream ifs;

    	ifs.open( address.c_str() );
    	if( !ifs.good() )
        	return FALSE;

    	ifs.unsetf( ios::skipws );

    	string line;
    	sourcev = new vector<string>;

    	while( !ifs.eof() )
    	{
        	getline( ifs, line, '\n' );
        	sourcev->push_back( line );
    	}

    	ifs.close();
    	return TRUE;
}

// destroy everything that is cached ( categories, source files, .. )

void clearCache()
{
	Currents c; c.save();

	for( unsigned int n = 0; n < cacheCatv.size(); n++ )
	{
		delete cacheCatv[n];
		delete cacheSourcev[n];
	}	
		
	cacheCatv.clear();
	cacheAddressv.clear();
	cacheSourcev.clear();

	cCategory = 0;

	for( unsigned int n = 0; n < backTracev.size(); n++ )
		backTracev[n].c_selectionentered = FALSE;

	if( clear_goto( c.fullpath ) )
		c.load_nosel();
}

// check cache for occurrence of file

int cached( string path, string file, int &entry )
{
    for( entry = 0; entry < cacheCatv.size(); entry++ )
        if( ( cacheAddressv[3*entry+1] == path ) && ( cacheAddressv[3*entry+2] == file ) )
  	    return TRUE;
    return FALSE;
}

void launchExtension( string address )
{
#ifdef GUI
	launchExtension( config->webBrowser(), address );
#else
	cout << "launch extension on:" << endl << address << endl;
#endif	
}

void launchExtension( string application, string address )
{
#ifdef DEBUGOUTPUT
	cout << "exec " + application + " " + address << endl;
#endif	
#ifdef GUI
	if( application == "" )
	{
		QMessageBox::warning( browser, "Error", "Please enter an application for this purpose" );

		config->show();
		return;
	}
	
	browser->timedMessage( "Launching..", 2000 );

/*	if( vfork() == 0 )
	{
		if( execlp( application.c_str(), application.c_str(), address.c_str(), (char *) 0 ) < 0 ) 
			cout << "could not launch application" << endl;
		exit(0);
	} 
*/

	system( string( application + " " + address + "&" ).c_str() );
#endif	
}