Initial commit
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
/* - | Copyright | ------------------------------------------------------------
|
||||
Copyright (c) 2026 Randy Jordan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
* --------------------------------------------------------------------------*/
|
||||
#include "spp.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static char*
|
||||
read_file_into_mem(const char* path)
|
||||
{
|
||||
FILE* file = fopen(path, "rb");
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, "Could not open file \"%s\".\n", path);
|
||||
exit(74);
|
||||
}
|
||||
|
||||
fseek(file, 0L, SEEK_END);
|
||||
size_t fileSize = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
char* buffer = (char*)malloc(fileSize + 1);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
|
||||
exit(74);
|
||||
}
|
||||
|
||||
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
|
||||
if (bytesRead < fileSize) {
|
||||
fprintf(stderr, "Could not read file \"%s\".\n", path);
|
||||
exit(74);
|
||||
}
|
||||
|
||||
buffer[bytesRead] = '\0';
|
||||
fclose(file);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static size_t
|
||||
line_count(const struct SPP_Stream s, const char* path)
|
||||
{
|
||||
|
||||
size_t lines = 0;
|
||||
while (!spp_eof(s)) {
|
||||
if (spp_is(s, "\n")) {
|
||||
lines++;
|
||||
printf("%s - Line Count: %lld\r", path, lines);
|
||||
fflush(stdout);
|
||||
(*s.cursor)++;
|
||||
|
||||
} else {
|
||||
(*s.cursor)++;
|
||||
}
|
||||
}
|
||||
printf("%s - Line Count: %lld\n", path, lines);
|
||||
fflush(stdout);
|
||||
return lines;
|
||||
}
|
||||
|
||||
bool
|
||||
spp_is(const struct SPP_Stream s, const char* list)
|
||||
{
|
||||
assert(list != NULL);
|
||||
assert(s.cursor != NULL && *s.cursor != NULL);
|
||||
|
||||
return strchr(list, **s.cursor);
|
||||
}
|
||||
|
||||
bool
|
||||
spp_eof(const struct SPP_Stream s)
|
||||
{
|
||||
assert(s.cursor != NULL && *s.cursor != NULL);
|
||||
|
||||
return **s.cursor == 0;
|
||||
}
|
||||
|
||||
char
|
||||
spp_take(const struct SPP_Stream s, const char* list)
|
||||
{
|
||||
if (!spp_is(s, list))
|
||||
return 0;
|
||||
|
||||
const char r = **s.cursor;
|
||||
(*s.cursor)++;
|
||||
return r;
|
||||
}
|
||||
|
||||
size_t
|
||||
spp_skip(const struct SPP_Stream s, const char* list)
|
||||
{
|
||||
size_t size = 0;
|
||||
for (; !spp_eof(s) && spp_is(s, list); size++) {
|
||||
(*s.cursor)++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t
|
||||
spp_until(const struct SPP_Stream s, const char* list)
|
||||
{
|
||||
size_t size = 0;
|
||||
for (; !spp_eof(s) && !spp_is(s, list); size++) {
|
||||
(*s.cursor)++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
const char*
|
||||
spp_cursor(struct SPP_Stream s)
|
||||
{
|
||||
return *s.cursor;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* path = argv[1];
|
||||
const char* buffer = read_file_into_mem(path);
|
||||
|
||||
struct SPP_Stream stream = { .cursor = &buffer };
|
||||
line_count(stream, path);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user