1 /********************************************************************/
2 /*                                                                  */
3 /*  s7   Seed7 interpreter                                          */
4 /*  Copyright (C) 1990 - 2005  Thomas Mertes                        */
5 /*                                                                  */
6 /*  This program is free software; you can redistribute it and/or   */
7 /*  modify it under the terms of the GNU General Public License as  */
8 /*  published by the Free Software Foundation; either version 2 of  */
9 /*  the License, or (at your option) any later version.             */
10 /*                                                                  */
11 /*  This program is distributed in the hope that it will be useful, */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of  */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   */
14 /*  GNU General Public License for more details.                    */
15 /*                                                                  */
16 /*  You should have received a copy of the GNU General Public       */
17 /*  License along with this program; if not, write to the           */
18 /*  Free Software Foundation, Inc., 51 Franklin Street,             */
19 /*  Fifth Floor, Boston, MA  02110-1301, USA.                       */
20 /*                                                                  */
21 /*  Module: Library                                                 */
22 /*  File: seed7/src/ut8lib.c                                        */
23 /*  Changes: 2005  Thomas Mertes                                    */
24 /*  Content: All Primitive actions for the UTF-8 file type.         */
25 /*                                                                  */
26 /********************************************************************/
27 
28 #include "version.h"
29 
30 #include "stdlib.h"
31 #include "stdio.h"
32 
33 #include "common.h"
34 #include "data.h"
35 #include "syvarutl.h"
36 #include "objutl.h"
37 #include "runerr.h"
38 #include "ut8_rtl.h"
39 
40 #undef EXTERN
41 #define EXTERN
42 #include "ut8lib.h"
43 
44 
45 
46 /**
47  *  Read a character from an UTF-8 file.
48  *  @return the character read, or EOF at the end of the file.
49  *  @exception RANGE_ERROR The file contains an illegal encoding.
50  */
ut8_getc(listType arguments)51 objectType ut8_getc (listType arguments)
52 
53   { /* ut8_getc */
54     isit_file(arg_1(arguments));
55     return bld_char_temp(
56         ut8Getc(take_file(arg_1(arguments))));
57   } /* ut8_getc */
58 
59 
60 
61 /**
62  *  Return a string read with a maximum length from an UTF-8 file.
63  *  @return the string read.
64  *  @exception RANGE_ERROR The length is negative or the file
65  *             contains an illegal encoding.
66  */
ut8_gets(listType arguments)67 objectType ut8_gets (listType arguments)
68 
69   { /* ut8_gets */
70     isit_file(arg_1(arguments));
71     isit_int(arg_2(arguments));
72     return bld_stri_temp(
73         ut8Gets(take_file(arg_1(arguments)), take_int(arg_2(arguments))));
74   } /* ut8_gets */
75 
76 
77 
78 /**
79  *  Read a line from an UTF-8 file.
80  *  The function accepts lines ending with '\n', "\r\n" or EOF.
81  *  The line ending characters are not copied into the string.
82  *  That means that the '\r' of a "\r\n" sequence is silently removed.
83  *  When the function is left the 2nd parameter (terminationChar)
84  *  contains '\n' or EOF.
85  *  @return the line read.
86  *  @exception RANGE_ERROR The file contains an illegal encoding.
87  *  @exception MEMORY_ERROR Not enough memory to represent the result.
88  *  @exception FILE_ERROR A system function returns an error.
89  */
ut8_line_read(listType arguments)90 objectType ut8_line_read (listType arguments)
91 
92   {
93     objectType terminationChar;
94 
95   /* ut8_line_read */
96     isit_file(arg_1(arguments));
97     terminationChar = arg_2(arguments);
98     isit_char(terminationChar);
99     is_variable(terminationChar);
100     return bld_stri_temp(
101         ut8LineRead(take_file(arg_1(arguments)),
102                     &terminationChar->value.charValue));
103   } /* ut8_line_read */
104 
105 
106 
107 /**
108  *  Set the current file position.
109  *  The file position is measured in bytes from the start of the file.
110  *  The first byte in the file has the position 1.
111  *  If the file position would be in the middle of an UTF-8 encoded
112  *  character the position is advanced to the beginning of the next
113  *  UTF-8 character.
114  *  @exception RANGE_ERROR The file position is negative or zero.
115  *  @exception FILE_ERROR The system function returns an error.
116  */
ut8_seek(listType arguments)117 objectType ut8_seek (listType arguments)
118 
119   { /* ut8_seek */
120     isit_file(arg_1(arguments));
121     isit_int(arg_2(arguments));
122     ut8Seek(take_file(arg_1(arguments)), take_int(arg_2(arguments)));
123     return SYS_EMPTY_OBJECT;
124   } /* ut8_seek */
125 
126 
127 
128 /**
129  *  Read a word from an UTF-8 file.
130  *  Before reading the word it skips spaces and tabs. The function
131  *  accepts words ending with ' ', '\t', '\n', "\r\n" or EOF.
132  *  The word ending characters are not copied into the string.
133  *  That means that the '\r' of a "\r\n" sequence is silently removed.
134  *  When the function is left the 2nd parameter (terminationChar)
135  *  contains ' ', '\t', '\n' or EOF.
136  *  @return the word read.
137  *  @exception RANGE_ERROR The file contains an illegal encoding.
138  *  @exception MEMORY_ERROR Not enough memory to represent the result.
139  *  @exception FILE_ERROR A system function returns an error.
140  */
ut8_word_read(listType arguments)141 objectType ut8_word_read (listType arguments)
142 
143   {
144     objectType terminationChar;
145 
146   /* ut8_word_read */
147     isit_file(arg_1(arguments));
148     terminationChar = arg_2(arguments);
149     isit_char(terminationChar);
150     is_variable(terminationChar);
151     return bld_stri_temp(
152         ut8WordRead(take_file(arg_1(arguments)),
153                     &terminationChar->value.charValue));
154   } /* ut8_word_read */
155 
156 
157 
158 /**
159  *  Write a string to an UTF-8 file.
160  *  @exception FILE_ERROR A system function returns an error.
161  */
ut8_write(listType arguments)162 objectType ut8_write (listType arguments)
163 
164   { /* ut8_write */
165     isit_file(arg_1(arguments));
166     isit_stri(arg_2(arguments));
167     ut8Write(take_file(arg_1(arguments)), take_stri(arg_2(arguments)));
168     return SYS_EMPTY_OBJECT;
169   } /* ut8_write */
170