Skip to content

Commit

Permalink
Refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Apr 12, 2024
1 parent 34b10d3 commit 460ad82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
38 changes: 19 additions & 19 deletions examples/cheap_grep.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <iostream>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <memory>
Expand All @@ -18,11 +17,11 @@ auto string_deleter = [](lite_string *ls) -> void { string_free(ls); };
* @param ignoreCase Whether to ignore the case of the characters.
* @return 0 if the pattern is found, 1 otherwise.
*/
int cheap_grep(const lite_string *pattern, std::istream &input, bool ignoreCase) {
int cheap_grep(const lite_string *pattern, std::istream &input, const bool ignoreCase) {
// Unique pointers to manage lite_string objects.
std::unique_ptr<lite_string, decltype(string_deleter)> s(string_new(), string_deleter);
std::unique_ptr<lite_string, decltype(string_deleter)> patternCopy(string_new(), string_deleter);
std::unique_ptr<lite_string, decltype(string_deleter)> lineCopy(string_new(), string_deleter);
const std::unique_ptr<lite_string, decltype(string_deleter)> s(string_new(), string_deleter);
const std::unique_ptr<lite_string, decltype(string_deleter)> patternCopy(string_new(), string_deleter);
const std::unique_ptr<lite_string, decltype(string_deleter)> lineCopy(string_new(), string_deleter);

string_copy(pattern, patternCopy.get());
if (ignoreCase) string_to_lower(patternCopy.get());
Expand All @@ -36,7 +35,7 @@ int cheap_grep(const lite_string *pattern, std::istream &input, bool ignoreCase)

if (ignoreCase) string_to_lower(lineCopy.get());

if (string_find(lineCopy.get(), patternCopy.get()) != SIZE_MAX) {
if (string_find(lineCopy.get(), patternCopy.get()) != lite_string_npos) {
ret = 0;
std::cout << line << '\n';
}
Expand All @@ -47,35 +46,36 @@ int cheap_grep(const lite_string *pattern, std::istream &input, bool ignoreCase)
return ret;
}

int main(int argc, char *argv[]) {
int main(const int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " [-i] <pattern> <filename>\n";
return 1;
}

bool ignoreCase = false;
int argIndex = 1;
std::unique_ptr<lite_string, decltype(string_deleter)> firstArg(string_new_cstr(argv[1]), string_deleter);
const std::unique_ptr<lite_string, decltype(string_deleter)> firstArg(string_new_cstr(argv[1]), string_deleter);

if (argc > 3 && string_compare_cstr(firstArg.get(), "-i")) {
ignoreCase = true;
argIndex = 2;
}

// Create smart pointers to manage the lifetime of the lite_string objects.
std::unique_ptr<lite_string, decltype(string_deleter)> pattern(string_new_cstr(argv[argIndex]), string_deleter);
std::unique_ptr<lite_string, decltype(string_deleter)> filename(string_new_cstr(argv[argIndex + 1]),
string_deleter);
const std::unique_ptr<lite_string, decltype(string_deleter)> pattern(
string_new_cstr(argv[argIndex]), string_deleter);
const std::unique_ptr<lite_string, decltype(string_deleter)> filename(
string_new_cstr(argv[argIndex + 1]), string_deleter);

// If the filename is "-", read from stdin.
if (string_compare_cstr(filename.get(), "-")) {
if (string_compare_cstr(filename.get(), "-"))
return cheap_grep(pattern.get(), std::cin, ignoreCase);
} else {
std::ifstream file(string_cstr(filename.get()));
if (!file.is_open()) {
std::cerr << "Error: Unable to open file: " << std::quoted(string_cstr(filename.get())) << std::endl;
return 1;
}
return cheap_grep(pattern.get(), file, ignoreCase);

// Open the file and read from it.
std::ifstream file(string_cstr(filename.get()));
if (!file.is_open()) {
std::cerr << "Error: Unable to open file: " << std::quoted(string_cstr(filename.get())) << std::endl;
return 1;
}
return cheap_grep(pattern.get(), file, ignoreCase);
}
8 changes: 3 additions & 5 deletions examples/word_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../lite_string.h"

// A simple program that reads a file and prints the number of words and characters in the file.
int main(int argc, char *argv[]) {
int main(const int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
Expand All @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) {
return 1;
}
// If the file size is greater than 1 MB, return an error.
size_t file_size = st.st_size;
const size_t file_size = st.st_size;
if (file_size > 1 << 20) {
fputs("Error: File size is too large.\n", stderr);
string_free(s);
Expand Down Expand Up @@ -76,11 +76,9 @@ int main(int argc, char *argv[]) {
}
}

double avg_word_length = (double) char_count / word_count;

printf("Word count: %zu\n", word_count);
printf("Character count: %zu\n", char_count);
printf("Average word length: %.2f\n", avg_word_length);
printf("Average word length: %.2f\n", (double) char_count / word_count);

string_free(s);
return 0;
Expand Down

0 comments on commit 460ad82

Please sign in to comment.