File: navigation.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 (790 lines) | stat: -rw-r--r-- 16,949 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
#include <iostream>
#include "category.h"
#include "currents.h"
#include "globals.h"
#include "navigation.h"
#include "parser.h"
#include "addressing.h"
#include "utility.h"
#include "errors.h"
#include "summation.h"
#include "expression.h"
#include "exprnode.h"
#include "tracing.h"
#include "files.h"

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

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

// enter a category and follow references. used in many places.
// this is the centerpiece of the navigation module.

int enter( Category *category )
{
	//if( !checkingstructure ) cout << "ENTER " + category->name << endl;

        if( !checkLoop( category ) )
                return FALSE;

	checkWarning( category );
	
        Currents c; c.save();
	
	cCategory = category->parent;

        if( category->summation )
        {
		if( checkingstructure )
		{	
			cCategory = category;
			return TRUE;
		}
		
		if( !category->expanded )
		{
			vector<Category *> s_loopCatv = loopCatv;
                    	if( !expandSummation( category ) )
                        	BACKTRACK
			loopCatv = s_loopCatv;	
		}

		cCategory = category;		
        }
        else if( category->target != "" )
        {
                if( category->mask )
                {
			if( checkingstructure )
			{
				cCategory = category;
				return TRUE;
			}

                        if( !enterMask( category ) )
                                BACKTRACK
			category->visited = TRUE;	
                }
                else if( category->selection )
                {
                        if( !enterSelection( category ) )
                                BACKTRACK
                }
                else if( !enterNormal( category ) )
			BACKTRACK
        }
        else
                cCategory = category;

        checkMask( category );

        return TRUE;
}

// enter a category that has a normal reference; handle passed and user arguments

int enterNormal( Category *category )
{
	string target = category->target;
	vector<string> argumentv;

	if( !getUserArgs( target ) )
		return FALSE;

	// expand arguments
	int i;
	for( unsigned int n = 0; n < argv.size(); n += 2 )
		while( ( i = target.find( argv[n] ) ) != -1 )
			target.replace( i, argv[n].length(), argv[n+1] );

	if( !parseArguments( target, argumentv, category ) ) 
		return FALSE;
					
	Currents c; c.save();

	for( unsigned int n = 0; n < argumentv.size(); n += 2 )
		if( !vectorContains( argv, argumentv[n], 2 ) )
		{
			argv.push_back( argumentv[n] );
			argv.push_back( argumentv[n+1] );
		}	

	if( !gotoAddress( target, category ) )
		BACKTRACK
	return TRUE;
}

// prompt user for required arguments

int getUserArgs( string &address )
{
	if( checkingstructure )
		return TRUE;

	int i, j;
	while( ( i = address.find( "%" ) ) != -1 )
	{	
		if( ( j = address.find( "%", i+1 ) ) == -1 )
			return TRUE;

		string arg;
#ifdef GUI
		if( !getInput( arg, address.substr( i+1, j-i-1 ) ) )
			return FALSE;
#else
		cout << "enter " << address.substr( i+1, j-i-1 ) << ":" << endl;
		cin >> arg;
#endif

		address.replace( i, j-i+1, arg );
	}

	return TRUE;
}

// parse arguments, if present

int parseArguments( string &address, vector<string> &argumentv, Category *category )
{
	int i;
	if( ( i = address.find( ';' ) ) == -1 )
		return TRUE;

	string args = address.substr( i+1, address.length()-i-1 );
	address = trim( address.substr( 0, i ) );

	vector<string> newv;
	dirToVec( args, newv, ',', FALSE );

	for( unsigned int n = 0; n < newv.size(); n++ )
	{
		if( ( i = newv[n].find( '=' ) ) <= 0 )
		{
			showNavigationError( category, "syntax error in argument definition" );
			return FALSE;
		}
			
		argumentv.push_back( "_" + trim( newv[n].substr( 0, i ) ) );
		argumentv.push_back( trim( newv[n].substr( i+1, newv[n].length()-i-1 ) ) );
	}
		
	return TRUE;
}

// check to see if we're not entering a reference loop.
// used by enter( .. )

int checkLoop( Category *category )
{
        for( unsigned int n = 0; n < loopCatv.size(); n++ )
                if( loopCatv[n] == category )
                {
                        if( !checkingstructure )
                                showNavigationError( category, "category (in)directly points to itself ; )" );
                        return FALSE;
                }

        loopCatv.push_back( category );
        return TRUE;
}

// drop mask expressions when we have entered a real subcat
// used by enter( .. )

void checkMask( Category *category )
{
        if( category->realsubcat )
        {
                maskv.clear();
                masksv.clear();
        }
}

// automatically enter the right subcategories if selection is active.
// used by enter( .. )

int checkSelection( )
{
        if( selectionv.empty() || !cCategory->subCatv )
                return TRUE;

	int found = FALSE;
        for( unsigned int n = 0; n < loopCatv.size(); n++ )
		if( loopCatv[n]->realsubcat )
		{
			found = TRUE;
			break;
		}
	if( !found ) 
		return TRUE;
	
        Currents c; c.save();

        string selection = selectionv[ selectionv.size()-1 ];
        selectionv.pop_back();

	vector<Category *> s_loopCatv = loopCatv;
	loopCatv.clear(); // experimental

        if( !gotoInternal( selection ) )
        {
                selectionentered = FALSE;
                selectionv.push_back( selection );
                return FALSE;
        }

	loopCatv = s_loopCatv;
        selectionentered = TRUE;
        skippedCat = c.c_cCategory;
        return TRUE; 
}

// enter category that has a selection reference. used by enter( .. )

int enterSelection( Category *category )
{
        //if( !gotoAddress( category->target, category ) )
	if( !enterNormal( category ) )
                return FALSE;
	
	string selection = (*(category->data1v))[0];

	for( unsigned int n = 0; n < selectionv.size(); n++ )
		if( selectionv[n] == selection )
			return TRUE;
	
        // complex task: determine if selection should be added at
        // the front or at the back of selectionv.

        int nrofsel = 0;
        for( unsigned int n = 0; n < loopCatv.size(); n++ )
                if( loopCatv[n]->selection )
                        nrofsel++;
	
        if( nrofsel == 1 )
            selectionv.push_back( selection );
        else
            selectionv.insert( selectionv.begin(), selection );

        return TRUE;
}

// enter category that has a mask reference. used by enter( .. )

int enterMask( Category *category )
{
    	if( !gotoAddress( category->target, category ) )
        	return FALSE;

    	vector<ExprNode *> exprv;
    	vector<string> maskstringv;
    	string maskstring;

    	// parse mask expressions

    	for( unsigned int x = 0; x < category->data1v->size(); x++ )
    	{
        	maskstring = (*(category->data1v))[x];
        	ExprNode *expr = createExprTree( maskstring );
        	
		if( expr == 0 )
            		return FALSE;

        	exprv.push_back( expr );
        	maskstringv.push_back( maskstring );
    	}

    	// update the current mask vector

    	for( unsigned int x = 0; x < exprv.size(); x++ )
    	{
        	int exists = FALSE;

        	for( unsigned int y = 0; y < masksv.size(); y++ )
	    	if( masksv[y] == maskstringv[x] )
                	exists = TRUE;

        	if( !exists )
       	 	{
            		maskv.push_back( exprv[x] );
            		masksv.push_back( maskstringv[x] );
        	}
   	} 

    	return TRUE;
}

// take us to a certain address. 
// consists of three functions, of which gotoRecursive does the real work

int gotoAddress( string address )
{
	return gotoAddress( address, 0 );
}

int gotoAddress( string address, Category *category )
{
    	Currents c; c.save();

    	if( !gotoRecursive( address, category ) )
        	BACKTRACK;

    	return TRUE;
}

int gotoRecursive( string address, Category *category )
{
    	int p_dii;
    	string p_extdir, p_file, p_intdir;
	
//	if( category && !checkingstructure  )
//		cout << "GOTOREC " + address + " CAT " + category->name << endl;

    	if( !parseAddress( address, p_dii, p_extdir, p_file, p_intdir ) )
        	return FALSE;

/*    	if( !checkingstructure )
    		cout << "DII " << p_dii << endl << "EXTDIR " << p_extdir << endl << "FILE " << p_file << endl << "INTDIR " << p_intdir << endl; */

    	if( p_dii )
    	{
        	if( !loadDIIFile( p_extdir, p_file ) )
		{
			if( checkingstructure )
				cCategory = category;
			
            		return FALSE;
		}	
    	}
    	else if( p_extdir != "" )
    	{
		extensionlaunched = TRUE;
		extensionaddress = p_extdir;

        	if( checkingstructure )
		{
			cCategory = category;
            		return TRUE;
		}	

		launchExtension( p_extdir );

		category->visited = TRUE;
        	return FALSE;
    	}

    	if( ( p_intdir != "" ) &&
            !gotoInternal( p_intdir ) ) // (remaining) rel. address
        	return FALSE;

    	return TRUE;
}

// used by gotoRecursive(..) for internal addresses. in turn calls enter(..)

int gotoInternal( string address )
{
    	//cout << "INTERNAL " << address << endl << "FROM " << cCategory->name << endl;

	// handle internal absolute prefix '/'
	
	if( ( address != "" ) &&
	    ( address[0] == '/' ) )
	{
		address.erase( 0, 1 );
	
		while( cCategory->parent )
			cCategory = cCategory->parent;
	}
	
    	if( address == "" )
        	return TRUE;

	// process directories one at a time
	
    	int i = -1;
    	do
    	{
		string dir;
		int j;
		if( ( j = address.find( '/', i+1 ) ) != -1 ) 
	    		dir = address.substr( i+1, j-i-1 ); 
		else
	    		dir = address.substr( i+1, address.length()-i-1 );
        	//cout << "DIR " << dir << endl;

        	vector<Category *> *subCats = cCategory->subCatv;
		int founddir = FALSE;

        	// skip "."'s

        	if( dir == "." )
            		founddir = TRUE;

        	// try ".."'s

        	else if( dir == ".." )
        	{
            		if( cCategory->parent == 0 )
            		{
				showNavigationError( cCategory, "root category has no parent" );
                		return FALSE;
            		}

            		cCategory = cCategory->parent;
            		founddir = TRUE;
        	}

        	// now try full name or abbreviation

        	Category *match = 0;
		int ambiguity = FALSE;
		vector<string> ambcatv;

        	if( !founddir &&
                    subCats )
        	{
            		for( unsigned int n = 0; n < subCats->size(); n++ )
                		if( equalCI( (*subCats)[n]->name, dir ) )
                		{
                    			match = (*subCats)[n];
					break;
                		}

            		if( !match )
                	for( unsigned int n = 0; n < subCats->size(); n++ )
                    		if( startsWithCI( (*subCats)[n]->name, dir ) )
                    		{
					if( match ) 
						ambiguity = TRUE;
                        		match = (*subCats)[n];
					ambcatv.push_back( match->name );
                    		}

			if( ambiguity )
			{
				showNavigationError( cCategory, "\"" + dir + "\" is ambiguous. it could mean:\n" + vecToDir( ambcatv, "\n" ) );
				match = 0;
			}	
			
			if( match )
                		founddir = TRUE;
        	}

        	if( !founddir ) // if still not found, display an error
        	{
			if( !ambiguity )
	                	showNavigationError( cCategory, "\"" + dir + "\" category cannot be resolved" );
                	return FALSE;
        	}

        	// forwarding

        	if( founddir &&
            	    match &&
            	    !enter( match ) )
            		return FALSE;

    	} while( ( i = address.find( '/', i+1 ) ) != -1 );

    	return TRUE;
}

// text-mode interface main loop

void traverseDII( )
{
    	cout << endl << "KASCADE BROWSER / TEXTMODE BUILD" << endl << endl;

    	cCategory->printCategory();

    	for( ;; )
    	{
        	cout << "choice: ";

		char choice;
		cin >> choice;

		if( choice == 'q' )
			break;

		if( choice == 'v' )
		{
			int i;
			cached( cPath, cFile, i );
            		vector<string> *sourcev = cacheSourcev[i];

            		cout << endl;

            		for( unsigned int n = 0; n < sourcev->size(); n++ )
            		{
                 		printint( n+1, sourcev->size() );
                 		cout << " " << (*sourcev)[n] << endl;
            		}
					
			continue;
		}
		
        	if( ( '0' <= choice ) &&
            	    ( choice <= '9' ) )
        	{
	    		unsigned int i;
            		if( ( cCategory->subCatv != 0 ) &&
                	    ( ( i = ( choice - '0' ) ) < cCategory->subCatv->size() ) )
	    		{
                		if( action_enter( (*(cCategory->subCatv))[i] ) ) 
					cCategory->printCategory();
            		} 
	    		else
				cout << "*** TRY A SMALLER NUMBER" << endl;

			continue;
		}

		string address;

		switch( choice )
		{
			case 's': 	cCategory->printCategory();
        	    			break;

			case 'r': 	action_jump( "dii/root.dii" ); 
					cCategory->printCategory();
					break;

			case 'b': 	if( backTrace() )
	                			cCategory->printCategory();
					break;
		
			case 'c': 	clearCache();
					break;

			case 'f':	if( fwdTrace() )
                				cCategory->printCategory();
					break;

            		case 'h':	cout << endl << "KASCADE BROWSER / TEXTMODE BUILD" << endl << endl
                 			<< "0-9:    choose subcategory" << endl
		 			<< "b:      netscape-like back" << endl
		 			<< "f:      netscape-like forward" << endl 
                 			<< "s:      display subcategories" << endl
					<< "c:      clear cache and refresh" << endl
                 			<< "r:      goto root category" << endl
                 			<< "v:      view category source file" << endl
                 			<< "q:      quit to shell prompt" << endl << endl;
					
					break;
					
			default:	cout << "*** UNKNOWN COMMAND" << endl;
		}
    	} 
}
 
// test to see if the current category is 'empty'
// if it is, but is has info or links, show these

int testEmpty() 
{
	if( !cCategory->subCatv &&
   	    ( cCategory->linkv ) )
	{
		cCategory->visited = TRUE;
		showLinks();
	}	

	else if( !cCategory->subCatv &&
	         ( cCategory->chatv ) )
	{
		cCategory->visited = TRUE;
		showChat();
	}

	else if( !cCategory->subCatv && 
	         ( cCategory->info != "" ) )
	{
//		cCategory->visited = TRUE;
		showInformation();
	}

	else if( cCategory->empty() )
	{
//		cCategory->visited = TRUE;
		showNavigationError( cCategory, "empty category" );
	}	
	else
		return FALSE;

	return TRUE;
}

// the user selects a category.

int action_enter( Category *category )
{
	Currents c; c.save();

	selectionentered = FALSE;
	loopCatv.clear();

	int entered = enter( category );
	showParseWarnings();
	parsewarningv.clear();

	if( entered )
	{
		if( checkSelection() &&
		    !testEmpty() )
		{	
			if( c.changed() )
				addTrace( c );

			cCategory->visited = TRUE;	
			return TRUE;
		}
	}
	
	c.load();
	return FALSE;
}

// the user has indicated that the browser should jump to a certain address.
// examples: 'new search', 'goto bookmark', 'open file', 'open url'

int action_jump( string address )
{
	Currents c; c.save();

	if( clear_goto( address ) )
	{
		if( c.changed() )
			addTrace( c );

		return TRUE;
	}
	return FALSE;
}

int clear_goto( string address )
{
	Currents c; c.save();

	clear_environment();

	int success = gotoAddress( address );
	showParseWarnings();
	parsewarningv.clear();

	if( success && !testEmpty() )
	{
		cCategory->visited = TRUE;
		return TRUE;
	}
	else 
		BACKTRACK	
}

void clear_environment() 
{
	selectionentered = FALSE;
	loopCatv.clear();

	selectionv.clear();
	maskv.clear();
	masksv.clear();
	argv.clear();

	cPath = "";
}

// check if category has warning

void checkWarning( Category *category )
{
	if( ( category->warning != "" ) &&
	    !checkingstructure &&
	    !category->visited )
#ifdef GUI
                new InfoBox( "warning", category->warning, browser );
#else
		cout << category->warning << endl;
#endif
}

void showInformation()
{
#ifdef GUI
	browser->action_showinfo();
#else
	cout << cCategory->info << endl;
#endif
}

void showLinks()
{
#ifdef GUI
	browser->action_showlinks();
#else
	cout << "** has only links" << endl;
#endif
}

void showChat()
{
#ifdef GUI
	browser->action_chat();
#else
	cout << "** has only chat channels" << endl;
#endif
}

string debugInfo()
{
	string info;

	if( selectionv.size() )
		info += "selection: " + selectionv[0] + "\n\n";

	if( masksv.size() )
	{
		info += "masks:\n";

		for( unsigned int n = 0; n < masksv.size(); n++ )
			info += masksv[n] + "\n";

		info += "\n";
	}	

	vector<string> *propv = cCategory->propv;
        if( propv )
	{
		info += "properties:\n";
		
        	for( int n = 0; n < propv->size(); n += 2 )
		{
			info += (*propv)[n];
	    		if( (*propv)[n+1] != "" )
				info += " = " + (*propv)[n+1];
			info += "\n";	
		}

		info += "\n";
	}			

    	if( argv.size() )
	{
		info += "arguments:\n";

    	    	for( unsigned int n = 0; n < argv.size(); n += 2 )
			info += argv[n] + " = " + argv[n+1] + "\n";

		info += "\n";
	}
	
	return info;	
}