-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_str.sprut
105 lines (77 loc) · 3 KB
/
std_str.sprut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This is file `std_str.sprut'. The file contains description of C
string type and its type specific macros (see SPRUT documentation).
The macros is implemented with the aid of macros for work with
memory from the file `std_mem.sprut' or `std_bmem.sprut' therefore
one of these files must be placed before `std_str.sprut' in
'%extend' construction and the initiation function from the file
`std_mem.sprut' must be called before usage macros from this
file.
Copyright (C) 1997-2016 Vladimir Makarov.
Written by Vladimir Makarov <vmakarov@gcc.gnu.org>
This file is part of the tool SPRUT.
This is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
%type string_t
%import {
#include <stdio.h>
#include <string.h>
/* The following type is usual C repesentation of strings. */
typedef char *string_t;
}
%local {
/* Type specific macros (see SPRUT documentation) for `string_t': */
#define IR_BEGIN_string_t(a) ((a) = "")
#define IR_END_string_t(a)
#define IR_COPY_string_t(a, b) ((a) = (b))
#define IR_EQUAL_string_t(a, b) (strcmp ((a), (b)) == 0)
#define IR_PRINT_string_t(a) printf ("%s", (a))
#define IR_INPUT_string_t(file, a) input_string_t ((file), &(a))
#define IR_OUTPUT_string_t(file, a) output_string_t ((file), (a))
extern int input_string_t (FILE *f, char **str);
extern int output_string_t (FILE *f, const char *str);
}
%%
%%
/* The following function allocates memory on the top of current block
memory region (setting up its address in *STR) and inputs string to
the memory from given file F. The function returns TRUE if input
is ok, FALSE otherwise. */
int
input_string_t (FILE *f, char **str)
{
int length;
if (fread (&length, sizeof (length), 1, f) != sizeof (length) || length < 0)
return 1 /* TRUE */;
IR_TOP_EXPAND (length + 1);
*str = IR_TOP_BEGIN ();
IR_TOP_FINISH ();
return fread (*str, length + 1, 1, f) != length + 1;
}
/* The following function outputs given string STR (its length and its
bytes including finish zero byte) to given file F. The function
returns TRUE if output is ok, FALSE otherwise. */
int
output_string_t (FILE *f, const char *str)
{
int length;
length = strlen (str);
if (fwrite (&length, sizeof (length), 1, f) != sizeof (length))
return 1 /* TRUE */;
return fwrite (str, length + 1, 1, f) != length + 1;
}
/*
Local Variables:
mode:c
End:
*/