some fixes to accented characters
[tangerine.git] / rom / dos / readitem.c
blob2b703e62acdfdcda0cedd29db989211017945ec0
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
8 #include <proto/exec.h>
9 #include <dos/rdargs.h>
10 #include <dos/dosasl.h>
11 #include <dos/dosextens.h>
12 #include "dos_intern.h"
14 /*****************************************************************************
16 NAME */
17 #include <proto/dos.h>
19 AROS_LH3(LONG, ReadItem,
21 /* SYNOPSIS */
22 AROS_LHA(STRPTR, buffer, D1),
23 AROS_LHA(LONG, maxchars, D2),
24 AROS_LHA(struct CSource *, input, D3),
26 /* LOCATION */
27 struct DosLibrary *, DOSBase, 135, Dos)
29 /* FUNCTION
30 Read an item from a given character source. Items are words
31 or quoted strings separated by whitespace or '=' just like on
32 the commandline. The separator is unread and the output string
33 is terminated by a NUL character.
35 INPUTS
36 buffer - Buffer to be filled.
37 maxchars - Size of the buffer. Must be at least 1 (for the NUL
38 terminator).
39 input - A ready to use CSource structure or NULL which means
40 "read from the input stream".
42 RESULT
43 One of ITEM_UNQUOTED - Normal word read.
44 ITEM_QUOTED - Quoted string read.
45 ITEM_NOTHING - End of line found. Nothing read.
46 ITEM_EQUAL - '=' read. Buffer is empty.
47 ITEM_ERROR - An error happened. IoErr() gives additional
48 information in that case.
50 NOTES
51 This function handles conversion of '**', '*"', etc inside quotes.
53 EXAMPLE
55 BUGS
57 SEE ALSO
59 INTERNALS
61 *****************************************************************************/
63 AROS_LIBFUNC_INIT
65 /* Macro to get a character from the input source */
66 #define GET(c) \
67 if(input!=NULL) \
68 { \
69 if(input->CS_CurChr>=input->CS_Length) \
70 c=EOF; \
71 else \
72 c=input->CS_Buffer[input->CS_CurChr++]; \
73 }else \
74 { \
75 c=FGetC(Input()); \
76 if(c==EOF&&*result) \
77 return ITEM_ERROR; \
80 /* Macro to push the character back */
81 #define UNGET() {if(input!=NULL) input->CS_CurChr--; else UnGetC(Input(),-1);}
83 STRPTR b=buffer;
84 LONG c;
85 LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
87 /* Skip leading whitespace characters */
90 GET(c);
91 } while (c==' '||c=='\t');
93 if(!c||c=='\n'||c==EOF||c==';')
95 *b=0;
96 return ITEM_NOTHING;
97 }else if(c=='=')
99 /* Found '='. Return it. */
100 *b=0;
101 return ITEM_EQUAL;
102 }else if(c=='\"')
103 /* Quoted item found. Convert Contents. */
104 for(;;)
106 if(!maxchars)
108 *buffer=0;
109 *result=ERROR_BUFFER_OVERFLOW;
110 return ITEM_ERROR;
112 maxchars--;
113 GET(c);
114 /* Convert ** to *, *" to ", *n to \n and *e to 0x1b. */
115 if(c=='*')
117 GET(c);
118 /* Check for premature end of line. */
119 if(!c||c=='\n'||c==EOF)
121 if(c!=EOF)
122 UNGET();
123 *buffer=0;
124 *result=ERROR_UNMATCHED_QUOTES;
125 return ITEM_ERROR;
126 }else if(c=='n'||c=='N')
127 c='\n';
128 else if(c=='e'||c=='E')
129 c=0x1b;
130 }else if(!c||c=='\n'||c==EOF)
132 if(c!=EOF)
133 UNGET();
134 *buffer=0;
135 *result=ERROR_UNMATCHED_QUOTES;
136 return ITEM_ERROR;
137 }else if(c=='\"')
139 /* " ends the item. */
140 *b=0;
141 return ITEM_QUOTED;
143 *b++=c;
145 else
147 /* Unquoted item. Store first character. */
148 if(!maxchars)
150 *buffer=0;
151 *result=ERROR_BUFFER_OVERFLOW;
152 return ITEM_ERROR;
154 maxchars--;
155 *b++=c;
156 /* Read up to the next terminator. */
157 for(;;)
159 if(!maxchars)
161 *buffer=0;
162 *result=ERROR_BUFFER_OVERFLOW;
163 return ITEM_ERROR;
165 maxchars--;
166 GET(c);
167 /* Check for terminator */
168 if(!c||c==' '||c=='\t'||c=='\n'||c=='='||c==EOF)
170 if(c!=EOF)
171 UNGET();
172 *b=0;
173 return ITEM_UNQUOTED;
175 *b++=c;
178 AROS_LIBFUNC_EXIT
179 } /* ReadItem */