*** empty log message ***
[m17n/m17n-test.git] / linebreak.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <m17n.h>
6 #include <m17n-misc.h>
7
8 int
9 main (int argc, char **argv)
10 {
11   char buf[256];
12   MSymbol Mutf_8;
13   int option = 0;
14   int i;
15
16   M17N_INIT ();
17   Mutf_8 = msymbol ("utf-8");
18   for (i = 1; i < argc; i++)
19     {
20       if (strcmp (argv[i], "--sp-cm") == 0)
21         option |= MTEXT_LBO_SP_CM;
22       else if (strcmp (argv[i], "--korean-sp") == 0)
23         option |= MTEXT_LBO_KOREAN_SP;
24       else if (strcmp (argv[i], "--ai-as-id") == 0)
25         option |= MTEXT_LBO_AI_AS_ID;
26       else
27         {
28           printf ("Usage: linebreak [--sp-cm] [--korean-sp] [--ai-as-id]\n");
29           exit (0);
30         }
31     }
32
33   while (fgets (buf, 256, stdin) != NULL)
34     {
35       int len = strlen (buf);
36       MText *mt1, *mt2;
37       int pos, last_pos = -1;
38
39       if (buf[len - 1] == '\n')
40         len--;
41
42       mt1 = mconv_decode_buffer (msymbol ("utf-8"), (unsigned char *) buf, len);
43       pos = mtext_len (mt1) - 1;
44       while ((pos = mtext_character (mt1, pos, 0, '\\')) >= 0)
45         {
46           if (mtext_ref_char (mt1, pos + 1) == 'n')
47             {
48               mtext_del (mt1, pos + 1, pos + 2);
49               mtext_set_char (mt1, pos, '\n');
50             }
51         }
52       pos = mtext_len (mt1);
53       mt2 = mtext_dup (mt1);
54       while (pos > 0)
55         {
56           int break_pos = mtext_line_break (mt1, pos, option, NULL);
57
58           if (break_pos > pos)
59             break;
60           if (break_pos != last_pos)
61             {
62               mtext_ins_char (mt2, break_pos, '|', 1);
63               last_pos = break_pos;
64             }
65           pos = break_pos - 1;
66         }
67
68       pos = mtext_len (mt2) - 1;
69       while ((pos = mtext_character (mt2, pos, 0, '\n')) >= 0)
70         {
71           mtext_set_char (mt2, pos, '\\');
72           mtext_ins_char (mt2, pos + 1, 'n', 1);
73         }
74
75       mconv_encode_stream (Mutf_8, mt2, stdout);
76       printf ("\n");
77       m17n_object_unref (mt1);
78       m17n_object_unref (mt2);
79     }
80
81   M17N_FINI ();
82   exit (0);
83 }