Human68k CUI emulator with sound.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utf8.c 676B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "utf8.h"
  2. int utf8_encode(int c, char *buf) {
  3. if(c < 0x80) {
  4. buf[0] = c;
  5. return 1;
  6. } else if(c < 0x800) {
  7. buf[0] = 192 + c / 64;
  8. buf[1] = 128 + c % 64;
  9. return 2;
  10. } else if(c - 0xd800u < 0x800) {
  11. } else if(c < 0x10000) {
  12. buf[0] = 224 + c / 4096;
  13. buf[1] = 128 + c / 64 % 64;
  14. buf[2] = 128 + c % 64;
  15. return 3;
  16. } else if (c<0x110000) {
  17. buf[0] = 240+c/262144;
  18. buf[1] = 128+c/4096%64;
  19. buf[2] = 128+c/64%64;
  20. buf[3] = 128+c%64;
  21. return 4;
  22. }
  23. buf[0] = c;
  24. return 1;
  25. }
  26. int utf8_fputc(int c, FILE *f) {
  27. char buf[4];
  28. int chars = utf8_encode(c, buf);
  29. if(chars) {
  30. if(fwrite(buf, chars, 1, f) < 1)
  31. return EOF;
  32. return c;
  33. }
  34. return EOF;
  35. }