University stuff.
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.

oppgave.c 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef unsigned char byte;
  4. typedef unsigned long unicode;
  5. // The minimum value of the first `char` to indicate n bytes
  6. #define C4bytes 0xF0
  7. #define C3bytes 0xE0
  8. #define C2bytes 0xC0
  9. // The minimum value for the vm_var_char for the utf8 equivalent to be n bytes
  10. #define U4bytes 0x10000
  11. #define U3bytes 0x0800
  12. #define U2bytes 0x0080
  13. void writebyte(FILE *f, byte b)
  14. {
  15. fwrite(&b, 1, 1, f);
  16. }
  17. void writeutf8char(FILE *f, unicode u)
  18. {
  19. if (u >= U4bytes)
  20. {
  21. writebyte(f, 0b11110000 | ((u >> 18) & 0b00000111));
  22. writebyte(f, 0b10000000 | ((u >> 12) & 0b00111111));
  23. writebyte(f, 0b10000000 | ((u >> 6) & 0b00111111));
  24. writebyte(f, 0b10000000 | (u & 0b00111111));
  25. }
  26. else if (u >= U3bytes)
  27. {
  28. writebyte(f, 0b11100000 | ((u >> 12) & 0b00001111));
  29. writebyte(f, 0b10000000 | ((u >> 6) & 0b00111111));
  30. writebyte(f, 0b10000000 | (u & 0b00111111));
  31. }
  32. else if (u >= U2bytes)
  33. {
  34. writebyte(f, 0b11000000 | ((u >> 6) & 0b00011111));
  35. writebyte(f, 0b10000000 | (u & 0b00111111));
  36. }
  37. else
  38. {
  39. writebyte(f, u);
  40. }
  41. }
  42. int readbyte(FILE *f)
  43. {
  44. int status;
  45. byte c;
  46. status = fread(&c, 1, 1, f);
  47. if (status <= 0) return -1;
  48. return (int)c;
  49. }
  50. long readutf8char(FILE *f)
  51. {
  52. byte first = readbyte(f);
  53. unicode u = 0;
  54. if (first >= C4bytes)
  55. {
  56. u |= (first & 0b00000111) << 18;
  57. u |= (readbyte(f) & 0b00111111) << 12;
  58. u |= (readbyte(f) & 0b00111111) << 6;
  59. u |= readbyte(f) & 0b00111111;
  60. }
  61. else if (first >= C3bytes)
  62. {
  63. u |= (first & 0b00001111) << 12;
  64. u |= (readbyte(f) & 0b00111111) << 6;
  65. u |= readbyte(f) & 0b00111111;
  66. }
  67. else if (first >= C2bytes)
  68. {
  69. u |= (first & 0b00011111) << 6;
  70. u |= readbyte(f) & 0b00111111;
  71. }
  72. else
  73. {
  74. u |= first;
  75. }
  76. return u;
  77. }