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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/* -*- c -*- */
/*
* output.c
*
* chpp
*
* Copyright (C) 1997-1998 Mark Probst
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "memory.h"
#include "output.h"
extern int generateDependencies;
void
dummyOutChar (void *state, char c)
{
}
void
dummyOutString (void *state, const char *str, int length)
{
}
void
dummyOutValue (void *state, value *theValue, int byRef)
{
}
void
dynstringOutChar (void *state, char c)
{
dsAppendChar((dynstring*)state, c);
}
void
dynstringOutString (void *state, const char *str, int length)
{
dsAppendString((dynstring*)state, str, length);
}
void
dynstringOutValue (void *state, value *theValue, int byRef)
{
if (theValue->type == VALUE_SCALAR)
dsAppendString((dynstring*)state,
theValue->v.scalar.scalar.data, theValue->v.scalar.scalar.length);
else
{
value *scalarValue = valueNewScalarFromValue(theValue);
dsAppendString((dynstring*)state,
scalarValue->v.scalar.scalar.data, scalarValue->v.scalar.scalar.length);
}
}
void
fileOutChar (void *state, char c)
{
if (!generateDependencies)
putc(c, (FILE*)state);
}
void
fileOutString (void *state, const char *str, int length)
{
if (!generateDependencies)
fwrite(str, length, 1, (FILE*)state);
}
void
fileOutValue (void *state, value *theValue, int byRef)
{
if (!generateDependencies)
{
if (theValue->type == VALUE_SCALAR)
fwrite(theValue->v.scalar.scalar.data, theValue->v.scalar.scalar.length,
1, (FILE*)state);
else
{
value *scalarValue = valueNewScalarFromValue(theValue);
fwrite(scalarValue->v.scalar.scalar.data, scalarValue->v.scalar.scalar.length,
1, (FILE*)state);
}
}
}
void
valueOutChar (void *state, char c)
{
outputStateValue *theState = (outputStateValue*)state;
if (theState->theValue == 0)
{
char string[2] = { c, '\0' };
theState->theValue = valueNewScalarFromCString(string);
}
else
{
if (theState->needCopy)
theState->theValue = valueCopy(theState->theValue);
valueTransformToScalar(theState->theValue);
dsAppendChar(&theState->theValue->v.scalar.scalar, c);
}
theState->needCopy = 0;
}
void
valueOutString (void *state, const char *str, int length)
{
outputStateValue *theState = (outputStateValue*)state;
if (length > 0)
{
if (theState->theValue == 0)
theState->theValue = valueNewScalarFromBytes(str, length);
else
{
if (theState->needCopy)
theState->theValue = valueCopy(theState->theValue);
valueTransformToScalar(theState->theValue);
dsAppendString(&theState->theValue->v.scalar.scalar, str, length);
}
theState->needCopy = 0;
}
}
void
valueOutValue (void *state, value *appendedValue, int byRef)
{
outputStateValue *theState = (outputStateValue*)state;
value *theValue = theState->theValue;
if (theState->theValue == 0)
{
if (theState->byReference)
{
theState->theValue = appendedValue;
theState->needCopy = !byRef;
}
else
theState->theValue = valueCopy(appendedValue);
}
else
{
if (theState->needCopy)
theState->theValue = valueCopy(theState->theValue);
if (theState->theValue->type == VALUE_LIST
&& appendedValue->type == VALUE_LIST)
valueListAppendList(theValue, appendedValue, 0);
else
{
valueTransformToScalar(theState->theValue);
if (appendedValue->type != VALUE_SCALAR)
appendedValue = valueNewScalarFromValue(appendedValue);
dsAppendString(&theState->theValue->v.scalar.scalar,
appendedValue->v.scalar.scalar.data,
appendedValue->v.scalar.scalar.length);
}
theState->needCopy = 0;
}
}
outputWriter
owNewDummy (void)
{
outputWriter ow;
ow.outChar = dummyOutChar;
ow.outString = dummyOutString;
ow.outValue = dummyOutValue;
ow.enabled = 1;
ow.state = 0;
return ow;
}
outputWriter
owNewDynstring (dynstring *ds)
{
outputWriter ow;
ow.outChar = dynstringOutChar;
ow.outString = dynstringOutString;
ow.outValue = dynstringOutValue;
ow.enabled = 1;
ow.state = ds;
return ow;
}
outputWriter
owNewFile (FILE *file)
{
outputWriter ow;
ow.outChar = fileOutChar;
ow.outString = fileOutString;
ow.outValue = fileOutValue;
ow.enabled = 1;
ow.state = file;
return ow;
}
outputWriter
owNewValue (int byReference)
{
outputWriter ow;
outputStateValue *state = memXAlloc(sizeof(outputStateValue));
ow.outChar = valueOutChar;
ow.outString = valueOutString;
ow.outValue = valueOutValue;
ow.enabled = 1;
ow.state = state;
state->theValue = 0;
state->byReference = byReference;
state->needCopy = 0;
return ow;
}
value*
owValueValue (outputWriter *ow)
{
outputStateValue *state = (outputStateValue*)ow->state;
if (state->theValue == 0)
return valueNewUndefined();
return state->theValue;
}
int
owValueNeedCopy (outputWriter *ow)
{
outputStateValue *state = (outputStateValue*)ow->state;
return state->needCopy;
}
|