File: modloader.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (988 lines) | stat: -rw-r--r-- 23,965 bytes parent folder | download | duplicates (2)
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/*
   FALCON - The Falcon Programming Language.
   FILE: flc_modloader.cpp

   Module loader
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: 2004-08-20

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/setup.h>
#include <falcon/modloader.h>
#include <falcon/error.h>
#include <falcon/fstream.h>
#include <falcon/module.h>
#include <falcon/dll.h>
#include <falcon/fstream.h>
#include <falcon/sys.h>
#include <falcon/pcodes.h>
#include <falcon/timestamp.h>
#include <falcon/transcoding.h>
#include <falcon/stringstream.h>
#include <falcon/gencode.h>
#include <falcon/uri.h>
#include <falcon/vfsprovider.h>
#include <falcon/streambuffer.h>
#include <falcon/stdstreams.h>

#include <falcon/globals.h>
#include <falcon/modulecache.h>

#include <memory>



#define BINMODULE_EXT "_fm"

namespace Falcon
{

ModuleLoader::ModuleLoader():
   m_alwaysRecomp( false ),
   m_compMemory( true ),
   m_saveModule( true ),
   m_saveMandatory( false ),
   m_detectTemplate( true ),
   m_forceTemplate( false ),
   m_delayRaise( false ),
   m_ignoreSources( false ),
   m_saveRemote( false ),
   m_compileErrors(0)
{
   m_path.deletor( string_deletor );
   setSearchPath( "." );
}


ModuleLoader::ModuleLoader( const String &path ):
   m_alwaysRecomp( false ),
   m_compMemory( true ),
   m_saveModule( true ),
   m_saveMandatory( false ),
   m_detectTemplate( true ),
   m_forceTemplate( false ),
   m_delayRaise( false ),
   m_ignoreSources( false ),
   m_saveRemote( false ),
   m_compileErrors(0)
{
   m_path.deletor( string_deletor );
   setSearchPath( path );
}

ModuleLoader::ModuleLoader( const ModuleLoader &other ):
   m_alwaysRecomp( other.m_alwaysRecomp ),
   m_compMemory( other.m_compMemory ),
   m_saveModule( other.m_saveModule ),
   m_saveMandatory( other.m_saveMandatory ),
   m_detectTemplate( other.m_detectTemplate ),
   m_forceTemplate( other.m_forceTemplate ),
   m_delayRaise( other.m_delayRaise ),
   m_ignoreSources( other.m_ignoreSources ),
   m_saveRemote( other.m_saveRemote ),
   m_compileErrors( other.m_compileErrors )
{
   setSearchPath( other.getSearchPath() );
}


ModuleLoader::~ModuleLoader()
{
}


ModuleLoader *ModuleLoader::clone() const
{
   return new ModuleLoader( *this );
}


void ModuleLoader::getModuleName( const String &path, String &modName )
{
   // .../modname.xxx
   // we need at least a char.
   if ( path.length() == 0 )
   {
      modName = "";
      return;
   }

   int32 dotpos = path.rfind( "." );
   int32 slashpos = path.rfind( "/" );
   if ( dotpos == -1  || dotpos < slashpos )
      dotpos = path.length();
   // legal also if slashpos < 0

   modName = path.subString( slashpos + 1, dotpos );
}


void ModuleLoader::addFalconPath()
{
   String envpath;
   bool hasEnvPath = Sys::_getEnv( "FALCON_LOAD_PATH", envpath );

   if ( hasEnvPath )
   {
      addSearchPath( envpath );
   }
   else {
      addSearchPath( FALCON_DEFAULT_LOAD_PATH );
   }
}

void ModuleLoader::setSearchPath( const String &path )
{
   m_path.clear();
   addSearchPath( path );
}

void ModuleLoader::addSearchPath( const String &path )
{
   // subdivide the path by ';'
   int32 pos = 0, pos1 = 0;

   // nothing to add ?
   if ( path == "" )
      return;;

   while( true )
   {
      String *tmp;

      pos1 = path.find( ";", pos );
      if ( pos1 ==  -1 )
      {
         tmp = new String( path, pos );
         tmp->bufferize();
         m_path.pushBack( tmp );
         break;
      }

      if ( pos1 == -1 ) {
         tmp = new String( path, pos );
         tmp->bufferize();
         m_path.pushBack( tmp );
         break;
      }

      if ( pos1 > pos ) {
         tmp = new String( path, pos, pos1 );
         tmp->bufferize();
         m_path.pushBack( tmp );
      }
      pos = pos1+1;
   }
}

void ModuleLoader::getSearchPath( String &tgt ) const
{
   tgt.size(0);

   ListElement *path_elem = m_path.begin();
   while ( path_elem != 0 )
   {
      String *pathp = (String *) path_elem->data();
      tgt += *pathp;
      path_elem = path_elem->next();
      if ( path_elem != 0 )
         tgt += ";";
   }
}


Stream *ModuleLoader::openResource( const String &path, t_filetype type )
{
   // Get the uri
   URI furi( path );

   if ( !furi.isValid() )
   {
      raiseError( e_malformed_uri, path ); // upstream will fill the module
   }

   // find the appropriage provider.
   VFSProvider* vfs = Engine::getVFS( furi.scheme() );
   if ( vfs == 0 )
   {
      raiseError( e_unknown_vfs, path );  // upstream will fill the module
   }

   Stream *in = vfs->open( furi, VFSProvider::OParams().rdOnly() );

   if ( in == 0 )
   {
      throw vfs->getLastError();
   }

   if ( type == t_source || type == t_ftd )
   {
      if ( m_srcEncoding != "" )
      {
         // set input encoding
         Stream *inputStream = TranscoderFactory( m_srcEncoding, in, true );
         if( inputStream != 0 )
            return AddSystemEOL( inputStream );

         delete in;
         raiseError( e_unknown_encoding, m_srcEncoding ); // upstream will fill the module
      }
      else
         return AddSystemEOL( in );
   }

   return in;
}


ModuleLoader::t_filetype ModuleLoader::fileType( const String &fext )
{
   String ext = fext;
   ext.lower();

   if ( ext == "fal" )
   {
      return t_source;
   }
   else if ( ext == "ftd" )
   {
      return t_ftd;
   }
   else if ( ext == DllLoader::dllExt() )
   {
      return t_binmod;
   }
   else if ( ext == "fam" )
   {
      return t_vmmod;
   }

   return t_none;
}


Module *ModuleLoader::loadName( const String &module_name, const String &parent_name )
{
   String nmodName;

   // prevent doing a crashy thing.
   if ( module_name.length() == 0 )
      throw new CodeError( ErrorParam( e_modname_inv ).extra( module_name ).origin( e_orig_loader ).
            module( "(loader)" ) );

   Module::absoluteName( module_name, parent_name, nmodName );

   String expName = nmodName;

   // expand "." names into "/"
   uint32 pos = expName.find( "." );
   while( pos != String::npos )
   {
      expName.setCharAt( pos, '/' );
      pos = expName.find( ".", pos + 1 );
   }


   Module *mod = loadFile( expName, t_none, true );
   mod->name( nmodName );

   return mod;
}


bool ModuleLoader::scanForFile( URI &origUri, VFSProvider* vfs, t_filetype &type, FileStat &fs )
{
   // loop over the possible extensions and pick the newest.
   const char *exts[] = { "ftd", "fal", "fam", DllLoader::dllExt(), 0 };
   const t_filetype ftypes[] = { t_ftd, t_source, t_vmmod, t_binmod, t_none };

   TimeStamp tsNewest;
   const char **ext = exts;
   const t_filetype *ptf = ftypes;

   // skip source exensions if so required.
   if ( ignoreSources() )
   {
      ext++; ext++;
      ptf++; ptf++;
   }

   while( *ext != 0 )
   {
      origUri.pathElement().setExtension( *ext );

      // did we find it?
      if ( *ptf == t_binmod )
      {
         // for binary module, add the extra module identifier.
         URI copy(origUri);
         copy.pathElement().setFile( copy.pathElement().getFile() + BINMODULE_EXT );
         if( vfs->readStats( copy, fs ) )
         {
            origUri.pathElement().setFile( copy.pathElement().getFile() );
            type = t_binmod;
            return true;
         }
      }
      else if ( vfs->readStats( origUri, fs ) )
      {
         type = *ptf;
         return true;
      }

      // get next extension and file type
      ext++;
      ptf++;
   }

   return false;
}

Module *ModuleLoader::loadFile( const URI& uri, t_filetype type, bool scan )
{
   URI origUri = uri;
   String file_path;
   t_filetype tf = t_none;

   VFSProvider *vfs = Engine::getVFS( origUri.scheme() );
   if ( vfs == 0 )
   {
      throw new CodeError( ErrorParam( e_unknown_vfs )
         .extra( uri.scheme() )
            .origin( e_orig_loader )
            );
   }

   // Check wether we have absolute files or files to be searched.

   FileStat foundStats;
   bool bFound = false;

   // If we don't have have an absolute path,
   if ( ! origUri.pathElement().isAbsolute() )
   {
      // ... and if scan is false, we must add our relative path to absolutize it.
      if ( ! scan )
      {
         String curdir;
         int32 error;
         if ( ! Sys::fal_getcwd( curdir, error ) )
         {
            throw new IoError( ErrorParam( e_io_error )
               .extra( Engine::getMessage( msg_io_curdir ) )
               .origin( e_orig_loader )
               .sysError( error )
               );
         }

         origUri.pathElement().setFullLocation( curdir + "/" + origUri.pathElement().getFullLocation() );
      }
   }
   else
   {
      // absolute path force scan to be off, just to be sure.
      scan = false;
   }

   // we are interested in knowing if a default extension was given,
   // in that case we won't go searching for that.
   bool bHadExtension = origUri.pathElement().getExtension() != "";

   // if we don't have to scan in the path...
   if ( ! scan )
   {
      // ... if type is t_none, we may anyhow scan for a proper extension.
      if( (type == t_none || type == t_defaultSource) && ! bHadExtension )
      {
         URI saveUri = origUri;
         bFound = scanForFile( origUri, vfs, tf, foundStats );
         if ( ! bFound && type == t_defaultSource )
         {
            origUri = saveUri;

            // scanforfile may
            bFound = vfs->readStats( origUri, foundStats );
            if ( bFound )
               tf = t_source;
         }
      }
      else {
         // just check if the file exists.
         tf = type == t_none ? fileType(origUri.pathElement().getExtension()) : type;
         bFound = vfs->readStats( origUri, foundStats );
      }
   }
   // shall we scan for a file?
   else
   {
      // ready to scan the list of directories;
      ListElement *path_elem = m_path.begin();
      String oldPath = origUri.pathElement().getLocation();
      while ( (! bFound) && path_elem != 0 )
      {
         String *pathp = (String *) path_elem->data();
         origUri.pathElement().setFullLocation( *pathp );
         origUri.pathElement().extendLocation( oldPath  );

         // If we originally had an extension, we must not add it.
         if ( bHadExtension )
         {
            // if the thing exists...
            if( ( bFound = vfs->readStats( origUri, foundStats ) ) )
            {
               // ... set the file type, either on our default or on the found extension.
               tf = (type == t_none || type == t_defaultSource ) ?
                     fileType( origUri.pathElement().getExtension() ) : type;
            }
         }
         else
         {
            // we must can the possible extensions in this directory
            bFound = scanForFile( origUri, vfs, tf, foundStats );
         }

         path_elem = path_elem->next();
      }
   }


   Module *mod = 0;
   // did we found a file?
   if( bFound )
   {
      // Ok, TF should be the file type.
      switch( tf )
      {
      case t_source: case t_ftd: case t_defaultSource:
         {
            FileStat fs;
            // should we load a .fam instead?
            URI famUri = origUri;
            famUri.pathElement().setExtension( "fam" );
            if ( ! alwaysRecomp()
                 && ( ignoreSources()
                      || (vfs->readStats( famUri, fs ) && *fs.m_mtime >= *foundStats.m_mtime) )
               )
            {
               try {
                  mod = loadModule( famUri.get() );
               }
               catch( Error *e )
               {
                  // well, our try wasn't cool. try with the source.
                  e->decref();
                  mod = loadSource( origUri.get() );
               }
            }
            else
            {
               mod = loadSource( origUri.get() );
            }
         }
         break;

      case t_vmmod: mod = loadModule( origUri.get() ); break;
      case t_binmod: mod = loadBinaryModule( URI::URLDecode( origUri.get() ) ); break;

      default:
         // we have not been able to find it
         // -- report the actual file that caused the problem.
         raiseError( e_unrec_file_type, origUri.get() );
      }
   }
   else
   {
   	String expl = URI::URLDecode(uri.get());
   	if( scan )
   		expl += String(" in path ") + getSearchPath();
      raiseError( e_nofile, expl );
   }

   // in case of errors, we already raised
   String modName;
   getModuleName( origUri.get(), modName );
   mod->name( modName );
   mod->path( origUri.get() );

   // try to load the language table.
   if ( m_language != "" && mod->language() != m_language )
   {
      // the function may fail, but it won't raise.
      loadLanguageTable( mod, m_language );
   }

   // in case of errors, we already raised
   return mod;
}


Module *ModuleLoader::loadFile( const String &module_path, t_filetype type, bool scan )
{
   // Preliminary filtering -- get the URI and the filesystem.
   URI origUri;
   origUri.parse( module_path, true, false );
  
   if ( ! origUri.isValid() )
   {
      throw new CodeError( ErrorParam( e_malformed_uri )
            .extra( module_path )
            .origin( e_orig_loader )
            );
   }
   
   return loadFile( origUri, type, scan );
}



bool ModuleLoader::loadLanguageTable( Module *module, const String &language )
{
   String langFileName;

   // try to find the .ftr file
   uint32 posDot = module->path().rfind( "." );
   uint32 posSlash = module->path().rfind( "/" );
   if ( posDot == String::npos || ( posSlash != String::npos && posDot < posSlash ) )
   {
      langFileName = module->path() + ".ftr";
   }
   else {
      langFileName = module->path().subString(0, posDot );
      langFileName += ".ftr";
   }

   if( applyLangTable( module, langFileName ) )
   {
      module->language( language );
      return true;
   }

   return false;
}

inline int32 xendianity( bool sameEndianity, int32 val )
{
   return sameEndianity ? val :
      (val >> 24) |
      ((val & 0xFF0000) >> 8) |
      ((val & 0xFF00 ) << 8) |
      (val << 24);
}

//TODO: add some diags.
bool ModuleLoader::applyLangTable( Module *mod, const String &file_path )
{
   URI fsuri( file_path );
   fassert( fsuri.isValid() );
   VFSProvider* vfs = Engine::getVFS( fsuri.scheme() );
   fassert( vfs != 0 );

   // try to open the required file table.
   Stream *fsin_p = vfs->open( fsuri, VFSProvider::OParams().rdOnly() );
   if( fsin_p == 0 )
      return false;
      
   std::auto_ptr<Stream> fsin( fsin_p );

   // check if this is a regular tab file.
   char buf[16];
   buf[5] = 0;
   if ( fsin->read( buf, 5 ) != 5 || String( "TLTAB" ) != buf )
   {
      return false;
   }

   uint16 endianity;
   if( fsin->read( &endianity, 2 ) != 2 )
   {
      return false;
   }

   bool sameEndianity = endianity == 0xFBFC;

   // read the language table index.
   int32 sizeField;
   if( fsin->read( &sizeField, 4 ) != 4 )
      return false;

   int32 tableSize = xendianity( sameEndianity, sizeField );
   int32 tablePos = -1;
   for( int32 i = 0; i < tableSize; i++ )
   {
      // read language code and position in file
      if( fsin->read( buf, 5 ) != 5 ||
          fsin->read( &sizeField, 4 ) != 4 )
         return false;

      // is this our language code?
      if( m_language == buf )
      {
         tablePos = xendianity( sameEndianity, sizeField );
         break;
      }
   }

   // entry not found?
   if( tablePos < 0 )
      return false;

   uint32 headerSise = 5 + 2 + 4 + (tableSize * 9);
   uint32 filePos = headerSise + tablePos;
   fsin->seekBegin( filePos );

   // read the number of strings to be decoded.
   if( fsin->read( &sizeField, 4 ) != 4 )
      return false;

   int32 stringCount = xendianity( sameEndianity, sizeField );

   // read table and alter module.
   int32 allocated = 256;
   char *memBuf = (char *) memAlloc( allocated );

   // the most intelligent thing is that to modify the strings as they are in memory.
   // In this way, we don't have to alter already allocated string structures, and
   // we don't have to scan the map for the correct string entry.

   while ( stringCount > 0 )
   {
      // read ID
      if( fsin->read( &sizeField, 4 ) != 4 )
         break;
      int32 stringID = xendianity( sameEndianity, sizeField );
      if ( stringID < 0 || stringID >= mod->stringTable().size() )
         break;

      // read the string size
      if( fsin->read( &sizeField, 4 ) != 4 )
         break;
      int32 stringSize = xendianity( sameEndianity, sizeField );
      if ( stringSize < 0 )
         break;
      if ( stringSize == 0 )
         continue;

      // if the string size exeeds the allocated amount, fix it.
      if( stringSize >= allocated )
      {
         memFree( memBuf );
         allocated = stringSize + 1;
         memBuf = (char *) memAlloc( allocated );
      }

      // read the string
      if( fsin->read( memBuf, stringSize ) != stringSize )
         break;

      // zero the end so we have an utf8 string
      memBuf[ stringSize ] = 0;

      // finally, place it in the right place
      if ( ! mod->stringTable().getNonConst( stringID )->fromUTF8( memBuf ) )
         break;

      stringCount --;
   }

   memFree( memBuf );
   return stringCount == 0;
}

Module *ModuleLoader::loadModule( const String &path )
{
   ModuleCache* mc = Engine::getModuleCache();
   if( mc != 0 )
   {
      Module* mod = mc->find( path );
      if( mod != 0 )
         return mod;
   }

   // May throw on error.
   Module *mod = 0;

   {
      // loadModule may throw, so we need an autoptr not to leak in case of errors.
      std::auto_ptr<Stream> in( openResource( path, t_vmmod ));
      mod = loadModule( in.get() );

      if( mc != 0 ) mod = mc->add( path, mod );
   }
   fassert( mod != 0 );

   String modName;
   getModuleName( path, modName );
   mod->name( modName );
   mod->path( path );

   if ( m_language != "" && mod->language() != m_language )
   {
      // This should not throw.
      loadLanguageTable( mod, m_language );
   }

   return mod;
}


Module *ModuleLoader::loadBinaryModule( const String &path )
{
   ModuleCache* mc = Engine::getModuleCache();
   if( mc != 0 )
   {
      Module* mod = mc->find( path );
      if( mod != 0 )
         return mod;
   }

   DllLoader dll;

   if ( ! dll.open( path ) )
   {
      String error;
      dll.getErrorDescription( error );
      error.prepend( path + ": " );
      raiseError( e_binload, error );
      return 0;
   }

   DllFunc dlfunc = dll.getSymbol( "falcon_module_init" );
   ext_mod_init func = (ext_mod_init) dlfunc.data();

   if ( func == 0 )
   {
      raiseError( e_binstartup, path );
      return 0;
   }

   Module *mod = func();

   if ( mod == 0 )
   {
      raiseError( e_bininit, path);
      return 0;
   }

   // Now I can pass the DLL instance to the module.
   mod->dllLoader().assign( dll );

   // and give the module its names.
   String modName;
   getModuleName( path, modName );
   mod->name( modName );
   mod->path( path );

   if ( mc ) mod = mc->add( path, mod );

   // as dll instance has been emptied, the DLL won't be closed till the
   // module lifetime comes to an end.
   return mod;
}


Module *ModuleLoader::loadModule( Stream *in )
{
   // try to open the file
   char c1, c2;

   in->read( &c1, 1 );
   in->read( &c2, 1 );

   if(c1 =='F' && c2 =='M')
   {
      Module *ret = loadModule_select_ver( in );
      return ret;
   }

   return 0;
}


Module *ModuleLoader::loadModule_select_ver( Stream *in )
{
   char c1, c2;

   in->read( &c1, 1 );
   in->read( &c2, 1 );

   // C1 and c2 now contain the version.
   // for now we can load only format PCODE

   if( c1 == FALCON_PCODE_VERSION && c2 == FALCON_PCODE_MINOR ) {
      return  loadModule_ver_1_0( in );
   }

   raiseError( e_modver, "" );
   return 0;
}

Module *ModuleLoader::loadModule_ver_1_0( Stream *in )
{
   Module *mod = new Module();
   if ( ! mod->load( in, true ) )
   {
      raiseError( e_modformat, "" );
   }
   return mod;
}


void  ModuleLoader::raiseError( int code, const String &expl, int fsError )
{
   throw new IoError( ErrorParam( code )
            .extra( expl )
            .origin( e_orig_loader )
            .sysError( fsError )
         );
}

Module *ModuleLoader::loadSource( const String &file )
{
   ModuleCache* mc = Engine::getModuleCache();
   if( mc != 0 )
   {
      Module* mod = mc->find( file );
      if( mod != 0 )
         return mod;
   }

   // we need it later
   int32 dotpos = file.rfind( "." );

   // Ok, if we're here we have to load the source.
   // should we force loading through Falcon Template Document parsing?
   bool bOldForceFtd = m_forceTemplate;
   if ( m_detectTemplate && ! m_forceTemplate )
   {
      if ( file.subString( dotpos ) == ".ftd" )
         m_forceTemplate = true;
   }

   // use the base load source routine
   // ask for detection, but default to falcon source
   // will throw on error
   std::auto_ptr<Stream> in( openResource( file, t_source ) );

   String modName;
   getModuleName( file, modName );

   Module *mod = 0;
   try {
      mod = loadSource( in.get(), file, modName );
      m_forceTemplate = bOldForceFtd;
      in->close();

      if( mc != 0 )
      {
         mod = mc->add( file, mod );
      }
   }
   catch (Error *)
   {
      // reset old forcing method
      m_forceTemplate = bOldForceFtd;
      throw;
   }

   return mod;
}


Module *ModuleLoader::loadSource( Stream *fin, const String &path, const String &name )
{
   Module *module;
   m_compileErrors = 0;

   // the temporary binary file for the pre-generated module
   Stream *temp_binary;

   module = new Module();
   module->name( name );
   module->path( path );

   m_compiler.reset();
   m_compiler.searchPath( getSearchPath() );

   if ( m_forceTemplate )
      m_compiler.parsingFtd( true );

   // the compiler can never throw
   if( ! m_compiler.compile( module, fin ) )
   {
      module->decref();
      throw m_compiler.detachErrors();
   }

   // we have compiled it. Now we need a file or a memory stream for
   // saving data.
   if ( m_compMemory )
   {
      temp_binary = new StringStream;
   }
   else {
      String tempFileName;
      Sys::_tempName( tempFileName );
      FileStream *tb= new FileStream;
      tb->create( tempFileName + "_1", Falcon::FileStream::e_aReadOnly | Falcon::FileStream::e_aUserWrite );
      if ( ! tb->good() )
      {
         int fserr = (int)tb->lastError();
         delete tb;
         module->decref();
         raiseError( e_file_output, tempFileName, fserr );
      }
      temp_binary = new StreamBuffer( tb );
   }

   GenCode codeOut( module );
   codeOut.generate( m_compiler.sourceTree() );

   // import the binary stream in the module;
   delete temp_binary;

   module->name( name );
   module->path( path );

   // if the base load source worked, save the result (if configured to do so).
   if ( m_saveModule )
   {
      URI tguri( path );
      fassert( tguri.isValid() );
      VFSProvider* vfs = Engine::getVFS( tguri.scheme() );
      fassert( vfs != 0 );

      // don't save on remote systems if saveRemote is false
      if ( vfs->protocol() == "file" || m_saveRemote )
      {
         tguri.pathElement().setExtension( "fam" );
         // Standard creations params are ok.
         Stream *temp_binary = vfs->create( tguri, VFSProvider::CParams() );
         if ( temp_binary == 0 || ! module->save( temp_binary ) )
         {
            if ( m_saveMandatory )
            {
               int fserr = (int) temp_binary->lastError();
               delete temp_binary;
               module->decref();
               raiseError( e_file_output, tguri.get(), fserr );
            }
         }

         delete temp_binary;
      }
   }

   return module;
}



}

/* end of flc_modloader.cpp */