Files access in Scol » History » Version 1
  iri, 09/25/2012 12:27 AM 
  
| 1 | 1 | iri | h1. Files access in Scol | 
|---|---|---|---|
| 2 | |||
| 3 | There is two standard APIs to access to a file. | ||
| 4 | Only files in a partition can be opened, read, written or deleted (under condition for this last operation). | ||
| 5 | |||
| 6 | h2. First API (classic) : | ||
| 7 | |||
| 8 | In this api, it needn't to open and close a file; there is no "file pointer". | ||
| 9 | 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. | ||
| 10 | |||
| 11 | |||
| 12 | h3. How to read the content ? | ||
| 13 | |||
| 14 | With the Scol functions *_getpack* and *_checkpack*. | ||
| 15 | |||
| 16 | @_getpack _checkpack <relative_pathname>@ | ||
| 17 | |||
| 18 | <pre> | ||
| 19 | /* | ||
| 20 | 1- we check the file (_checkpack) | ||
| 21 | 2- we get the content (_getpack) | ||
| 22 | 3- we display it, if the check is ok (otherwise, nil will be displayed) (_fooS) | ||
| 23 | */ | ||
| 24 | fun main ()= | ||
| 25 | _showconsole; | ||
| 26 | _fooS _getpack _checkpack "dir_1/dir_2/myfile.ext"; | ||
| 27 | 0;; | ||
| 28 | </pre> | ||
| 29 | |||
| 30 | h3. How to know if a file exists (and is accessible in reading) ? | ||
| 31 | |||
| 32 | With the Scol function *_checkpack* | ||
| 33 | |||
| 34 | @_checkpack <relative_pathname>@ | ||
| 35 | <pre> | ||
| 36 | |||
| 37 | fun main ()= | ||
| 38 | _showconsole; | ||
| 39 | let _checkpack "dir_1/dir_2/myfile.ext" -> result in | ||
| 40 | _fooS if (result == nil) then | ||
| 41 | "This file doesn't exist" | ||
| 42 | else | ||
| 43 | "This file already exists"; | ||
| 44 | 0;; | ||
| 45 | </pre> | ||
| 46 | |||
| 47 | h3. How to find a word in a file : | ||
| 48 | |||
| 49 | <pre> | ||
| 50 | // check if the file exists | ||
| 51 | fun checkFile (pathname)= | ||
| 52 | if (nil == _checkpack pathname) then // file not found or access is forbidden | ||
| 53 | 0 | ||
| 54 | else // file found | ||
| 55 | 1;; | ||
| 56 | |||
| 57 | // return the string content of the file | ||
| 58 | fun getContent (pathreference)= | ||
| 59 | let _getpack pathreference -> content in // content in a single string | ||
| 60 | strextr content;; // content is in a list, line by line and word by word (strextr) | ||
| 61 | |||
| 62 | // search a value from a key | ||
| 63 | fun findValue (listContent, key)= | ||
| 64 | hd switchstr listContent key;; // only the first word is useful in this case (hd) | ||
| 65 | |||
| 66 | fun mainGet ()= | ||
| 67 | let "locked/etc/version.txt" -> pathName in | ||
| 68 | let "version" -> key in | ||
| 69 | if checkFile pathName then | ||
| 70 | let _checkpack pathName -> refPathName in | ||
| 71 | let getContent refPathName -> listWords in | ||
| 72 | let findValue listWords key -> res in | ||
| 73 | if res == nil then | ||
| 74 | _fooS strcat key " not found" | ||
| 75 | else | ||
| 76 | _fooS strcatn key :: " is " :: res :: nil | ||
| 77 | else | ||
| 78 | _fooS "file not found or access is forbidden"; | ||
| 79 | 0;; | ||
| 80 | </pre> | ||
| 81 | |||
| 82 | h3. How to list the content of a directory ? | ||
| 83 | |||
| 84 | With the Scol functions *_listoffiles* and or *_listofsubdir* | ||
| 85 | |||
| 86 | <pre> | ||
| 87 | _listoffiles <relative_pathname> // return a list of files | ||
| 88 | _listofsubdir <relative_pathname> // return a list of sub-directories | ||
| 89 | </pre> | ||
| 90 | |||
| 91 | Here is an example to list all files included in the "locked" directory : | ||
| 92 | |||
| 93 | <pre> | ||
| 94 | fun listConcatenate (listA, listB)= // listA + listB | ||
| 95 | if listA == nil then | ||
| 96 | listB | ||
| 97 | else let listA -> [h nxt] in | ||
| 98 | h :: listConcatenate nxt listB;; | ||
| 99 | |||
| 100 | fun print (list)= // display the list of files, one by line | ||
| 101 | if list == nil then | ||
| 102 | 0 | ||
| 103 | else | ||
| 104 | ( | ||
| 105 | _fooS hd list; | ||
| 106 | print tl list | ||
| 107 | );; | ||
| 108 | |||
| 109 | fun listFilesInDirectory (dir)= // return the list of files from a given directory | ||
| 110 | _listoffiles dir;; | ||
| 111 | |||
| 112 | fun listDirInDirectory (dir)= // return the list of sub directories from a directory | ||
| 113 | _listofsubdir dir;; | ||
| 114 | |||
| 115 | fun listDirectories (listDir, out)= | ||
| 116 | if listDir == nil then | ||
| 117 | out | ||
| 118 | else | ||
| 119 | ( | ||
| 120 | set out = listConcatenate out listFilesInDirectory hd listDir; | ||
| 121 | set out = listConcatenate out listDirectories listDirInDirectory hd listDir out; | ||
| 122 | listDirectories tl listDir out | ||
| 123 | );; | ||
| 124 | |||
| 125 | fun mainDir ()= | ||
| 126 | let listFilesInDirectory "locked" -> rootFiles in | ||
| 127 | let listDirInDirectory "locked" -> rootSubFolder in | ||
| 128 | let listDirectories rootSubFolder rootFiles -> list in | ||
| 129 | print list; | ||
| 130 | 0;; | ||
| 131 | </pre> | ||
| 132 | |||
| 133 | h3. How to write in a file ? | ||
| 134 | |||
| 135 | With the Scol functions *_getmodifypack* and *_createpack*, *_appendpack* or *_storepack*. | ||
| 136 | |||
| 137 | * @_createpack <string_content> _getmodifypack <relative_pathname>@ : create a file (if the file already exist, it will be destroyed) and write the string content. | ||
| 138 | Don't set the content to nil. To have an empty content, set to "". | ||
| 139 | |||
| 140 | * @_appendpack <string_content> _getmodifypack <relative_pathname>@ : append the string content to the end of file. | ||
| 141 | |||
| 142 | * @_storepack <string_content> <relative_pathname>@ : the file will be sored in the first writable partition. | ||
| 143 | |||
| 144 | * @_getmodifypack <relative_pathname>@ : check if the given path is correct (if correct, this function returns not nil). | ||
| 145 | |||
| 146 | h3. How to get the size of a file ? | ||
| 147 | |||
| 148 | With the Scol functions *_fileSize* and *_checkpack*. | ||
| 149 | |||
| 150 | @_fileSize _checkpack <relative_pathname>@ | ||
| 151 | |||
| 152 | h3. How to separate the path name from the file name ? | ||
| 153 | |||
| 154 | <pre> | ||
| 155 | fun getPathFile (longfile, file)= | ||
| 156 | if (longfile==nil) || (strlen longfile)==0 || (nth_char longfile ((strlen longfile)-1)) == '/ | ||
| 157 | then | ||
| 158 | [longfile file] | ||
| 159 | else | ||
| 160 | getPathFile | ||
| 161 | substr longfile 0 (strlen longfile)-1 | ||
| 162 | strcat | ||
| 163 | substr longfile ((strlen longfile)-1) 1 | ||
| 164 | file;; | ||
| 165 | </pre> | ||
| 166 | |||
| 167 | This function has been wrote by Marc Barilley. | ||
| 168 | |||
| 169 | h2. Second API (Ansi-C) : | ||
| 170 | |||
| 171 | This is the same thing as in C. | ||
| 172 | |||
| 173 | *_FILEOpen* : open a file. Return a Scol reference | ||
| 174 | *_FILEClose* : close a file from its Scol reference | ||
| 175 | *_FILERead* : read the content from the cursor to a given length | ||
| 176 | *_FILESeek* : move the cursor | ||
| 177 | *_FILETell* : get the cursor position | ||
| 178 | *_FILESize* : get the size | ||
| 179 | |||
| 180 | <pre> | ||
| 181 | fun main ()= | ||
| 182 | _showconsole; | ||
| 183 | |||
| 184 | let _FILEOpen _channel _checkpack "dir_1/dir_2/myfile.ext" -> pf in | ||
| 185 | let _FILESize pf -> size in | ||
| 186 | ( | ||
| 187 | _FILESeek pf size/2 0; | ||
| 188 | _fooS _FILERead pf 255; | ||
| 189 | _FILEClose pf | ||
| 190 | ); | ||
| 191 | 0;; | ||
| 192 | </pre> | ||
| 193 | |||
| 194 | h2. Temporary files | ||
| 195 | |||
| 196 | The library Syspack introduces the temporary files support : http://www.scolring.org/files/doc_html/syspack_temfile.html | ||
| 197 | |||
| 198 | License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/ | ||
| 199 | Tutorial by iri | ||
| 200 | Updated by / |