ref: 6775c0a9a046673bb6e472bd286271725013968a
parent: 920367d75626ca3077dc881dcba8c19530bbab03
author: Ethan Long <[email protected]>
date: Mon Jun 28 01:27:59 EDT 2021
Cleaned up the code, added comments, fixed ん handling.
--- a/src/test.c
+++ b/src/test.c
@@ -1,4 +1,5 @@
#include <u.h>
+#include <stdio.h>
#include <libc.h>
#include <String.h>
@@ -43,5 +44,18 @@
print("%C\n", (Rune)(12353 + i));
}
+ print("%d , %d\n", (1 == 1), (1 != 1));
+ if(1){
+ print("True\n");
+ }
+
+ char teststring[3] = "abc";
+ print("%s\n", teststring);
+ teststring[0] = 0;
+ print("%s\n", teststring);
+ char lol[3] = "lol";
+ Rune tesuto[4] = L"テスト";
+ int fp = fileno(stdout);
+ fprint(fp, "%s\n%S\n", lol, tesuto);
exits(nil);
}
--- a/src/to-kana.c
+++ b/src/to-kana.c
@@ -1,11 +1,103 @@
#include <u.h>
#include <stdio.h>
#include <libc.h>
-#include <String.h>
+void eval(int, int);
+void kanafill(Rune, char*, int, Rune*);
+void kanalook(char*, Rune*);
+void printhelp(void);
+int strappend(char*, char);
+
+
void
-kanafill(Rune base, char in[2], int dakuten, Rune* out)
+main(int argc, char *argv[])
{
+ int fpin, fpout;
+ ARGBEGIN{
+ case 's':
+ fpin = fileno(stdin);
+ fpout = fileno(stdout);
+ eval(fpin, fpout);
+ break;
+ case 'h':
+ printhelp();
+ exits(0);
+ default:
+ fprint(2, "usage: %s [-s][-h]\n", argv0);
+ printhelp();
+ exits("usage");
+ }ARGEND;
+ exits(nil);
+}
+
+void
+printhelp(void)
+{
+ print("options:\n");
+ print(" [-s] - use stdin stream\n");
+ print(" [-h] - show this help\n");
+ return;
+}
+
+/*
+ * Runs through the input at the file pointer fpin until it reaches EOF
+ * outputs kana to file pointer fpout.
+ */
+void
+eval(int fpin, int fpout)
+{
+ int reading = 1;
+ char charin;
+ char buf[10];
+ Rune kana[10];
+ while(reading){
+ if(read(fpin, &charin, 1)){
+ if(strappend(buf, charin)){
+ kanalook(buf, kana);
+ fprint(fpout, "%s %S\n", buf, kana);
+ for(int i=0; i<10; i++){
+ kana[i] = 0;
+ buf[i] = 0;
+ }
+ }
+ }
+ else{
+ reading = 0;
+ }
+ }
+ fprint(fpout, "%s\n", buf);
+ return;
+}
+
+/*
+ * Appends the provided character to the end of the provided string.
+ * If this results in a complete kana being formable, return 1.
+ */
+int
+strappend(char* string, char in)
+{
+ int end;
+ int i;
+
+ for(i=0; string[i] != 0; i++);
+ end = (in == 'a' || in == 'i' || in == 'u' || in == 'e' || in == 'o');
+ if (in == 'n'){
+ if (string[i-1] == 'n')
+ end = 1;
+ }
+ string[i] = in;
+ return end;
+}
+
+/*
+ * The kanafill function takes in the base kana of the family to be output,
+ * the character representation of the ending syllabaries, an integer
+ * representing the type of dakuten (1 for regular, 2 for maru), and a pointer
+ * to a rune array that is being edited.
+ */
+void
+kanafill(Rune base, char* in, int dakuten, Rune* out)
+{
switch(base){
/* Special Boys */
case L'や':
@@ -190,13 +282,15 @@
return;
}
+/*
+ * kanalook takes in a pointer to a string and a pointer to a string of Rune
+ * and converts the string in romaji to a string of runes of kana.
+ */
void
-kanalook(char buf[3], Rune* str)
+kanalook(char* buf, Rune* str)
{
- print("%s\n", buf);
- char end[2];
- end[0] = buf[1];
- end[1] = buf[2];
+ char* end;
+ end = buf + 1;
switch(buf[0]){
/* あ family */
case 'a':
@@ -257,7 +351,10 @@
/* な family (and ん) */
case 'n':
- kanafill(L'な', end, 0, str);
+ if(buf[1] == 'n')
+ str[0] = L'ん';
+ else
+ kanafill(L'な', end, 0, str);
break;
/* は family */
@@ -299,25 +396,7 @@
default:
break;
}
- print("%S\n", str);
return;
}
-void
-main(int argc, char *argv[])
-{
- ARGBEGIN{
- default:
- print("No commandline arguments have been implemented yet\n");
- break;
- }ARGEND;
- int fp = fileno(stdin);
- char buf[3];
- read(fp, buf, 2);
- Rune kana[2];
- kanalook(buf, kana);
- print("%s\n", buf);
- print("%S\n", kana);
- print("Hello World\n");
- exits(nil);
-}
+