Project

General

Profile

Actions

Files access in Scol

There is two standard APIs to access to a file.
Only files in a partition can be opened, read, written or deleted (under condition for this last operation).

First API (classic) :

In this api, it needn't to open and close a file; there is no "file pointer".
The file can be loaded in the memory ; don't forget this if you want to work with a big file. If the file can be very big, you should use the second api.

How to read the content ?

With the Scol functions _getpack and _checkpack.

_getpack _checkpack <relative_pathname>

/*
    1- we check the file (_checkpack)
    2- we get the content (_getpack)
    3- we display it, if the check is ok (otherwise, nil will be displayed) (_fooS)
*/
fun main ()=
    _showconsole;
    _fooS _getpack _checkpack "dir_1/dir_2/myfile.ext";
    0;;

How to know if a file exists (and is accessible in reading) ?

With the Scol function _checkpack

_checkpack <relative_pathname>


fun main ()=
    _showconsole;
    let _checkpack "dir_1/dir_2/myfile.ext" -> result in
    _fooS if (result == nil) then
                "This file doesn't exist" 
            else
                "This file already exists";
    0;;

How to find a word in a file :

// check if the file exists
fun checkFile (pathname)=
    if (nil == _checkpack pathname) then    // file not found or access is forbidden
        0
    else    // file found
        1;;

// return the string content of the file        
fun getContent (pathreference)=
    let _getpack pathreference -> content in    // content in a single string
    strextr content;;    // content is in a list, line by line and word by word (strextr)

// search a value from a key    
fun findValue (listContent, key)=
    hd switchstr listContent key;;    // only the first word is useful in this case (hd)

fun mainGet ()=
    let "locked/etc/version.txt" -> pathName in
    let "version" -> key in
    if checkFile pathName then
        let _checkpack pathName -> refPathName in
        let getContent refPathName -> listWords in
        let findValue listWords key -> res in
        if res == nil then
            _fooS strcat key " not found" 
        else
            _fooS strcatn key :: " is " :: res :: nil
    else
        _fooS "file not found or access is forbidden";
    0;;

How to list the content of a directory ?

With the Scol functions _listoffiles and or _listofsubdir

_listoffiles <relative_pathname>    // return a list of files
_listofsubdir <relative_pathname>    // return a list of sub-directories

Here is an example to list all files included in the "locked" directory :

fun listConcatenate (listA, listB)=    // listA + listB
    if listA == nil then
        listB
    else let listA -> [h nxt] in
        h :: listConcatenate nxt listB;;

fun print (list)=    // display the list of files, one by line
    if list == nil then
        0
    else
    (
        _fooS hd list;
        print tl list
    );;

fun listFilesInDirectory (dir)=    // return the list of files from a given directory
    _listoffiles dir;;

fun listDirInDirectory (dir)=    // return the list of sub directories from a directory
    _listofsubdir dir;;

fun listDirectories (listDir, out)=
    if listDir == nil then
        out
    else
    (
        set out = listConcatenate out listFilesInDirectory hd listDir;
        set out = listConcatenate out listDirectories listDirInDirectory hd listDir out;
        listDirectories tl listDir out
    );;

fun mainDir ()=
    let listFilesInDirectory "locked" -> rootFiles in
    let listDirInDirectory "locked" -> rootSubFolder in
    let listDirectories rootSubFolder rootFiles -> list in
    print list;
    0;;

How to write in a file ?

With the Scol functions _getmodifypack and _createpack, _appendpack or _storepack.

  • _createpack <string_content> _getmodifypack <relative_pathname> : create a file (if the file already exist, it will be destroyed) and write the string content.
    Don't set the content to nil. To have an empty content, set to "".
  • _appendpack <string_content> _getmodifypack <relative_pathname> : append the string content to the end of file.
  • _storepack <string_content> <relative_pathname> : the file will be sored in the first writable partition.
  • _getmodifypack <relative_pathname> : check if the given path is correct (if correct, this function returns not nil).

How to get the size of a file ?

With the Scol functions _fileSize and _checkpack.

_fileSize _checkpack <relative_pathname>

How to separate the path name from the file name ?

fun getPathFile (longfile, file)=
  if (longfile==nil) || (strlen longfile)==0 || (nth_char longfile ((strlen longfile)-1)) == '/
  then
    [longfile file]
  else
    getPathFile
      substr longfile 0 (strlen longfile)-1
      strcat
        substr longfile ((strlen longfile)-1) 1
        file;;

This function has been wrote by Marc Barilley.

Second API (Ansi-C) :

This is the same thing as in C.

_FILEOpen : open a file. Return a Scol reference
_FILEClose : close a file from its Scol reference
_FILERead : read the content from the cursor to a given length
_FILESeek : move the cursor
_FILETell : get the cursor position
_FILESize : get the size

fun main ()=
    _showconsole;

    let _FILEOpen _channel _checkpack "dir_1/dir_2/myfile.ext" -> pf in
    let _FILESize pf -> size in
    (
    _FILESeek pf size/2 0;
    _fooS _FILERead pf 255;
    _FILEClose pf
    );
    0;;

Temporary files

The library Syspack introduces the temporary files support : http://www.scolring.org/files/doc_html/syspack_temfile.html

License : CC-BY-SA-2.0
Tutorial by iri
Updated by /

Updated by iri over 11 years ago · 1 revisions