Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
libloc
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
nexedi
libloc
Commits
3df5f70a
Commit
3df5f70a
authored
Mar 09, 2022
by
Michael Tremer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
writer: Use country list internally
Signed-off-by:
Michael Tremer
<
michael.tremer@ipfire.org
>
parent
7db88f1b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
36 deletions
+31
-36
src/country-list.c
src/country-list.c
+9
-0
src/libloc.sym
src/libloc.sym
+1
-0
src/libloc/country-list.h
src/libloc/country-list.h
+2
-0
src/writer.c
src/writer.c
+19
-36
No files found.
src/country-list.c
View file @
3df5f70a
...
...
@@ -168,3 +168,12 @@ LOC_EXPORT int loc_country_list_contains_code(
return
r
;
}
static
int
__loc_country_cmp
(
const
void
*
country1
,
const
void
*
country2
)
{
return
loc_country_cmp
(
*
(
struct
loc_country
**
)
country1
,
*
(
struct
loc_country
**
)
country2
);
}
LOC_EXPORT
void
loc_country_list_sort
(
struct
loc_country_list
*
list
)
{
// Sort everything
qsort
(
list
->
elements
,
list
->
size
,
sizeof
(
*
list
->
elements
),
__loc_country_cmp
);
}
src/libloc.sym
View file @
3df5f70a
...
...
@@ -53,6 +53,7 @@ global:
loc_country_list_new;
loc_country_list_ref;
loc_country_list_size;
loc_country_list_sort;
loc_country_list_unref;
# Database
...
...
src/libloc/country-list.h
View file @
3df5f70a
...
...
@@ -40,4 +40,6 @@ int loc_country_list_contains(
int
loc_country_list_contains_code
(
struct
loc_country_list
*
list
,
const
char
*
code
);
void
loc_country_list_sort
(
struct
loc_country_list
*
list
);
#endif
src/writer.c
View file @
3df5f70a
...
...
@@ -36,6 +36,7 @@
#include <libloc/as-list.h>
#include <libloc/compat.h>
#include <libloc/country.h>
#include <libloc/country-list.h>
#include <libloc/database.h>
#include <libloc/format.h>
#include <libloc/network.h>
...
...
@@ -61,12 +62,10 @@ struct loc_writer {
char
signature2
[
LOC_SIGNATURE_MAX_LENGTH
];
size_t
signature2_length
;
struct
loc_country
**
countries
;
size_t
countries_count
;
struct
loc_network_tree
*
networks
;
struct
loc_as_list
*
as_list
;
struct
loc_country_list
*
country_list
;
};
static
int
parse_private_key
(
struct
loc_writer
*
writer
,
EVP_PKEY
**
private_key
,
FILE
*
f
)
{
...
...
@@ -158,12 +157,8 @@ static void loc_writer_free(struct loc_writer* writer) {
loc_as_list_unref
(
writer
->
as_list
);
// Unref all countries
if
(
writer
->
countries
)
{
for
(
unsigned
int
i
=
0
;
i
<
writer
->
countries_count
;
i
++
)
{
loc_country_unref
(
writer
->
countries
[
i
]);
}
free
(
writer
->
countries
);
}
if
(
writer
->
country_list
)
loc_country_list_unref
(
writer
->
country_list
);
// Release network tree
if
(
writer
->
networks
)
...
...
@@ -249,30 +244,14 @@ LOC_EXPORT int loc_writer_add_network(struct loc_writer* writer, struct loc_netw
return
loc_network_tree_add_network
(
writer
->
networks
,
*
network
);
}
static
int
__loc_country_cmp
(
const
void
*
country1
,
const
void
*
country2
)
{
return
loc_country_cmp
(
*
(
struct
loc_country
**
)
country1
,
*
(
struct
loc_country
**
)
country2
);
}
LOC_EXPORT
int
loc_writer_add_country
(
struct
loc_writer
*
writer
,
struct
loc_country
**
country
,
const
char
*
country_code
)
{
// Allocate a new country
int
r
=
loc_country_new
(
writer
->
ctx
,
country
,
country_code
);
if
(
r
)
return
r
;
// We have a new country to add
writer
->
countries_count
++
;
// Make space
writer
->
countries
=
realloc
(
writer
->
countries
,
sizeof
(
*
writer
->
countries
)
*
writer
->
countries_count
);
if
(
!
writer
->
countries
)
return
-
ENOMEM
;
// Add as last element
writer
->
countries
[
writer
->
countries_count
-
1
]
=
loc_country_ref
(
*
country
);
// Sort everything
qsort
(
writer
->
countries
,
writer
->
countries_count
,
sizeof
(
*
writer
->
countries
),
__loc_country_cmp
);
return
0
;
// Append it to the list
return
loc_country_list_append
(
writer
->
country_list
,
*
country
);
}
static
void
make_magic
(
struct
loc_writer
*
writer
,
struct
loc_database_magic
*
magic
,
...
...
@@ -524,20 +503,24 @@ static int loc_database_write_countries(struct loc_writer* writer,
DEBUG
(
writer
->
ctx
,
"Countries section starts at %jd bytes
\n
"
,
(
intmax_t
)
*
offset
);
header
->
countries_offset
=
htobe32
(
*
offset
);
size_t
countries_length
=
0
;
const
size_t
countries_count
=
loc_country_list_size
(
writer
->
country_list
);
struct
loc_database_country_v1
block
;
size_t
block_length
=
0
;
for
(
unsigned
int
i
=
0
;
i
<
countries_count
;
i
++
)
{
struct
loc_country
*
country
=
loc_country_list_get
(
writer
->
country_list
,
i
);
struct
loc_database_country_v1
country
;
for
(
unsigned
int
i
=
0
;
i
<
writer
->
countries_count
;
i
++
)
{
// Convert country into database format
loc_country_to_database_v1
(
writer
->
countries
[
i
],
writer
->
pool
,
&
country
);
loc_country_to_database_v1
(
country
,
writer
->
pool
,
&
block
);
// Write to disk
*
offset
+=
fwrite
(
&
country
,
1
,
sizeof
(
country
),
f
);
countries_length
+=
sizeof
(
country
);
*
offset
+=
fwrite
(
&
block
,
1
,
sizeof
(
block
),
f
);
block_length
+=
sizeof
(
block
);
}
DEBUG
(
writer
->
ctx
,
"Countries section has a length of %zu bytes
\n
"
,
countries
_length
);
header
->
countries_length
=
htobe32
(
countries
_length
);
DEBUG
(
writer
->
ctx
,
"Countries section has a length of %zu bytes
\n
"
,
block
_length
);
header
->
countries_length
=
htobe32
(
block
_length
);
align_page_boundary
(
offset
,
f
);
...
...
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