2012-05-12 01:00:57 +02:00
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-05-12 01:00:57 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Utility functions used by the example programs.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "./example_util.h"
|
2014-08-30 04:07:17 +02:00
|
|
|
|
2012-05-12 01:00:57 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-04-23 04:33:22 +02:00
|
|
|
|
2014-09-11 08:35:48 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// String parsing
|
|
|
|
|
|
|
|
uint32_t ExUtilGetUInt(const char* const v, int base, int* const error) {
|
|
|
|
char* end = NULL;
|
|
|
|
const uint32_t n = (v != NULL) ? (uint32_t)strtoul(v, &end, base) : 0u;
|
|
|
|
if (end == v && error != NULL && !*error) {
|
|
|
|
*error = 1;
|
|
|
|
fprintf(stderr, "Error! '%s' is not an integer.\n",
|
|
|
|
(v != NULL) ? v : "(null)");
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ExUtilGetInt(const char* const v, int base, int* const error) {
|
|
|
|
return (int)ExUtilGetUInt(v, base, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ExUtilGetFloat(const char* const v, int* const error) {
|
|
|
|
char* end = NULL;
|
|
|
|
const float f = (v != NULL) ? (float)strtod(v, &end) : 0.f;
|
|
|
|
if (end == v && error != NULL && !*error) {
|
|
|
|
*error = 1;
|
|
|
|
fprintf(stderr, "Error! '%s' is not a floating point number.\n",
|
|
|
|
(v != NULL) ? v : "(null)");
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|