1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8-*- */
4 * This file is part of The Croco Library
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2.1 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * Author: Dodji Seketeli
21 * See COPYRIGHTS file for copyright information.
27 #include "cr-enc-handler.h"
32 *The definition of the #CRInput class.
40 *The private attributes of
41 *the #CRInputPriv class.
53 *The index of the next byte
56 gulong next_byte_index;
59 *The current line number
64 *The current col number
69 gboolean end_of_input;
72 *the reference count of this
79 #define PRIVATE(object) (object)->priv
81 /***************************
83 **************************/
84 #define CR_INPUT_MEM_CHUNK_SIZE 1024 * 4
86 static CRInput *cr_input_new_real (void);
89 cr_input_new_real (void)
91 CRInput *result = NULL;
93 result = g_try_malloc (sizeof (CRInput));
95 cr_utils_trace_info ("Out of memory");
98 memset (result, 0, sizeof (CRInput));
100 PRIVATE (result) = g_try_malloc (sizeof (CRInputPriv));
101 if (!PRIVATE (result)) {
102 cr_utils_trace_info ("Out of memory");
106 memset (PRIVATE (result), 0, sizeof (CRInputPriv));
107 PRIVATE (result)->free_in_buf = TRUE;
116 * cr_input_new_from_buf:
117 *@a_buf: the memory buffer to create the input stream from.
118 *The #CRInput keeps this pointer so user should not free it !.
119 *@a_len: the size of the input buffer.
120 *@a_enc: the buffer's encoding.
121 *@a_free_buf: if set to TRUE, this a_buf will be freed
122 *at the destruction of this instance. If set to false, it is up
123 *to the caller to free it.
125 *Creates a new input stream from a memory buffer.
126 *Returns the newly built instance of #CRInput.
129 cr_input_new_from_buf (guchar * a_buf,
131 enum CREncoding a_enc,
134 CRInput *result = NULL;
135 enum CRStatus status = CR_OK;
136 CREncHandler *enc_handler = NULL;
139 g_return_val_if_fail (a_buf, NULL);
141 result = cr_input_new_real ();
142 g_return_val_if_fail (result, NULL);
144 /*transform the encoding in utf8 */
145 if (a_enc != CR_UTF_8) {
146 enc_handler = cr_enc_handler_get_instance (a_enc);
151 status = cr_enc_handler_convert_input
152 (enc_handler, a_buf, &len,
153 &PRIVATE (result)->in_buf,
154 &PRIVATE (result)->in_buf_size);
157 PRIVATE (result)->free_in_buf = TRUE;
158 if (a_free_buf == TRUE && a_buf) {
162 PRIVATE (result)->nb_bytes = PRIVATE (result)->in_buf_size;
164 PRIVATE (result)->in_buf = (guchar *) a_buf;
165 PRIVATE (result)->in_buf_size = a_len;
166 PRIVATE (result)->nb_bytes = a_len;
167 PRIVATE (result)->free_in_buf = a_free_buf;
169 PRIVATE (result)->line = 1;
170 PRIVATE (result)->col = 0;
175 cr_input_destroy (result);
183 * cr_input_new_from_uri:
184 *@a_file_uri: the file to create *the input stream from.
185 *@a_enc: the encoding of the file *to create the input from.
187 *Creates a new input stream from
190 *Returns the newly created input stream if
191 *this method could read the file and create it,
196 cr_input_new_from_uri (const gchar * a_file_uri, enum CREncoding a_enc)
198 CRInput *result = NULL;
199 enum CRStatus status = CR_OK;
200 FILE *file_ptr = NULL;
201 guchar tmp_buf[CR_INPUT_MEM_CHUNK_SIZE] = { 0 };
205 gboolean loop = TRUE;
208 g_return_val_if_fail (a_file_uri, NULL);
210 file_ptr = fopen (a_file_uri, "r");
212 if (file_ptr == NULL) {
215 cr_utils_trace_debug ("could not open file");
217 g_warning ("Could not open file %s\n", a_file_uri);
224 nb_read = fread (tmp_buf, 1 /*read bytes */ ,
225 CR_INPUT_MEM_CHUNK_SIZE /*nb of bytes */ ,
228 if (nb_read != CR_INPUT_MEM_CHUNK_SIZE) {
229 /*we read less chars than we wanted */
230 if (feof (file_ptr)) {
235 cr_utils_trace_debug ("an io error occured");
241 if (status == CR_OK) {
243 buf = g_realloc (buf, len + CR_INPUT_MEM_CHUNK_SIZE);
244 memcpy (buf + len, tmp_buf, nb_read);
246 buf_size += CR_INPUT_MEM_CHUNK_SIZE;
250 if (status == CR_OK) {
251 result = cr_input_new_from_buf (buf, len, a_enc, TRUE);
256 *we should free buf here because it's own by CRInput.
257 *(see the last parameter of cr_input_new_from_buf().
278 *@a_this: the current instance of #CRInput.
280 *The destructor of the #CRInput class.
283 cr_input_destroy (CRInput * a_this)
288 if (PRIVATE (a_this)) {
289 if (PRIVATE (a_this)->in_buf && PRIVATE (a_this)->free_in_buf) {
290 g_free (PRIVATE (a_this)->in_buf);
291 PRIVATE (a_this)->in_buf = NULL;
294 g_free (PRIVATE (a_this));
295 PRIVATE (a_this) = NULL;
303 *@a_this: the current instance of #CRInput.
305 *Increments the reference count of the current
306 *instance of #CRInput.
309 cr_input_ref (CRInput * a_this)
311 g_return_if_fail (a_this && PRIVATE (a_this));
313 PRIVATE (a_this)->ref_count++;
318 *@a_this: the current instance of #CRInput.
320 *Decrements the reference count of this instance
321 *of #CRInput. If the reference count goes down to
322 *zero, this instance is destroyed.
324 * Returns TRUE if the instance of #CRInput got destroyed, false otherwise.
327 cr_input_unref (CRInput * a_this)
329 g_return_val_if_fail (a_this && PRIVATE (a_this), FALSE);
331 if (PRIVATE (a_this)->ref_count) {
332 PRIVATE (a_this)->ref_count--;
335 if (PRIVATE (a_this)->ref_count == 0) {
336 cr_input_destroy (a_this);
343 * cr_input_end_of_input:
344 *@a_this: the current instance of #CRInput.
345 *@a_end_of_input: out parameter. Is set to TRUE if
346 *the current instance has reached the end of its input buffer,
349 *Tests wether the current instance of
350 *#CRInput has reached its input buffer.
352 * Returns CR_OK upon successful completion, an error code otherwise.
353 * Note that all the out parameters of this method are valid if
354 * and only if this method returns CR_OK.
357 cr_input_end_of_input (CRInput const * a_this, gboolean * a_end_of_input)
359 g_return_val_if_fail (a_this && PRIVATE (a_this)
360 && a_end_of_input, CR_BAD_PARAM_ERROR);
362 *a_end_of_input = (PRIVATE (a_this)->next_byte_index
363 >= PRIVATE (a_this)->in_buf_size) ? TRUE : FALSE;
369 * cr_input_get_nb_bytes_left:
370 *@a_this: the current instance of #CRInput.
372 *Returns the number of bytes left in the input stream
373 *before the end, -1 in case of error.
376 cr_input_get_nb_bytes_left (CRInput const * a_this)
378 g_return_val_if_fail (a_this && PRIVATE (a_this), -1);
379 g_return_val_if_fail (PRIVATE (a_this)->nb_bytes
380 <= PRIVATE (a_this)->in_buf_size, -1);
381 g_return_val_if_fail (PRIVATE (a_this)->next_byte_index
382 <= PRIVATE (a_this)->nb_bytes, -1);
384 if (PRIVATE (a_this)->end_of_input)
387 return PRIVATE (a_this)->nb_bytes - PRIVATE (a_this)->next_byte_index;
391 * cr_input_read_byte:
392 *@a_this: the current instance of #CRInput.
393 *@a_byte: out parameter the returned byte.
395 *Gets the next byte of the input.
396 *Updates the state of the input so that
397 *the next invocation of this method returns
398 *the next coming byte.
400 *Returns CR_OK upon successful completion, an error code
401 *otherwise. All the out parameters of this method are valid if
402 *and only if this method returns CR_OK.
405 cr_input_read_byte (CRInput * a_this, guchar * a_byte)
407 g_return_val_if_fail (a_this && PRIVATE (a_this)
408 && a_byte, CR_BAD_PARAM_ERROR);
410 g_return_val_if_fail (PRIVATE (a_this)->next_byte_index <=
411 PRIVATE (a_this)->nb_bytes, CR_BAD_PARAM_ERROR);
413 if (PRIVATE (a_this)->end_of_input == TRUE)
414 return CR_END_OF_INPUT_ERROR;
416 *a_byte = PRIVATE (a_this)->in_buf[PRIVATE (a_this)->next_byte_index];
418 if (PRIVATE (a_this)->nb_bytes -
419 PRIVATE (a_this)->next_byte_index < 2) {
420 PRIVATE (a_this)->end_of_input = TRUE;
422 PRIVATE (a_this)->next_byte_index++;
429 * cr_input_read_char:
430 *@a_this: the current instance of CRInput.
431 *@a_char: out parameter. The read character.
433 *Reads an unicode character from the current instance of
436 *Returns CR_OK upon successful completion, an error code
440 cr_input_read_char (CRInput * a_this, guint32 * a_char)
442 enum CRStatus status = CR_OK;
446 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_char,
449 if (PRIVATE (a_this)->end_of_input == TRUE)
450 return CR_END_OF_INPUT_ERROR;
452 nb_bytes_left = cr_input_get_nb_bytes_left (a_this);
454 if (nb_bytes_left < 1) {
455 return CR_END_OF_INPUT_ERROR;
458 status = cr_utils_read_char_from_utf8_buf
459 (PRIVATE (a_this)->in_buf
461 PRIVATE (a_this)->next_byte_index,
462 nb_bytes_left, a_char, &consumed);
464 if (status == CR_OK) {
465 /*update next byte index */
466 PRIVATE (a_this)->next_byte_index += consumed;
468 /*update line and column number */
469 if (PRIVATE (a_this)->end_of_line == TRUE) {
470 PRIVATE (a_this)->col = 1;
471 PRIVATE (a_this)->line++;
472 PRIVATE (a_this)->end_of_line = FALSE;
473 } else if (*a_char != '\n') {
474 PRIVATE (a_this)->col++;
477 if (*a_char == '\n') {
478 PRIVATE (a_this)->end_of_line = TRUE;
487 * cr_input_set_line_num:
488 *@a_this: the "this pointer" of the current instance of #CRInput.
489 *@a_line_num: the new line number.
491 *Setter of the current line number.
493 *Return CR_OK upon successful completion, an error code otherwise.
496 cr_input_set_line_num (CRInput * a_this, glong a_line_num)
498 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
500 PRIVATE (a_this)->line = a_line_num;
506 * cr_input_get_line_num:
507 *@a_this: the "this pointer" of the current instance of #CRInput.
508 *@a_line_num: the returned line number.
510 *Getter of the current line number.
512 *Returns CR_OK upon successful completion, an error code otherwise.
515 cr_input_get_line_num (CRInput const * a_this, glong * a_line_num)
517 g_return_val_if_fail (a_this && PRIVATE (a_this)
518 && a_line_num, CR_BAD_PARAM_ERROR);
520 *a_line_num = PRIVATE (a_this)->line;
526 * cr_input_set_column_num:
527 *@a_this: the "this pointer" of the current instance of #CRInput.
528 *@a_col: the new column number.
530 *Setter of the current column number.
532 *Returns CR_OK upon successful completion, an error code otherwise.
535 cr_input_set_column_num (CRInput * a_this, glong a_col)
537 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
539 PRIVATE (a_this)->col = a_col;
545 * cr_input_get_column_num:
546 *@a_this: the "this pointer" of the current instance of #CRInput.
547 *@a_col: out parameter
549 *Getter of the current column number.
551 *Returns CR_OK upon successful completion, an error code otherwise.
554 cr_input_get_column_num (CRInput const * a_this, glong * a_col)
556 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_col,
559 *a_col = PRIVATE (a_this)->col;
565 * cr_input_increment_line_num:
566 *@a_this: the "this pointer" of the current instance of #CRInput.
567 *@a_increment: the increment to add to the line number.
569 *Increments the current line number.
571 *Returns CR_OK upon successful completion, an error code otherwise.
574 cr_input_increment_line_num (CRInput * a_this, glong a_increment)
576 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
578 PRIVATE (a_this)->line += a_increment;
584 * cr_input_increment_col_num:
585 *@a_this: the "this pointer" of the current instance of #CRInput.
586 *@a_increment: the increment to add to the column number.
588 *Increments the current column number.
590 *Returns CR_OK upon successful completion, an error code otherwise.
593 cr_input_increment_col_num (CRInput * a_this, glong a_increment)
595 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
597 PRIVATE (a_this)->col += a_increment;
603 * cr_input_consume_char:
604 *@a_this: the this pointer.
605 *@a_char: the character to consume. If set to zero,
606 *consumes any character.
608 *Consumes the next character of the input stream if
609 *and only if that character equals a_char.
611 *Returns CR_OK upon successful completion, CR_PARSING_ERROR if
612 *next char is different from a_char, an other error code otherwise
615 cr_input_consume_char (CRInput * a_this, guint32 a_char)
618 enum CRStatus status;
620 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
622 if ((status = cr_input_peek_char (a_this, &c)) != CR_OK) {
626 if (c == a_char || a_char == 0) {
627 status = cr_input_read_char (a_this, &c);
629 return CR_PARSING_ERROR;
636 * cr_input_consume_chars:
637 *@a_this: the this pointer of the current instance of #CRInput.
638 *@a_char: the character to consume.
639 *@a_nb_char: in/out parameter. The number of characters to consume.
640 *If set to a negative value, the function will consume all the occurences
642 *After return, if the return value equals CR_OK, this variable contains
643 *the number of characters actually consumed.
645 *Consumes up to a_nb_char occurences of the next contiguous characters
646 *which equal a_char. Note that the next character of the input stream
647 **MUST* equal a_char to trigger the consumption, or else, the error
648 *code CR_PARSING_ERROR is returned.
649 *If the number of contiguous characters that equals a_char is less than
650 *a_nb_char, then this function consumes all the characters it can consume.
652 *Returns CR_OK if at least one character has been consumed, an error code
656 cr_input_consume_chars (CRInput * a_this, guint32 a_char, gulong * a_nb_char)
658 enum CRStatus status = CR_OK;
659 gulong nb_consumed = 0;
661 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_nb_char,
664 g_return_val_if_fail (a_char != 0 || a_nb_char != NULL,
667 for (nb_consumed = 0; ((status == CR_OK)
669 && nb_consumed < *a_nb_char));
671 status = cr_input_consume_char (a_this, a_char);
674 *a_nb_char = nb_consumed;
676 if ((nb_consumed > 0)
677 && ((status == CR_PARSING_ERROR)
678 || (status == CR_END_OF_INPUT_ERROR))) {
686 * cr_input_consume_white_spaces:
687 *@a_this: the "this pointer" of the current instance of #CRInput.
688 *@a_nb_chars: in/out parameter. The number of white spaces to
689 *consume. After return, holds the number of white spaces actually consumed.
691 *Same as cr_input_consume_chars() but this one consumes white
694 *Returns CR_OK upon successful completion, an error code otherwise.
697 cr_input_consume_white_spaces (CRInput * a_this, gulong * a_nb_chars)
699 enum CRStatus status = CR_OK;
700 guint32 cur_char = 0,
703 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_nb_chars,
706 for (nb_consumed = 0;
707 ((*a_nb_chars > 0) && (nb_consumed < *a_nb_chars));
709 status = cr_input_peek_char (a_this, &cur_char);
713 /*if the next char is a white space, consume it ! */
714 if (cr_utils_is_white_space (cur_char) == TRUE) {
715 status = cr_input_read_char (a_this, &cur_char);
725 if (nb_consumed && status == CR_END_OF_INPUT_ERROR) {
733 * cr_input_peek_char:
734 *@a_this: the current instance of #CRInput.
735 *@a_char: out parameter. The returned character.
737 *Same as cr_input_read_char() but does not update the
738 *internal state of the input stream. The next call
739 *to cr_input_peek_char() or cr_input_read_char() will thus
740 *return the same character as the current one.
742 *Returns CR_OK upon successful completion, an error code
746 cr_input_peek_char (CRInput const * a_this, guint32 * a_char)
748 enum CRStatus status = CR_OK;
752 g_return_val_if_fail (a_this && PRIVATE (a_this)
753 && a_char, CR_BAD_PARAM_ERROR);
755 if (PRIVATE (a_this)->next_byte_index >=
756 PRIVATE (a_this)->in_buf_size) {
757 return CR_END_OF_INPUT_ERROR;
760 nb_bytes_left = cr_input_get_nb_bytes_left (a_this);
762 if (nb_bytes_left < 1) {
763 return CR_END_OF_INPUT_ERROR;
766 status = cr_utils_read_char_from_utf8_buf
767 (PRIVATE (a_this)->in_buf +
768 PRIVATE (a_this)->next_byte_index,
769 nb_bytes_left, a_char, &consumed);
775 * cr_input_peek_byte:
776 *@a_this: the current instance of #CRInput.
777 *@a_origin: the origin to consider in the calculation
778 *of the position of the byte to peek.
779 *@a_offset: the offset of the byte to peek, starting from
780 *the origin specified by a_origin.
781 *@a_byte: out parameter the peeked byte.
783 *Gets a byte from the input stream,
784 *starting from the current position in the input stream.
785 *Unlike cr_input_peek_next_byte() this method
786 *does not update the state of the current input stream.
787 *Subsequent calls to cr_input_peek_byte with the same arguments
788 *will return the same byte.
790 *Returns CR_OK upon successful completion or,
791 *CR_BAD_PARAM_ERROR if at least one of the parameters is invalid;
792 *CR_OUT_OF_BOUNDS_ERROR if the indexed byte is out of bounds.
795 cr_input_peek_byte (CRInput const * a_this, enum CRSeekPos a_origin,
796 gulong a_offset, guchar * a_byte)
798 gulong abs_offset = 0;
800 g_return_val_if_fail (a_this && PRIVATE (a_this)
801 && a_byte, CR_BAD_PARAM_ERROR);
806 abs_offset = PRIVATE (a_this)->next_byte_index - 1 + a_offset;
810 abs_offset = a_offset;
814 abs_offset = PRIVATE (a_this)->in_buf_size - 1 - a_offset;
818 return CR_BAD_PARAM_ERROR;
821 if (abs_offset < PRIVATE (a_this)->in_buf_size) {
823 *a_byte = PRIVATE (a_this)->in_buf[abs_offset];
828 return CR_END_OF_INPUT_ERROR;
833 * cr_input_peek_byte2:
834 *@a_this: the current byte input stream.
835 *@a_offset: the offset of the byte to peek, starting
836 *from the current input position pointer.
837 *@a_eof: out parameter. Is set to true is we reach end of
838 *stream. If set to NULL by the caller, this parameter is not taken
841 *Same as cr_input_peek_byte() but with a simplified
844 *Returns the read byte or 0 if something bad happened.
847 cr_input_peek_byte2 (CRInput const * a_this, gulong a_offset, gboolean * a_eof)
850 enum CRStatus status = CR_ERROR;
852 g_return_val_if_fail (a_this && PRIVATE (a_this), 0);
857 status = cr_input_peek_byte (a_this, CR_SEEK_CUR, a_offset, &result);
859 if ((status == CR_END_OF_INPUT_ERROR)
867 * cr_input_get_byte_addr:
868 *@a_this: the current instance of #CRInput.
869 *@a_offset: the offset of the byte in the input stream starting
870 *from the beginning of the stream.
872 *Gets the memory address of the byte located at a given offset
873 *in the input stream.
875 *Returns the address, otherwise NULL if an error occured.
878 cr_input_get_byte_addr (CRInput * a_this, gulong a_offset)
880 g_return_val_if_fail (a_this && PRIVATE (a_this), NULL);
882 if (a_offset >= PRIVATE (a_this)->nb_bytes) {
886 return &PRIVATE (a_this)->in_buf[a_offset];
890 * cr_input_get_cur_byte_addr:
891 *@a_this: the current input stream
892 *@a_offset: out parameter. The returned address.
894 *Gets the address of the current character pointer.
896 *Returns CR_OK upon successful completion, an error code otherwise.
899 cr_input_get_cur_byte_addr (CRInput * a_this, guchar ** a_offset)
901 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_offset,
904 if (!PRIVATE (a_this)->next_byte_index) {
905 return CR_START_OF_INPUT_ERROR;
908 *a_offset = cr_input_get_byte_addr
909 (a_this, PRIVATE (a_this)->next_byte_index - 1);
915 * cr_input_seek_index:
916 *@a_this: the current instance of #CRInput.
917 *@a_origin: the origin to consider during the calculation
918 *of the absolute position of the new "current byte index".
919 *@a_pos: the relative offset of the new "current byte index."
920 *This offset is relative to the origin a_origin.
922 *Sets the "current byte index" of the current instance
923 *of #CRInput. Next call to cr_input_get_byte() will return
924 *the byte next after the new "current byte index".
926 *Returns CR_OK upon successful completion otherwise returns
927 *CR_BAD_PARAM_ERROR if at least one of the parameters is not valid
928 *or CR_OUT_BOUNDS_ERROR in case of error.
931 cr_input_seek_index (CRInput * a_this, enum CRSeekPos a_origin, gint a_pos)
934 glong abs_offset = 0;
936 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
941 abs_offset = PRIVATE (a_this)->next_byte_index - 1 + a_pos;
949 abs_offset = PRIVATE (a_this)->in_buf_size - 1 - a_pos;
953 return CR_BAD_PARAM_ERROR;
957 && (gulong) abs_offset < PRIVATE (a_this)->nb_bytes) {
959 /*update the input stream's internal state */
960 PRIVATE (a_this)->next_byte_index = abs_offset + 1;
965 return CR_OUT_OF_BOUNDS_ERROR;
969 * cr_input_get_cur_pos:
970 *@a_this: the current instance of #CRInput.
971 *@a_pos: out parameter. The returned position.
973 *Gets the position of the "current byte index" which
974 *is basically the position of the last returned byte in the
977 *Returns CR_OK upon successful completion. Otherwise,
978 *CR_BAD_PARAMETER_ERROR if at least one of the arguments is invalid.
979 *CR_START_OF_INPUT if no call to either cr_input_read_byte()
980 *or cr_input_seek_index() have been issued before calling
981 *cr_input_get_cur_pos()
982 *Note that the out parameters of this function are valid if and only if this
983 *function returns CR_OK.
986 cr_input_get_cur_pos (CRInput const * a_this, CRInputPos * a_pos)
988 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pos,
991 a_pos->next_byte_index = PRIVATE (a_this)->next_byte_index;
992 a_pos->line = PRIVATE (a_this)->line;
993 a_pos->col = PRIVATE (a_this)->col;
994 a_pos->end_of_line = PRIVATE (a_this)->end_of_line;
995 a_pos->end_of_file = PRIVATE (a_this)->end_of_input;
1001 * cr_input_get_parsing_location:
1002 *@a_this: the current instance of #CRInput
1003 *@a_loc: the set parsing location.
1005 *Gets the current parsing location.
1006 *The Parsing location is a public datastructure that
1007 *represents the current line/column/byte offset/ in the input
1010 *Returns CR_OK upon successful completion, an error
1014 cr_input_get_parsing_location (CRInput const *a_this,
1015 CRParsingLocation *a_loc)
1017 g_return_val_if_fail (a_this
1020 CR_BAD_PARAM_ERROR) ;
1022 a_loc->line = PRIVATE (a_this)->line ;
1023 a_loc->column = PRIVATE (a_this)->col ;
1024 if (PRIVATE (a_this)->next_byte_index) {
1025 a_loc->byte_offset = PRIVATE (a_this)->next_byte_index - 1 ;
1027 a_loc->byte_offset = PRIVATE (a_this)->next_byte_index ;
1033 * cr_input_get_cur_index:
1034 *@a_this: the "this pointer" of the current instance of
1036 *@a_index: out parameter. The returned index.
1038 *Getter of the next byte index.
1039 *It actually returns the index of the
1040 *next byte to be read.
1042 *Returns CR_OK upon successful completion, an error code
1046 cr_input_get_cur_index (CRInput const * a_this, glong * a_index)
1048 g_return_val_if_fail (a_this && PRIVATE (a_this)
1049 && a_index, CR_BAD_PARAM_ERROR);
1051 *a_index = PRIVATE (a_this)->next_byte_index;
1057 * cr_input_set_cur_index:
1058 *@a_this: the "this pointer" of the current instance
1060 *@a_index: the new index to set.
1062 *Setter of the next byte index.
1063 *It sets the index of the next byte to be read.
1065 *Returns CR_OK upon successful completion, an error code otherwise.
1068 cr_input_set_cur_index (CRInput * a_this, glong a_index)
1070 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
1072 PRIVATE (a_this)->next_byte_index = a_index;
1078 * cr_input_set_end_of_file:
1079 *@a_this: the current instance of #CRInput.
1080 *@a_eof: the new end of file flag.
1082 *Sets the end of file flag.
1084 *Returns CR_OK upon successful completion, an error code otherwise.
1087 cr_input_set_end_of_file (CRInput * a_this, gboolean a_eof)
1089 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
1091 PRIVATE (a_this)->end_of_input = a_eof;
1097 * cr_input_get_end_of_file:
1098 *@a_this: the current instance of #CRInput.
1099 *@a_eof: out parameter the place to put the end of
1102 *Gets the end of file flag.
1104 *Returns CR_OK upon successful completion, an error code otherwise.
1107 cr_input_get_end_of_file (CRInput const * a_this, gboolean * a_eof)
1109 g_return_val_if_fail (a_this && PRIVATE (a_this)
1110 && a_eof, CR_BAD_PARAM_ERROR);
1112 *a_eof = PRIVATE (a_this)->end_of_input;
1118 * cr_input_set_end_of_line:
1119 *@a_this: the current instance of #CRInput.
1120 *@a_eol: the new end of line flag.
1122 *Sets the end of line flag.
1124 *Returns CR_OK upon successful completion, an error code
1128 cr_input_set_end_of_line (CRInput * a_this, gboolean a_eol)
1130 g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
1132 PRIVATE (a_this)->end_of_line = a_eol;
1138 * cr_input_get_end_of_line:
1139 *@a_this: the current instance of #CRInput
1140 *@a_eol: out parameter. The place to put
1143 *Gets the end of line flag of the current input.
1145 *Returns CR_OK upon successful completion, an error code
1149 cr_input_get_end_of_line (CRInput const * a_this, gboolean * a_eol)
1151 g_return_val_if_fail (a_this && PRIVATE (a_this)
1152 && a_eol, CR_BAD_PARAM_ERROR);
1154 *a_eol = PRIVATE (a_this)->end_of_line;
1160 * cr_input_set_cur_pos:
1161 *@a_this: the "this pointer" of the current instance of
1163 *@a_pos: the new position.
1165 *Sets the current position in the input stream.
1167 * Returns CR_OK upon successful completion, an error code otherwise.
1170 cr_input_set_cur_pos (CRInput * a_this, CRInputPos const * a_pos)
1172 g_return_val_if_fail (a_this && PRIVATE (a_this) && a_pos,
1173 CR_BAD_PARAM_ERROR);
1175 cr_input_set_column_num (a_this, a_pos->col);
1176 cr_input_set_line_num (a_this, a_pos->line);
1177 cr_input_set_cur_index (a_this, a_pos->next_byte_index);
1178 cr_input_set_end_of_line (a_this, a_pos->end_of_line);
1179 cr_input_set_end_of_file (a_this, a_pos->end_of_file);