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.

sjisstat.c 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "sjis.h"
  5. struct charstat {
  6. int chr, cnt;
  7. };
  8. struct charstat stats[65536];
  9. void push_charstat(int chr) {
  10. chr &= 0xffff;
  11. stats[chr].chr = chr;
  12. stats[chr].cnt++;
  13. }
  14. int cmpstatfn(const void *p1, const void *p2) {
  15. return ((struct charstat *)p2)->cnt - ((struct charstat *)p1)->cnt;
  16. }
  17. int main(int argc, char **argv) {
  18. FILE *in = stdin;
  19. uint8_t buf[1024];
  20. int last_byte = 0;
  21. int l;
  22. while((l = fread(buf, 1, 1024, in)) > 0) {
  23. for(int i = 0; i < l; i++) {
  24. uint8_t b = buf[i];
  25. if(last_byte == 0 && SJIS_FIRST_CHAR(b)) {
  26. last_byte = b;
  27. } else {
  28. push_charstat((last_byte << 8) | b);
  29. last_byte = 0;
  30. }
  31. }
  32. }
  33. qsort(stats, sizeof(stats) / sizeof(stats[0]), sizeof(stats[0]), cmpstatfn);
  34. for(int i = 0; i < 65536; i++) {
  35. if(stats[i].cnt == 0) break;
  36. int chr = stats[i].chr;
  37. char utfbuf[5];
  38. if(chr < 0x20) {
  39. snprintf(utfbuf, 5, "0x%02x", chr);
  40. } else {
  41. int l = utf8_encode(sjis_char_to_unicode(chr), utfbuf);
  42. utfbuf[l] = 0;
  43. }
  44. if(chr > 0xff)
  45. printf("0x%02x 0x%02x %d %s\n", chr >> 8, chr & 0xff, stats[i].cnt, utfbuf);
  46. else
  47. printf(" 0x%02x %d %s\n", chr & 0xff, stats[i].cnt, utfbuf);
  48. }
  49. return 0;
  50. }