Project

General

Profile

File api SCOL » History » Revision 2

Revision 1 (iri, 12/17/2011 09:13 PM) → Revision 2/3 (iri, 12/17/2011 09:14 PM)

h1. File api SCOL 

 "http://www.scolring.org/files/doc_html/file_system.html":http://www.scolring.org/files/doc_html/file_system.html 

 +Files must be relative at the active Scol partition+. 
 There is not the close file function, Scol manage any I/O. 

 *To open a file :* 

 Read only : 
 _checkpack = fun [S] P 
 Write only : 
 _getmodifypack = fun [S] W 

 *To read a file :* 

 _getpack = fun [P] S 

 <pre> 
 fun main ()= 
   _showconsole; 
   let _checkpack "myFolder/myFile.ext" -> pFile in 
   if pFile == nil then 
     _fooS "This file doesn't exist" 
   else 
     _fooS _getpack pFile; 
   0;; 
 </pre> 

 To get the content line by line : 

 <pre> 
 fun displayLines (list)= 
   if list == empty then 
     0 
   else 
     let hd list -> line in 
     ( 
     _fooS line; 
     displayLines tl list 
     );; 

 fun main ()= 
   _showconsole; 
   let _checkpack "myFolder/myFile.ext" -> pFile in 
   if pFile == nil then 
   ( 
     _fooS "This file doesn't exist"; 
     1 
   ) 
   else 
     displayLines lineextr _getpack pFile;; 
 </pre> 

 If you want the content word by word, you can do something similar with _strextr_ instead of _lineextr_. 

 To get the size : 

 _fileSize = fun [P] I 

 <pre> 
 fun main ()= 
   _showconsole; 
   let _checkpack "myFolder/myFile.ext" -> pFile in 
   _fooId 
      if pFile == nil then 
        nil 
      else 
        _fileSize pFile; 
   0;; 
 </pre> 

 *To write in a file :* 

 _createpack = fun [S W] I 
 _appendpack = fun [S W] I 
 _storepack = fun [S S] I 

 <pre> 
 fun main ()= 
   _showconsole; 
   let _getmodifypack "anyFolder/anyFile.ext" -> wFile in 
   if 0 == _createpack "Bob and Alice " wFile then 
     if 0 == _appendpack "are married !" wFile then 
       _fooS "Done !" 
     else 
       _fooS "_appendpack : error" 
   else 
     _fooS "_createpack : error"; 
   0;; 
 </pre> 

 Author : iri 
 Date     : december 2011 

 *Return to [[Examples]]*