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

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

View File

@ -23,12 +23,13 @@
#define StreamFormat_h #define StreamFormat_h
typedef enum { typedef enum {
left_flag = 0x01, left_flag = 0x01,
sign_flag = 0x02, sign_flag = 0x02,
space_flag = 0x04, space_flag = 0x04,
alt_flag = 0x08, alt_flag = 0x08,
zero_flag = 0x10, zero_flag = 0x10,
skip_flag = 0x20 skip_flag = 0x20,
default_flag = 0x40
} StreamFormatFlag; } StreamFormatFlag;
typedef enum { typedef enum {

View File

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

View File

@ -1504,6 +1504,16 @@ compileFormat(StreamBuffer& buffer, const char*& formatstr,
} }
streamFormat.flags |= skip_flag; streamFormat.flags |= skip_flag;
break; 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: default:
loop = false; loop = false;
} }