diff --git a/.gitignore b/.gitignore index 5761abc..a5515b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.o +gen_ft8 diff --git a/Makefile b/Makefile index 54a18a6..d8b0936 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CXXFLAGS = -std=c++14 LDFLAGS = -lm -gen_ft8: gen_ft8.o encode.o pack.o +gen_ft8: gen_ft8.o encode.o pack.o text.o $(CXX) $(LDFLAGS) -o $@ $^ diff --git a/pack.cpp b/pack.cpp index 9d04482..8c9a876 100644 --- a/pack.cpp +++ b/pack.cpp @@ -2,57 +2,11 @@ #include #include "pack.h" - +#include "text.h" constexpr int32_t NBASE = 37*36*10*27*27*27L; constexpr int32_t NGBASE = 180*180L; - -// Utility functions for characters and strings - -char to_upper(char c) { - return (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c; -} - -bool is_digit(char c) { - return (c >= '0') && (c <= '9'); -} - -bool is_letter(char c) { - return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')); -} - -bool is_space(char c) { - return (c == ' '); -} - -bool starts_with(const char *string, const char *prefix) { - return 0 == memcmp(string, prefix, strlen(prefix)); -} - -bool equals(const char *string1, const char *string2) { - return 0 == strcmp(string1, string2); -} - - -// Text message formatting: -// - replaces lowercase letters with uppercase -// - merges consecutive spaces into single space -void fmtmsg(char *msg_out, const char *msg_in) { - char c; - char last_out = 0; - while ( (c = *msg_in) ) { - if (c != ' ' || last_out != ' ') { - last_out = to_upper(c); - *msg_out = last_out; - ++msg_out; - } - ++msg_in; - } - *msg_out = 0; // Add zero termination -} - - // Returns integer encoding of a character (number/digit/space). // Alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ +-./?" // - Digits are encoded as 0..9 diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..4e0d10d --- /dev/null +++ b/text.cpp @@ -0,0 +1,51 @@ +#include "text.h" + +#include + +// Utility functions for characters and strings + +char to_upper(char c) { + return (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c; +} + +bool is_digit(char c) { + return (c >= '0') && (c <= '9'); +} + +bool is_letter(char c) { + return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')); +} + +bool is_space(char c) { + return (c == ' '); +} + +bool in_range(char c, char min, char max) { + return (c >= min) && (c <= max); +} + +bool starts_with(const char *string, const char *prefix) { + return 0 == memcmp(string, prefix, strlen(prefix)); +} + +bool equals(const char *string1, const char *string2) { + return 0 == strcmp(string1, string2); +} + + +// Text message formatting: +// - replaces lowercase letters with uppercase +// - merges consecutive spaces into single space +void fmtmsg(char *msg_out, const char *msg_in) { + char c; + char last_out = 0; + while ( (c = *msg_in) ) { + if (c != ' ' || last_out != ' ') { + last_out = to_upper(c); + *msg_out = last_out; + ++msg_out; + } + ++msg_in; + } + *msg_out = 0; // Add zero termination +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..0d904d4 --- /dev/null +++ b/text.h @@ -0,0 +1,14 @@ +#pragma once + +char to_upper(char c); +bool is_digit(char c); +bool is_letter(char c); +bool is_space(char c); +bool in_range(char c, char min, char max); +bool starts_with(const char *string, const char *prefix); +bool equals(const char *string1, const char *string2); + +// Text message formatting: +// - replaces lowercase letters with uppercase +// - merges consecutive spaces into single space +void fmtmsg(char *msg_out, const char *msg_in);