diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/luma/arghandl.cc | 12 | ||||
-rw-r--r-- | src/luma/luma.cc | 37 | ||||
-rw-r--r-- | src/luma/print.c | 16 | ||||
-rw-r--r-- | src/luma/utf8dec.c | 8 | ||||
-rw-r--r-- | src/luma/utf8enc.c | 61 | ||||
-rw-r--r-- | src/main.c | 32 | ||||
-rw-r--r-- | src/main.cc | 4 |
7 files changed, 117 insertions, 53 deletions
diff --git a/src/luma/arghandl.cc b/src/luma/arghandl.cc deleted file mode 100644 index f9f08c0..0000000 --- a/src/luma/arghandl.cc +++ /dev/null @@ -1,12 +0,0 @@ -# include <cstdio> -# include <luma.hh> -# include <string> -using namespace std::literals::string_literals; -void luma::arghandl(int const argc,char const * * argv) { - for(int pos = 0x1;pos < argc; ++pos) { - std::string arg = argv[pos]; - if(pos == 0x1) { - this->lumafile = arg; - } - } -} diff --git a/src/luma/luma.cc b/src/luma/luma.cc deleted file mode 100644 index 5b72788..0000000 --- a/src/luma/luma.cc +++ /dev/null @@ -1,37 +0,0 @@ -# include <cstdio> -# include <cstdlib> -# include <luma.hh> -# include <fcntl.h> -# include <sys/stat.h> -# include <unistd.h> -[[noreturn]] luma::luma(int const argc,char const * * argv) { - this->arghandl(argc,argv); - std::printf("Will open \"%s\".\u000A",this->lumafile.c_str()); - int file = ::open(this->lumafile.c_str(),O_RDONLY); - char tok = '\0'; - char word[0x1000]; - int wordpos = 0x0; - struct ::stat fst; - ::fstat(file,&fst); - printf("Size of file is %ld characters.\u000A",fst.st_size); - ::sleep(0x1); - for(int filepos = 0x0;filepos < fst.st_size;++filepos) { - if(::read(file,&tok,0x1) < 0x0) { - std::printf("Error reading file.\u000A"); - ::_exit(EXIT_FAILURE); - } - if(tok == '\u000A' || tok == '\u0020') { - if(wordpos == 0x0) continue; - word[wordpos] = '\0'; - std::printf("The word is \"%s\".\u000A",word); - word[0x0] = '\0'; - wordpos = 0x0; - } - else { - word[wordpos] = tok; - std::printf("Got character '%c' and set word[%d] to it.\u000A",tok,wordpos); - ++wordpos; - } - } - ::_exit(EXIT_SUCCESS); -} diff --git a/src/luma/print.c b/src/luma/print.c new file mode 100644 index 0000000..df53d8a --- /dev/null +++ b/src/luma/print.c @@ -0,0 +1,16 @@ +# include <luma/utf8enc.h> +# include <stdio.h> +# include <string.h> +void luma_print([[maybe_unused]] char * str,...) { + /*for(size_t n = 0x0;;++n) { + if(str[n] == 0x0) { + fwrite(&(char){0xA},0x1,0x1,stdout); + break; + } + if(!strcmp(&str[n],"\uFFFD")) { + fwrite(&(char){0x20},0x1,0x1,stdout); + continue; + } + fwrite(&str[n],0x1,0x1,stdout); + }*/ +} diff --git a/src/luma/utf8dec.c b/src/luma/utf8dec.c new file mode 100644 index 0000000..35ef07e --- /dev/null +++ b/src/luma/utf8dec.c @@ -0,0 +1,8 @@ +# include <luma/utf8dec.h> +# include <stdint.h> +# include <stdlib.h> +uint32_t * luma_utf8dec([[maybe_unused]] char const * str) { + uint32_t * utf = malloc(0x4); + utf[0x0] = (uint32_t){0x0}; + return utf; +} diff --git a/src/luma/utf8enc.c b/src/luma/utf8enc.c new file mode 100644 index 0000000..cd1edff --- /dev/null +++ b/src/luma/utf8enc.c @@ -0,0 +1,61 @@ +# include <luma/utf8enc.h> +# include <stdint.h> +# include <stdio.h> +# include <stdlib.h> +uint8_t const * luma_utf8enc(uint32_t * codeps) { + size_t sz = (size_t){0x0}; // Size of input array (bytes). + size_t outsz = (size_t){0x0}; // Size of output array /bytes). + for(size_t n = (size_t){0x0};;n += (size_t){0x1}) { // First pass: get size of input array, and determine size of output array. + uint32_t codep = codeps[n]; // Current Unicode codepoint. + if(codep == (uint32_t){0x0}) { // U+0000 is Null. + sz = n; + break; + } + if(codep > 0x10FFFF) { // Codepoint out of range. + return NULL; + } + if(codep > 0xFFFF) { // 4 bytes. + outsz += (size_t){0x2}; + continue; + } + if(codep > 0x7FF) { // 3 bytes. + outsz += (size_t){0x3}; + continue; + } + if(codep > 0x7F) { // 2 bytes. + outsz += (size_t){0x2}; + continue; + } + // 1 byte. + outsz += (size_t){0x1}; + } + outsz += (size_t){0x1}; // Add space for null-terminator. + printf("There are %zu element(s).\n",sz); + printf("The output will have %zu element(s).\n",outsz); + uint8_t * outstr = malloc(outsz); // Allocate space for output array. + outstr[outsz - (size_t){0x1}] = (uint8_t){0x0}; // Create null-terminator on output array. + size_t outn = (size_t){0x0}; // Keep track of position in output array. + for(size_t n = (size_t){0x0};n < sz;n += (size_t){0x1}) { + uint32_t codep = codeps[n]; // Current Unicode codepoint. + if(codep > 0xFFFF) { + outstr[outn] = (uint8_t){0x3F}; + outn += (size_t){0x1}; + continue; + } + if(codep > 0x7FF) { + outstr[outn] = (uint8_t){0x3F}; + outn += (size_t){0x1}; + continue; + } + if(codep > 0x7F) { + outstr[outn] = (uint8_t){0xC0 + (codep >> 0x6)}; + outn += (size_t){0x1}; + outstr[outn] = (uint8_t){0x80 + ((uint8_t){codep << 0x2} >> 0x2)}; + outn += (size_t){0x1}; + continue; + } + outstr[outn] = codep; + outn += (size_t){0x1}; + } + return (uint8_t const *){outstr}; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..8f55eec --- /dev/null +++ b/src/main.c @@ -0,0 +1,32 @@ +# include <locale.h> +# include <luma/arch.h> +# include <luma/print.h> +# include <luma/utf8enc.h> +# include <stdint.h> +# include <stdio.h> +# include <stdlib.h> +int main(void) { + setlocale(LC_ALL,"en_GB.UTF-8"); + enum luma_arch code[] = { + luma_arch_lab, + luma_arch_hello, + }; + for(size_t i = (size_t){0x0};i < sizeof code / sizeof code[0x0];++i) { + printf("Got code %d.\n",code[i]); + } + uint8_t const * msg = luma_utf8enc((uint32_t[]){0x00A2,0x2C,0x20Ac,0x2C,0x218A,0x2C,0x1F44B,0x0}); + printf("%u\n",msg[0x0]); + printf("%u\n",msg[0x1]); + printf("%s\n",msg); + //uint32_t * utf = luma_utf8dec(msg); + free((void *)msg); + /*for(size_t n = (size_t){0x0};;n += (size_t){0x1}) { + if(utf[n] == (uint32_t){0x0}) { + break; + } + printf("%d\n",utf[n]); + } + free((void *)utf);*/ + //luma_print("Hello world. �👋"); + exit(EXIT_SUCCESS); +} diff --git a/src/main.cc b/src/main.cc deleted file mode 100644 index 5de90c4..0000000 --- a/src/main.cc +++ /dev/null @@ -1,4 +0,0 @@ -# include <luma.hh> -int main(int const argc,char const * * argv) { - ::luma luma(argc,argv); -} |