@@ -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. */
174174struct 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 );
186186static Flat_priority_queue build_encoding_priority_queue (FILE * f ,
187187 struct Huffman_tree * );
188188static 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,
208208static void print_node (struct Huffman_tree const * tree , size_t node );
209209static bool is_leaf (struct Huffman_tree const * tree , size_t node );
210210static 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 );
212212static struct Huffman_tree
213213reconstruct_tree (struct Compressed_huffman_tree * blueprint );
214214static 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,
219219static size_t parent_index (struct Huffman_tree const * t , size_t node );
220220static char char_index (struct Huffman_tree const * t , size_t node );
221221static 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 * );
224224static 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 );
226226static size_t readbytes (FILE * f , void * base , size_t to_read );
227227static size_t writebytes (FILE * f , void const * base , size_t to_write );
228228static 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
319319compressed file has a header that can be used to reconstruct the data. */
320320void
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
594594later file reconstruction. */
595595static 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)
703702is reconstructed and a copy of the original text is written to the output
704703directory as a new file with the same name. */
705704static 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)
744744file. Once complete this function returns all information needed to reconstruct
745745the tree and write out a copy of the original file to the output directory. */
746746static 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