Skip to content

Commit b304aa8

Browse files
remove str_view from source make fetch content (#142)
* remove str_view from source make fetch content * fix release compilation warning
1 parent 4a96336 commit b304aa8

14 files changed

Lines changed: 205 additions & 2383 deletions

File tree

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ if (EXISTS "${PROJECT_SOURCE_DIR}/tests" AND EXISTS "${PROJECT_SOURCE_DIR}/sampl
3535
include(tools/scanners.cmake)
3636
endif()
3737

38+
# If we are in our own repository build environment, not a release, then we
39+
# will bring in the str_view dependency for our samples and testing.
40+
if (EXISTS "${PROJECT_SOURCE_DIR}/tests" OR EXISTS "${PROJECT_SOURCE_DIR}/samples" OR EXISTS "${PROJECT_SOURCE_DIR}/utility")
41+
include(FetchContent)
42+
FetchContent_Declare(
43+
str_view
44+
URL https://github.com/skeletoss/str_view/releases/download/v0.7.0/str_view-v0.7.0.zip
45+
DOWNLOAD_EXTRACT_TIMESTAMP FALSE
46+
)
47+
FetchContent_MakeAvailable(str_view)
48+
target_compile_options(str_view PRIVATE "-w")
49+
endif()
50+
3851
if (NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
3952
message(STATUS "This project has a top-level one called [${CMAKE_PROJECT_NAME}]")
4053
else()

samples/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_executable(maze maze.c)
1111
target_link_libraries(maze PRIVATE
1212
cli
1313
random
14-
string_view
14+
str_view::str_view
1515
ccc
1616
allocate
1717
)
@@ -21,7 +21,7 @@ add_executable(graph graph.c)
2121
target_link_libraries(graph PRIVATE
2222
cli
2323
random
24-
string_view
24+
str_view::str_view
2525
ccc
2626
allocate
2727
)
@@ -30,7 +30,7 @@ add_dependencies(samples graph)
3030
add_executable(words words.c)
3131
target_link_libraries(words PRIVATE
3232
cli
33-
string_view
33+
str_view::str_view
3434
ccc
3535
allocate
3636
string_arena
@@ -40,7 +40,7 @@ add_dependencies(samples words)
4040
add_executable(ccczip ccczip.c)
4141
target_link_libraries(ccczip PRIVATE
4242
cli
43-
string_view
43+
str_view::str_view
4444
ccc
4545
allocate
4646
string_arena

samples/ccczip.c

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ algorithms use a wide range of data structures. */
3333
#include "ccc/flat_priority_queue.h"
3434
#include "ccc/traits.h"
3535
#include "ccc/types.h"
36+
#include "str_view/str_view.h"
3637
#include "utility/allocate.h"
3738
#include "utility/string_arena.h"
38-
#include "utility/string_view/string_view.h"
3939

4040
/*=========================== Type Declarations ==========================*/
4141

@@ -173,16 +173,16 @@ struct Huffman_encoding
173173
/** Files the user wants zipped or unzipped. */
174174
struct Zip_actions
175175
{
176-
SV_String_view zip;
177-
SV_String_view unzip;
176+
SV_Str_view zip;
177+
SV_Str_view unzip;
178178
};
179179

180-
static SV_String_view const output_dir = SV("samples/output/");
181-
static SV_String_view const cccz_suffix = SV(".cccz");
180+
static SV_Str_view const output_dir = SV_from("samples/output/");
181+
static SV_Str_view const cccz_suffix = SV_from(".cccz");
182182

183183
/*=========================== Prototypes ==========================*/
184184

185-
static void zip_file(SV_String_view to_compress);
185+
static void zip_file(SV_Str_view to_compress);
186186
static Flat_priority_queue build_encoding_priority_queue(FILE *f,
187187
struct Huffman_tree *);
188188
static void bitq_push_back(struct Bit_queue *, CCC_Tribool);
@@ -208,7 +208,7 @@ static void print_inner_tree(struct Huffman_tree const *tree, size_t node,
208208
static void print_node(struct Huffman_tree const *tree, size_t node);
209209
static bool is_leaf(struct Huffman_tree const *tree, size_t node);
210210
static void print_bitq(struct Bit_queue const *bq);
211-
static void unzip_file(SV_String_view unzip);
211+
static void unzip_file(SV_Str_view unzip);
212212
static struct Huffman_tree
213213
reconstruct_tree(struct Compressed_huffman_tree *blueprint);
214214
static void reconstruct_text(FILE *f, struct Huffman_tree const *,
@@ -219,10 +219,10 @@ static size_t branch_index(struct Huffman_tree const *t, size_t node,
219219
static size_t parent_index(struct Huffman_tree const *t, size_t node);
220220
static char char_index(struct Huffman_tree const *t, size_t node);
221221
static struct Huffman_node *node_at(struct Huffman_tree const *t, size_t node);
222-
static void write_to_file(SV_String_view original_filepath,
222+
static void write_to_file(SV_Str_view original_filepath,
223223
size_t original_filesize, struct Huffman_encoding *);
224224
static void write_bitq(FILE *cccz, struct Bit_queue *bq);
225-
static struct Huffman_encoding read_from_file(SV_String_view unzip);
225+
static struct Huffman_encoding read_from_file(SV_Str_view unzip);
226226
static size_t readbytes(FILE *f, void *base, size_t to_read);
227227
static size_t writebytes(FILE *f, void const *base, size_t to_write);
228228
static void fill_bitq(FILE *f, struct Bit_queue *bq, size_t expected_bits);
@@ -257,7 +257,7 @@ Do not return early or use goto out of this macro or memory will be leaked. */
257257
ptrdiff_t read = 0; \
258258
while ((read = getline(&linepointer, &len, f)) > 0) \
259259
{ \
260-
SV_String_view const line = {.s = linepointer, .len = read}; \
260+
SV_Str_view const line = {.str = linepointer, .len = read}; \
261261
for (char const *char_iterator_name = SV_begin(line); \
262262
char_iterator_name != SV_end(line); \
263263
char_iterator_name = SV_next(char_iterator_name)) \
@@ -281,32 +281,32 @@ main(int argc, char **argv)
281281
struct Zip_actions todo = {};
282282
for (int arg = 1; arg < argc; ++arg)
283283
{
284-
SV_String_view const sv_arg = SV_sv(argv[arg]);
285-
if (SV_starts_with(sv_arg, SV("-h")))
284+
SV_Str_view const sv_arg = SV_from_terminated(argv[arg]);
285+
if (SV_starts_with(sv_arg, SV_from("-h")))
286286
{
287287
print_help();
288288
return 0;
289289
}
290-
if (SV_starts_with(sv_arg, SV("-c=")))
290+
if (SV_starts_with(sv_arg, SV_from("-c=")))
291291
{
292-
SV_String_view const raw_file = SV_substr(
293-
sv_arg, SV_find(sv_arg, 0, SV("=")) + 1, SV_len(sv_arg));
294-
check(!SV_empty(raw_file));
292+
SV_Str_view const raw_file = SV_substr(
293+
sv_arg, SV_find(sv_arg, 0, SV_from("=")) + 1, SV_len(sv_arg));
294+
check(!SV_is_empty(raw_file));
295295
todo.zip = raw_file;
296296
}
297-
else if (SV_starts_with(sv_arg, SV("-d=")))
297+
else if (SV_starts_with(sv_arg, SV_from("-d=")))
298298
{
299-
SV_String_view const raw_file = SV_substr(
300-
sv_arg, SV_find(sv_arg, 0, SV("=")) + 1, SV_len(sv_arg));
301-
check(!SV_empty(raw_file));
299+
SV_Str_view const raw_file = SV_substr(
300+
sv_arg, SV_find(sv_arg, 0, SV_from("=")) + 1, SV_len(sv_arg));
301+
check(!SV_is_empty(raw_file));
302302
todo.unzip = raw_file;
303303
}
304304
}
305-
if (!SV_empty(todo.zip))
305+
if (!SV_is_empty(todo.zip))
306306
{
307307
zip_file(todo.zip);
308308
}
309-
if (!SV_empty(todo.unzip))
309+
if (!SV_is_empty(todo.unzip))
310310
{
311311
unzip_file(todo.unzip);
312312
}
@@ -318,7 +318,7 @@ main(int argc, char **argv)
318318
/** Zips the requested file via Huffman Encoding into the output directory. The
319319
compressed file has a header that can be used to reconstruct the data. */
320320
void
321-
zip_file(SV_String_view const to_compress)
321+
zip_file(SV_Str_view const to_compress)
322322
{
323323
FILE *const f = fopen(SV_begin(to_compress), "r");
324324
check(f, printf("%s", strerror(errno)););
@@ -593,27 +593,26 @@ compress_tree(struct Huffman_tree *const tree)
593593
/** Writes all encoded information to a file with the help of a header for
594594
later file reconstruction. */
595595
static void
596-
write_to_file(SV_String_view const original_filepath,
596+
write_to_file(SV_Str_view const original_filepath,
597597
size_t const original_filesize,
598598
struct Huffman_encoding *const header)
599599
{
600600
/* We write all new files to output directory so create the new path. */
601601
char path_to_cccz[FILESYS_MAX_PATH];
602-
size_t const dir_delim
603-
= SV_rfind(original_filepath, SV_len(original_filepath), SV("/"));
604-
SV_String_view const raw_file
605-
= dir_delim == SV_npos(original_filepath)
606-
? original_filepath
607-
: SV_substr(original_filepath, dir_delim + 1,
608-
SV_len(original_filepath));
602+
size_t const dir_delim = SV_reverse_find(
603+
original_filepath, SV_len(original_filepath), SV_from("/"));
604+
SV_Str_view const raw_file = dir_delim == SV_npos(original_filepath)
605+
? original_filepath
606+
: SV_substr(original_filepath, dir_delim + 1,
607+
SV_len(original_filepath));
609608
size_t const total_bytes
610-
= SV_size(output_dir) + SV_size(raw_file) + SV_size(cccz_suffix);
609+
= SV_bytes(output_dir) + SV_bytes(raw_file) + SV_bytes(cccz_suffix);
611610
check(total_bytes < FILESYS_MAX_PATH);
612611
size_t const path_bytes
613-
= SV_fill(SV_size(output_dir), path_to_cccz, output_dir);
614-
size_t const file_bytes
615-
= SV_fill(SV_size(raw_file), path_to_cccz + (path_bytes - 1), raw_file);
616-
(void)SV_fill(SV_size(cccz_suffix),
612+
= SV_fill(SV_bytes(output_dir), path_to_cccz, output_dir);
613+
size_t const file_bytes = SV_fill(
614+
SV_bytes(raw_file), path_to_cccz + (path_bytes - 1), raw_file);
615+
(void)SV_fill(SV_bytes(cccz_suffix),
617616
path_to_cccz + (path_bytes - 1) + (file_bytes - 1),
618617
cccz_suffix);
619618

@@ -703,7 +702,7 @@ writebytes(FILE *const f, void const *const base, size_t const to_write)
703702
is reconstructed and a copy of the original text is written to the output
704703
directory as a new file with the same name. */
705704
static void
706-
unzip_file(SV_String_view unzip)
705+
unzip_file(SV_Str_view unzip)
707706
{
708707
/* First we verify the compressed file is correct before creating new. */
709708
struct Huffman_encoding he = read_from_file(unzip);
@@ -714,15 +713,16 @@ unzip_file(SV_String_view unzip)
714713
CCC_Tribool has_suf = SV_ends_with(unzip, cccz_suffix);
715714
check(has_suf);
716715
unzip = SV_remove_suffix(unzip, SV_len(cccz_suffix));
717-
size_t const dir_delim = SV_rfind(unzip, SV_len(unzip), SV("/"));
718-
SV_String_view const raw_file
716+
size_t const dir_delim
717+
= SV_reverse_find(unzip, SV_len(unzip), SV_from("/"));
718+
SV_Str_view const raw_file
719719
= dir_delim == SV_npos(unzip)
720720
? unzip
721721
: SV_substr(unzip, dir_delim + 1, SV_len(unzip));
722-
size_t const total_bytes = SV_size(output_dir) + SV_size(raw_file);
722+
size_t const total_bytes = SV_bytes(output_dir) + SV_bytes(raw_file);
723723
check(total_bytes < FILESYS_MAX_PATH);
724-
size_t const prefix = SV_fill(SV_size(output_dir), path, output_dir);
725-
(void)SV_fill(SV_size(raw_file), path + (prefix - 1), raw_file);
724+
size_t const prefix = SV_fill(SV_bytes(output_dir), path, output_dir);
725+
(void)SV_fill(SV_bytes(raw_file), path + (prefix - 1), raw_file);
726726

727727
/* Checks are good and path is set this will be a fresh copy. */
728728
FILE *const copy_of_original = fopen(path, "w");
@@ -744,9 +744,9 @@ unzip_file(SV_String_view unzip)
744744
file. Once complete this function returns all information needed to reconstruct
745745
the tree and write out a copy of the original file to the output directory. */
746746
static struct Huffman_encoding
747-
read_from_file(SV_String_view const unzip)
747+
read_from_file(SV_Str_view const unzip)
748748
{
749-
CCC_Tribool has_suffix = SV_ends_with(unzip, SV(".cccz"));
749+
CCC_Tribool has_suffix = SV_ends_with(unzip, SV_from(".cccz"));
750750
check(has_suffix);
751751
FILE *const cccz = fopen(SV_begin(unzip), "r");
752752
check(cccz, (void)fprintf(stderr, "%s", strerror(errno)););

0 commit comments

Comments
 (0)