Skip to content

Commit

Permalink
Update examples' compatibility with MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Apr 22, 2024
1 parent 11ad197 commit d5a63eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/cheap_grep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <memory>
#include "../lite_string.h"

/// A lambda function to clean up a lite_string object.
/// A lambda function to free a lite_string object.
auto string_deleter = [](lite_string *ls) -> void { string_free(ls); };

/**
Expand Down
32 changes: 27 additions & 5 deletions examples/word_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
#include <ctype.h>
#include "../lite_string.h"

#ifndef S_ISREG // Not defined in Windows
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif

// A simple program that reads a file and prints the number of words and characters in the file.
int main(const int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}

lite_string *s = string_new();
string_append_cstr(s, argv[1]);
// Store the filename in a new lite_string object.
lite_string *s = string_new_cstr(argv[1]);

// Get the file properties.
struct stat st = {};
struct stat st = {0};
if (stat(string_cstr(s), &st) != 0) {
perror("stat");
string_free(s);
Expand All @@ -40,14 +43,28 @@ int main(const int argc, char *argv[]) {
return 1;
}
// Open the file
FILE *file = fopen(string_cstr(s), "r");
#if _MSC_VER
// MSVC deprecates fopen, use fopen_s instead.
FILE *file;
errno_t err = fopen_s(&file, string_cstr(s), "rb");
if (err != 0 || file == nullptr) {
#else
FILE *file = fopen(string_cstr(s), "rb");
if (file == nullptr) {
#endif
perror("Could not open file");
string_free(s);
return 1;
}
// Read the file into a buffer.
#if _MSC_VER
// MSVC does not support variable length arrays.

#include <stdlib.h>
char *buffer = (char *) malloc(file_size + 1);
#else
char buffer[file_size + 1];
#endif
if (fread(buffer, sizeof(char), file_size, file) != file_size) {
fputs("Failed to read the file.\n", stderr);
return 1;
Expand All @@ -60,6 +77,10 @@ int main(const int argc, char *argv[]) {
string_clear(s);
string_append_cstr(s, buffer);

#if _MSC_VER
free(buffer);
#endif

// Initializations
size_t word_count = 0;
size_t char_count = 0;
Expand All @@ -78,6 +99,7 @@ int main(const int argc, char *argv[]) {
}
}

// Print the statistics.
if (word_count && char_count) {
printf("Word count: %zu\n", word_count);
printf("Character count: %zu\n", char_count);
Expand Down

0 comments on commit d5a63eb

Please sign in to comment.