Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
rdma-mwe
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Titouan Soulard
rdma-mwe
Commits
db0a5a58
Commit
db0a5a58
authored
Jan 18, 2024
by
Titouan Soulard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
libpotoml: add parsing of long
parent
1a31dde3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
12 deletions
+47
-12
include/libpotoml/toml.h
include/libpotoml/toml.h
+3
-1
libpotoml/toml.c
libpotoml/toml.c
+44
-11
No files found.
include/libpotoml/toml.h
View file @
db0a5a58
...
...
@@ -23,7 +23,8 @@ enum PotomlState {
PotomlAfterPropertyName
=
0x31
,
PotomlBeforePropertyValue
=
0x32
,
PotomlPropertyValueString
=
0x40
,
PotomlPropertyValueDouble
=
0x41
,
PotomlPropertyValueLong
=
0x41
,
PotomlPropertyValueDouble
=
0x42
,
};
struct
PotomlTomlSubset
{
...
...
@@ -37,6 +38,7 @@ char potoml_toml_parse_file(struct CommonHashtableTable *hashtable, char *toml_f
struct
PotomlTomlSubset
*
potoml_toml_create_subset
(
struct
CommonHashtableTable
*
hashtable
,
const
char
*
section_name
);
char
*
potoml_toml_get_string
(
struct
PotomlTomlSubset
*
subset
,
const
char
*
property
);
double
potoml_toml_get_double
(
struct
PotomlTomlSubset
*
subset
,
const
char
*
property
);
long
potoml_toml_get_long
(
struct
PotomlTomlSubset
*
subset
,
const
char
*
property
);
void
potoml_toml_subset_free
(
struct
PotomlTomlSubset
*
subset
);
void
potoml_toml_free
(
struct
CommonHashtableTable
*
hashtable
);
libpotoml/toml.c
View file @
db0a5a58
...
...
@@ -6,10 +6,8 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
size_t
section_length
=
0
;
size_t
property_length
=
0
;
size_t
value_length
=
0
;
double
*
double_value
;
size_t
ci
;
char
c
;
char
s
=
PotomlLineBeginning
;
...
...
@@ -17,6 +15,8 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
char
*
property
=
malloc
(
POTOML_MAX_PROPERTY_LENGTH
*
sizeof
(
char
));
char
*
value
=
malloc
(
POTOML_MAX_PROPERTY_LENGTH
*
sizeof
(
char
));
void
*
casted_value
;
for
(
ci
=
0
;
ci
<
toml_length
;
ci
++
)
{
c
=
toml
[
ci
];
...
...
@@ -85,7 +85,7 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
if
(
c
==
'"'
)
s
=
PotomlPropertyValueString
;
else
if
(
c
>=
48
&&
c
<=
57
)
{
ci
--
;
s
=
PotomlPropertyValue
Double
;
s
=
PotomlPropertyValue
Long
;
}
else
if
(
c
!=
' '
&&
c
!=
'\t'
)
return
InvalidCharacter
;
break
;
...
...
@@ -103,19 +103,28 @@ char potoml_toml_parse(struct CommonHashtableTable *hashtable, char *toml) {
value_length
++
;
break
;
case
PotomlPropertyValueLong
:
case
PotomlPropertyValueDouble
:
if
(
c
==
'\n'
||
c
==
' '
||
c
==
'\t'
)
{
double_value
=
malloc
(
sizeof
(
double
));
*
double_value
=
strtod
(
value
,
NULL
);
if
(
s
==
PotomlPropertyValueLong
)
{
casted_value
=
malloc
(
sizeof
(
long
));
*
((
long
*
)
casted_value
)
=
strtol
(
value
,
NULL
,
10
);
}
else
/* if(s == PotomlPropertyValueDouble) */
{
casted_value
=
malloc
(
sizeof
(
double
));
*
((
double
*
)
casted_value
)
=
strtod
(
value
,
NULL
);
}
if
(
!
common_hashtable_insert
(
hashtable
,
property
,
double
_value
))
{
if
(
!
common_hashtable_insert
(
hashtable
,
property
,
casted
_value
))
{
return
HashtableInsertError
;
}
if
(
c
==
'\n'
)
ci
--
;
s
=
PotomlLineEnding
;
break
;
}
else
if
((
c
<
48
||
c
>
57
)
&&
c
!=
'.'
)
return
InvalidCharacter
;
}
else
if
(
c
==
'.'
)
{
if
(
s
==
PotomlPropertyValueLong
)
s
=
PotomlPropertyValueDouble
;
else
return
InvalidCharacter
;
}
else
if
(
c
<
48
||
c
>
57
)
return
InvalidCharacter
;
value
[
value_length
]
=
c
;
value_length
++
;
...
...
@@ -167,10 +176,10 @@ struct PotomlTomlSubset *potoml_toml_create_subset(struct CommonHashtableTable *
char
*
owned_section_name
=
malloc
((
section_name_length
+
2
)
*
sizeof
(
char
));
if
(
!
owned_section_name
)
return
NULL
;
// Append dot to section name
strcpy
(
owned_section_name
,
section_name
);
strcat
(
owned_section_name
,
"."
);
// Append dot to section name if non-empty
if
(
section_name_length
!=
0
)
strcat
(
owned_section_name
,
"."
);
subset
=
(
struct
PotomlTomlSubset
*
)
malloc
(
sizeof
(
struct
PotomlTomlSubset
));
...
...
@@ -228,6 +237,30 @@ double potoml_toml_get_double(struct PotomlTomlSubset *subset, const char *prope
return
value
;
}
long
potoml_toml_get_long
(
struct
PotomlTomlSubset
*
subset
,
const
char
*
property
)
{
struct
CommonHashtableElement
*
el
;
size_t
property_length
;
long
value
;
char
*
full_property
;
value
=
0
;
property_length
=
strlen
(
property
);
full_property
=
(
char
*
)
malloc
(
property_length
+
subset
->
section_name_length
+
1
);
strcpy
(
full_property
,
subset
->
section_name
);
strcat
(
full_property
,
property
);
el
=
common_hashtable_find
(
subset
->
hashtable
,
full_property
);
if
(
el
)
{
// Clone instead of giving reference for long
value
=
*
((
long
*
)
el
->
content
);
}
free
(
full_property
);
return
value
;
}
void
potoml_toml_subset_free
(
struct
PotomlTomlSubset
*
subset
)
{
free
((
void
*
)
subset
->
section_name
);
free
((
void
*
)
subset
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment