mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
util: virParseVersionString: move to virstring.c
Signed-off-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
parent
bc8a1071ee
commit
f40179b9fb
@ -3303,6 +3303,7 @@ virStorageFileParseBackingStoreStr;
|
|||||||
|
|
||||||
|
|
||||||
# util/virstring.h
|
# util/virstring.h
|
||||||
|
virParseVersionString;
|
||||||
virSkipSpaces;
|
virSkipSpaces;
|
||||||
virSkipSpacesAndBackslash;
|
virSkipSpacesAndBackslash;
|
||||||
virSkipSpacesBackwards;
|
virSkipSpacesBackwards;
|
||||||
@ -3549,7 +3550,6 @@ virMemoryLimitIsSet;
|
|||||||
virMemoryLimitTruncate;
|
virMemoryLimitTruncate;
|
||||||
virMemoryMaxValue;
|
virMemoryMaxValue;
|
||||||
virParseOwnershipIds;
|
virParseOwnershipIds;
|
||||||
virParseVersionString;
|
|
||||||
virPipe;
|
virPipe;
|
||||||
virPipeNonBlock;
|
virPipeNonBlock;
|
||||||
virPipeQuiet;
|
virPipeQuiet;
|
||||||
|
@ -1019,3 +1019,50 @@ int virStringParseYesNo(const char *str, bool *result)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virParseVersionString:
|
||||||
|
* @str: const char pointer to the version string
|
||||||
|
* @version: unsigned long pointer to output the version number
|
||||||
|
* @allowMissing: true to treat 3 like 3.0.0, false to error out on
|
||||||
|
* missing minor or micro
|
||||||
|
*
|
||||||
|
* Parse an unsigned version number from a version string. Expecting
|
||||||
|
* 'major.minor.micro' format, ignoring an optional suffix.
|
||||||
|
*
|
||||||
|
* The major, minor and micro numbers are encoded into a single version number:
|
||||||
|
*
|
||||||
|
* 1000000 * major + 1000 * minor + micro
|
||||||
|
*
|
||||||
|
* Returns the 0 for success, -1 for error.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
virParseVersionString(const char *str, unsigned long *version,
|
||||||
|
bool allowMissing)
|
||||||
|
{
|
||||||
|
unsigned int major, minor = 0, micro = 0;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
if (virStrToLong_ui(str, &tmp, 10, &major) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!allowMissing && *tmp != '.')
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if ((*tmp == '.') && virStrToLong_ui(tmp + 1, &tmp, 10, &minor) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!allowMissing && *tmp != '.')
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if ((*tmp == '.') && virStrToLong_ui(tmp + 1, &tmp, 10, µ) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (major > UINT_MAX / 1000000 || minor > 999 || micro > 999)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
*version = 1000000 * major + 1000 * minor + micro;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@ -135,3 +135,7 @@ int virStringParsePort(const char *str,
|
|||||||
int virStringParseYesNo(const char *str,
|
int virStringParseYesNo(const char *str,
|
||||||
bool *result)
|
bool *result)
|
||||||
G_GNUC_WARN_UNUSED_RESULT;
|
G_GNUC_WARN_UNUSED_RESULT;
|
||||||
|
|
||||||
|
int virParseVersionString(const char *str,
|
||||||
|
unsigned long *version,
|
||||||
|
bool allowMissing);
|
||||||
|
@ -231,52 +231,6 @@ virScaleInteger(unsigned long long *value, const char *suffix,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* virParseVersionString:
|
|
||||||
* @str: const char pointer to the version string
|
|
||||||
* @version: unsigned long pointer to output the version number
|
|
||||||
* @allowMissing: true to treat 3 like 3.0.0, false to error out on
|
|
||||||
* missing minor or micro
|
|
||||||
*
|
|
||||||
* Parse an unsigned version number from a version string. Expecting
|
|
||||||
* 'major.minor.micro' format, ignoring an optional suffix.
|
|
||||||
*
|
|
||||||
* The major, minor and micro numbers are encoded into a single version number:
|
|
||||||
*
|
|
||||||
* 1000000 * major + 1000 * minor + micro
|
|
||||||
*
|
|
||||||
* Returns the 0 for success, -1 for error.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
virParseVersionString(const char *str, unsigned long *version,
|
|
||||||
bool allowMissing)
|
|
||||||
{
|
|
||||||
unsigned int major, minor = 0, micro = 0;
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
if (virStrToLong_ui(str, &tmp, 10, &major) < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!allowMissing && *tmp != '.')
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if ((*tmp == '.') && virStrToLong_ui(tmp + 1, &tmp, 10, &minor) < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!allowMissing && *tmp != '.')
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if ((*tmp == '.') && virStrToLong_ui(tmp + 1, &tmp, 10, µ) < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (major > UINT_MAX / 1000000 || minor > 999 || micro > 999)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
*version = 1000000 * major + 1000 * minor + micro;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format @val as a base-10 decimal number, in the
|
* Format @val as a base-10 decimal number, in the
|
||||||
* buffer @buf of size @buflen. To allocate a suitable
|
* buffer @buf of size @buflen. To allocate a suitable
|
||||||
|
@ -44,9 +44,6 @@ int virScaleInteger(unsigned long long *value, const char *suffix,
|
|||||||
unsigned long long scale, unsigned long long limit)
|
unsigned long long scale, unsigned long long limit)
|
||||||
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
|
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
|
||||||
|
|
||||||
int virParseVersionString(const char *str, unsigned long *version,
|
|
||||||
bool allowMissing);
|
|
||||||
|
|
||||||
char *virFormatIntDecimal(char *buf, size_t buflen, int val)
|
char *virFormatIntDecimal(char *buf, size_t buflen, int val)
|
||||||
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
|
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user