-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.c
52 lines (48 loc) · 1.24 KB
/
common.c
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
#include "common.h"
/**
* @brief Return the sign of a number.
*
* @param number The number to get the sign from.
*
* @return The sign of the number.
*/
int sign(float number)
{
return (int)(number > 0) - (int)(number < 0);
}
/**
* @brief Check if the received buffer starts with the given string.
*
* @param[in] string String buffer.
* @param[in] start_string Prefix to look for at the start of the string buffer.
*/
bool starts_with(char *string, char *start_string)
{
return (bool)!strncmp(string, start_string, strlen(start_string));
}
/**
* @brief Parse a float number in a given string.
*
* The parsing will start after a defined number of spaces that are expected
* before the float in the string.
*
* @param[in] string String to parse the float from.
* @param[in] string_size Size from the string to parse the float from.
* @param[in] spaces_before Number of spaces expected before the float.
*/
float parse_float(char *string, unsigned int string_size, int spaces_before)
{
unsigned int i;
char *pointer;
pointer = string;
for (i = 0; i < string_size; i++) {
if (string[i] == ' ')
spaces_before--;
if (string[i] == '\0')
return 0.;
pointer++;
if (spaces_before == 0)
break;
}
return strtof(pointer, NULL);
}