Allow optional input %? format

This commit is contained in:
zimoch
2010-06-09 08:05:05 +00:00
parent eae987e7f1
commit 1fb402e838
4 changed files with 62 additions and 20 deletions

View File

@ -1165,6 +1165,12 @@ matchInput()
return false;
}
if (consumed < 0)
{
if (fmt.flags & default_flag)
{
consumed = 0;
}
else
{
if (!(flags & AsyncMode) && onMismatch[0] != in_cmd)
{
@ -1175,6 +1181,7 @@ matchInput()
}
return false;
}
}
consumedInput += consumed;
break;
}
@ -1323,8 +1330,16 @@ scanValue(const StreamFormat& fmt, long& value)
scanLong(fmt, inputLine(consumedInput), value);
debug("StreamCore::scanValue(%s, format=%%%c, long) input=\"%s\"\n",
name(), fmt.conv, inputLine.expand(consumedInput)());
if (consumed < 0 ||
consumed > inputLine.length()-consumedInput) return -1;
if (consumed < 0)
{
if (fmt.flags & default_flag)
{
value = 0;
consumed = 0;
}
else return -1;
}
if (consumed > inputLine.length()-consumedInput) return -1;
debug("StreamCore::scanValue(%s) scanned %li\n",
name(), value);
flags |= GotValue;
@ -1346,8 +1361,16 @@ scanValue(const StreamFormat& fmt, double& value)
scanDouble(fmt, inputLine(consumedInput), value);
debug("StreamCore::scanValue(%s, format=%%%c, double) input=\"%s\"\n",
name(), fmt.conv, inputLine.expand(consumedInput)());
if (consumed < 0 ||
consumed > inputLine.length()-consumedInput) return -1;
if (consumed < 0)
{
if (fmt.flags & default_flag)
{
value = 0.0;
consumed = 0;
}
else return -1;
}
if (consumed > inputLine.length()-consumedInput) return -1;
debug("StreamCore::scanValue(%s) scanned %#g\n",
name(), value);
flags |= GotValue;
@ -1370,8 +1393,16 @@ scanValue(const StreamFormat& fmt, char* value, long maxlen)
scanString(fmt, inputLine(consumedInput), value, maxlen);
debug("StreamCore::scanValue(%s, format=%%%c, char*, maxlen=%ld) input=\"%s\"\n",
name(), fmt.conv, maxlen, inputLine.expand(consumedInput)());
if (consumed < 0 ||
consumed > inputLine.length()-consumedInput) return -1;
if (consumed < 0)
{
if (fmt.flags & default_flag)
{
value[0] = 0;
consumed = 0;
}
else return -1;
}
if (consumed > inputLine.length()-consumedInput) return -1;
#ifndef NO_TEMPORARY
debug("StreamCore::scanValue(%s) scanned \"%s\"\n",
name(), StreamBuffer(value, maxlen).expand()());

View File

@ -28,7 +28,8 @@ typedef enum {
space_flag = 0x04,
alt_flag = 0x08,
zero_flag = 0x10,
skip_flag = 0x20
skip_flag = 0x20,
default_flag = 0x40
} StreamFormatFlag;
typedef enum {

View File

@ -112,7 +112,7 @@ static void copyFormatString(StreamBuffer& info, const char* source)
const char* p = source - 1;
while (*p != '%' && *p != ')') p--;
info.append('%');
while (++p != source-1) info.append(*p);
while (++p != source-1) if (*p != '?') info.append(*p);
}
// Standard Long Converter for 'diouxX'
@ -278,7 +278,7 @@ scanString(const StreamFormat& fmt, const char* input,
if (*input == '\0')
{
// match empty string
value[0] = '\0';
if (value) value[0] = '\0';
return 0;
}
if (fmt.flags & skip_flag)

View File

@ -1504,6 +1504,16 @@ compileFormat(StreamBuffer& buffer, const char*& formatstr,
}
streamFormat.flags |= skip_flag;
break;
case '?':
if (formatType != ScanFormat)
{
errorMsg(line,
"Use of default modifier '?' "
"only allowed in input formats\n");
return false;
}
streamFormat.flags |= default_flag;
break;
default:
loop = false;
}