forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar.h
133 lines (120 loc) · 3.49 KB
/
scalar.h
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @file scalar.c
*
* @brief Functions for handling scalar key values, used on reading.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef ELEKTRA_PLUGIN_TOML_SCALAR_H
#define ELEKTRA_PLUGIN_TOML_SCALAR_H
#include <stdbool.h>
#include <stddef.h>
typedef enum
{
SCALAR_INTEGER_DEC,
SCALAR_INTEGER_HEX,
SCALAR_INTEGER_OCT,
SCALAR_INTEGER_BIN,
SCALAR_BOOLEAN,
SCALAR_FLOAT_NUM,
SCALAR_FLOAT_INF,
SCALAR_FLOAT_POS_INF,
SCALAR_FLOAT_NEG_INF,
SCALAR_FLOAT_NAN,
SCALAR_FLOAT_POS_NAN,
SCALAR_FLOAT_NEG_NAN,
SCALAR_STRING_COMMENT,
SCALAR_STRING_BARE,
SCALAR_STRING_LITERAL,
SCALAR_STRING_BASIC,
SCALAR_STRING_ML_LITERAL,
SCALAR_STRING_ML_BASIC,
SCALAR_DATE_OFFSET_DATETIME,
SCALAR_DATE_LOCAL_DATETIME,
SCALAR_DATE_LOCAL_DATE,
SCALAR_DATE_LOCAL_TIME,
} ScalarType;
typedef struct
{
ScalarType type;
char * str;
char * orig;
size_t leadingSpaces;
size_t line;
} Scalar;
/*
* @brief Creates a new scalar
*
* This function only creates a shallow copy of the supplied
* scalarString takes ownership of it. Therefore, the supplied string
* must not be freed in any other place than the freeScalar function.
* If this cannot be guarenteed, use createScalarDup for a deep copy instead.
*
* This function is used in the lexer during the reading of string.s
*
* @param type The type of the scalar.
* @param scalarString The raw string representation of the scalar.
* @param origString The original bytes as part of the file.
* @param line The line number, where the scalar was read from.
*
* @retval Pointer On success.
* @retval NULL When out-of-memory.
*/
Scalar * createScalar (ScalarType type, char * scalarString, char * origString, size_t line);
/*
* @brief Creates a new scalar
*
* Creates a deep copy of the given scalar string.
*
* This function is used in the lexer during the reading of non-string values.
*
* @param type The type of the scalar.
* @param scalarString The string representation of the scalar.
* @param origString The original bytes as part of the file.
* @param line The line number, where the scalar was read from.
*
* @retval Pointer On success.
* @retval NULL When out-of-memory.
*/
Scalar * createScalarDup (ScalarType type, const char * scalarString, const char * origString, size_t line);
/*
* @brief Frees up a scalar and any memory pointed within it.
*
* @param scalar Scalar to be freed.
*/
void freeScalar (Scalar * scalar);
/*
* @brief Converts the given scalar to format usable within Elektra.
*
* Converts binary, octal and hexadecimal scalars to decimal,
* Handles special cases for floats (inf, nan) and strips underscores.
* Applies escape characters to basic strings.
*
* @param scalar Scalar to be converted.
*
* @retval Pointer On Success.
* @retval NULL When supplying unknown scalar type or on conversion errors.
*/
char * translateScalar (const Scalar * scalar);
/*
* @brief Checks if the given string is bare.
*
* TOML bare strings only contain alphanumeric characters, underscores or hyphens.
*
* @param str String to be checked. Must not be NULL.
*
* @retval true If string only contains bare characters.
* @retval false If contains non-bare characters.
*/
bool isValidBareString (const char * str);
/*
* @brief Checks if the given scalar contains a valid RFC3339 datetime.
*
* @param scalar Scalare to be checked.
*
* @retval true If the scalar contains a valid RFC3339 datetime.
* @retval false Otherwise.
*/
bool isValidDateTime (const Scalar * scalar);
#endif // ELEKTRA_PLUGIN_TOML_SCALAR_H