comparison src/kaigo/qtuti/95y/95y.go @ 65:0369656be06c default tip

many changes.
author pyon@macmini
date Fri, 20 May 2022 06:30:34 +0900
parents
children
comparison
equal deleted inserted replaced
64:ad5c30ee5cf1 65:0369656be06c
1 /*
2 95y.go: Qfuhi Tsuchi (Year Version)
3
4 Last Change: 2021-11-15 月 14:40:46.
5 */
6
7 package main
8
9 /*
10 #cgo LDFLAGS: -L. -lxdwapi -static
11 //
12 // 95.cpp: Qfuhi Tsuchi
13 // Last Change: 2020-09-08 火 15:20:43.
14 //
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <io.h>
20 #include <windows.h>
21 #include <xdw_api.h>
22 #include <xdwapian.h>
23
24 #define MAXCOL 1024
25 #define MAXLINE 9999
26 #define BLOCKSZ 128
27 #define ATN_N 20
28
29 char* xdw2txt(const char* file) {
30 char in_path[_MAX_PATH];
31 _fullpath(in_path, file, _MAX_PATH);
32
33 XDW_DOCUMENT_HANDLE h = NULL; // 文書ハンドルを開く
34 XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_READONLY, XDW_AUTH_NODIALOGUE};
35 if (XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode)) {
36 fprintf(stderr, "Error: cannot open %s\n", file);
37 return NULL;
38 }
39
40 XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0}; // 総ページ数を得る
41 XDW_GetDocumentInformation(h, &info);
42 int nPage = info.nPages;
43
44 // メイン処理
45 char *lpszvalue, *all_lpszvalue;
46 long datasize[9999];
47 for (int i=1; i<=nPage; i++) {
48 datasize[i] = XDW_GetPageTextToMemory(h, i, NULL, 0, NULL);
49 datasize[0] += datasize[i];
50 }
51 datasize[0] += nPage - 1; // for "\n"
52 all_lpszvalue = (char*)malloc(sizeof(char)*datasize[0]);
53 all_lpszvalue[0] = '\0';
54 for (int i = 1; i <= nPage; i++) {
55 if (i < nPage) datasize[i]++; // for "\n"
56 lpszvalue = (char*)malloc(sizeof(char)*(datasize[i]));
57 XDW_GetPageTextToMemory(h, i, lpszvalue, datasize[i], NULL);
58 strcat(all_lpszvalue, lpszvalue);
59 if (i < nPage) strcat(all_lpszvalue, "\n");
60 free(lpszvalue);
61 }
62
63 XDW_CloseDocumentHandle(h, NULL); // 文書ハンドルを閉じる
64 return all_lpszvalue;
65 }
66
67 void xdw2txtb(const char* xdwfile, const char* txtfile) {
68 char in_path[_MAX_PATH], out_path[_MAX_PATH];
69 _fullpath(in_path, xdwfile, _MAX_PATH);
70 _fullpath(out_path, txtfile, _MAX_PATH);
71
72 XDW_DOCUMENT_HANDLE h = NULL; // 文書ハンドルを開く
73 XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_READONLY, XDW_AUTH_NODIALOGUE};
74 if (XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode)) {
75 fprintf(stderr, "Error: cannot open %s\n", xdwfile);
76 return;
77 }
78
79 XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0}; // 総ページ数を得る
80 XDW_GetDocumentInformation(h, &info);
81 int nPage = info.nPages;
82
83 FILE *fp;
84 if ((fp = fopen(out_path, "w")) == NULL) {
85 fprintf(stderr, "Error: cannot open %s\n", out_path);
86 return;
87 }
88
89 long datasize;
90 char* lpszvalue;
91
92 for (int i = 1; i <= nPage; i++) {
93 datasize = XDW_GetPageTextToMemory(h, i, NULL, 0, NULL);
94 lpszvalue = (char*)malloc(sizeof(char)*datasize);
95 XDW_GetPageTextToMemory(h, i, lpszvalue, datasize, NULL);
96 fprintf(fp, "%s\n", lpszvalue);
97 free(lpszvalue);
98 }
99
100 fclose(fp);
101 XDW_CloseDocumentHandle(h, NULL); // 文書ハンドルを閉じる
102 return;
103 }
104
105 int xdwsplit1(const char* file, const char* workdir, const char* prefix) {
106 char file_path[_MAX_PATH];
107 _fullpath(file_path, file, _MAX_PATH);
108
109 XDW_DOCUMENT_HANDLE h = NULL;
110 XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_READONLY, XDW_AUTH_NODIALOGUE};
111 if (XDW_OpenDocumentHandle(file_path, &h, (XDW_OPEN_MODE*)&mode)) {
112 fprintf(stderr, "Error: cannot open %s\n", file);
113 return -1;
114 }
115
116 XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0};
117 XDW_GetDocumentInformation(h, &info);
118 int nPage = info.nPages;
119
120 char buf[_MAX_PATH];
121 for (int i = 1; i <= nPage; i++) {
122 sprintf(buf, "%s/%s%05d.xdw", workdir, prefix, i);
123 //sprintf(buf, "%s_%05d.xdw", prefix, i);
124 _fullpath(file_path, buf, _MAX_PATH);
125
126 int api_result = XDW_GetPage(h, i, file_path, NULL);
127 if (api_result < 0) {
128 fprintf(stderr, "XDW Error: cannot get page (%s p=%d)\n", file, i);
129 return -1;
130 }
131 }
132
133 XDW_CloseDocumentHandle(h, NULL);
134 return 0;
135 }
136
137 int xdwextpage(const char* infile, const int p, const char* outfile) {
138 char file_path[_MAX_PATH];
139 _fullpath(file_path, infile, _MAX_PATH);
140
141 XDW_DOCUMENT_HANDLE h = NULL;
142 XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_READONLY, XDW_AUTH_NODIALOGUE};
143 if (XDW_OpenDocumentHandle(file_path, &h, (XDW_OPEN_MODE*)&mode)) {
144 fprintf(stderr, "Error: cannot open %s\n", infile);
145 return -1;
146 }
147
148 XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0};
149 XDW_GetDocumentInformation(h, &info);
150 int nPage = info.nPages;
151
152 char buf[_MAX_PATH];
153 _fullpath(file_path, outfile, _MAX_PATH);
154
155 int api_result = XDW_GetPage(h, p, file_path, NULL);
156 if (api_result < 0) {
157 fprintf(stderr, "XDW Error: cannot get page (%s p=%d)\n", infile, p);
158 return -1;
159 }
160
161 XDW_CloseDocumentHandle(h, NULL);
162 return 0;
163 }
164
165 void xdwmerge(const char* list, const char* output) {
166 FILE *fp;
167
168 if ((fp = fopen(list, "r")) == NULL) {
169 fprintf(stderr, "XDW Error: can't open file [%s]\n", list);
170 exit(1);
171 }
172
173 char *all_path = (char*)malloc(MAXLINE * sizeof(char) * _MAX_PATH);
174
175 if (all_path == NULL) {
176 fprintf(stderr, "XDW Error: can't allocate memory\n");
177 exit(1);
178 }
179
180 int n = 0;
181 char *q;
182 char buf[_MAX_PATH];
183
184 while (fgets(buf, sizeof buf, fp)) {
185 if ((q = strchr(buf, '\n')) != NULL) {
186 *q = '\0';
187 }
188 _fullpath(buf, buf, _MAX_PATH);
189 strncpy(&all_path[n * _MAX_PATH], buf, _MAX_PATH);
190 n++;
191 }
192 fclose(fp);
193
194 char *blk_path = (char*)malloc(BLOCKSZ * sizeof(char) * _MAX_PATH);
195 const char **blk_path_addr = (const char**)malloc((n / BLOCKSZ + 1) * sizeof(char*) * _MAX_PATH);
196 if (blk_path == NULL || blk_path_addr == NULL) {
197 fprintf(stderr, "XDW Error: can't allocate memory\n");
198 exit(1);
199 }
200
201 // process by block
202 int api_result;
203 int bn = 0;
204 for (int p = 0, m = 0; p < n; p++) {
205 m = p % BLOCKSZ;
206 if (m == 0 && p > 0) {
207 sprintf(buf, "tmp_b%04d.xdw", ++bn);
208 _fullpath(buf, buf, _MAX_PATH);
209 api_result = XDW_MergeXdwFiles(blk_path_addr, BLOCKSZ, buf, NULL);
210 if (api_result < 0) {
211 fprintf(stderr, "XDW Error: can't merge [1] (p = %d, m = %d)\n", p, m);
212 exit(1);
213 }
214 }
215 strncpy(&blk_path[m * _MAX_PATH], &all_path[p * _MAX_PATH], _MAX_PATH);
216 blk_path_addr[m] = &blk_path[m * _MAX_PATH];
217 }
218
219 sprintf(buf, "tmp_b%04d.xdw", ++bn);
220 _fullpath(buf, buf, _MAX_PATH);
221
222 int mod = n % BLOCKSZ;
223 if (mod == 0) mod = BLOCKSZ;
224 api_result = XDW_MergeXdwFiles(blk_path_addr, mod, buf, NULL);
225
226 if (api_result < 0) {
227 fprintf(stderr, "XDW Error: can't merge [2]\n");
228 exit(1);
229 }
230
231 // merge blocks
232 for (int b = 0; b < bn; b++) {
233 sprintf(buf, "tmp_b%04d.xdw", b + 1);
234 _fullpath(buf, buf, _MAX_PATH);
235 strncpy(&blk_path[b * _MAX_PATH], buf, _MAX_PATH);
236 blk_path_addr[b] = &blk_path[b * _MAX_PATH];
237 }
238
239 _fullpath(buf, output, _MAX_PATH );
240 api_result = XDW_MergeXdwFiles(blk_path_addr, bn, buf, NULL);
241
242 if (api_result < 0) {
243 fprintf(stderr, "XDW Error: can't merge [3]\n");
244 exit(1);
245 }
246
247 free(all_path);
248 free(blk_path);
249 free(blk_path_addr);
250
251 for (int b = 0; b < bn; b++) {
252 sprintf(buf, "tmp_b%04d.xdw", b + 1);
253 _fullpath(buf, buf, _MAX_PATH);
254 remove(buf);
255 }
256 }
257
258 int xdwaddatn(const char* xdwfile, const char* atnfile) {
259 FILE *fp;
260 char filepath[_MAX_PATH];
261 _fullpath(filepath, atnfile, _MAX_PATH);
262
263 if ((fp = fopen(filepath, "r")) == NULL) {
264 fprintf(stderr, "Error: cannot open %s\n", filepath);
265 return -1;
266 }
267
268 char keyword[128];
269 char *q;
270 fgets(keyword, sizeof keyword, fp);
271 if ((q = strchr(keyword, '\n')) != NULL) {
272 *q = '\0';
273 }
274
275 char buf[_MAX_PATH];
276 int x[ATN_N], y[ATN_N], sz[ATN_N], tr[ATN_N];
277 char txt[ATN_N][256];
278 int an = 0;
279 while (fgets(buf, sizeof buf, fp)) {
280 if ((q = strchr(buf, '\n')) != NULL) {
281 *q = '\0';
282 }
283
284 x[an] = atoi(strtok(buf, ","));
285 y[an] = atoi(strtok(NULL, ","));
286 sz[an] = atoi(strtok(NULL, ","));
287 tr[an] = atoi(strtok(NULL, ","));
288 strcpy(txt[an], strtok(NULL, ","));
289 an++;
290 //printf("x=%d y=%d txt=%s sz=%d tr=%d\n", x[an], y[an], txt[an], sz[an], tr[an]);
291 }
292 fclose(fp);
293
294
295 XDW_DOCUMENT_HANDLE h = NULL;
296 XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE};
297
298 _fullpath(filepath, xdwfile, _MAX_PATH);
299 int api_result = XDW_OpenDocumentHandle(filepath, &h, (XDW_OPEN_MODE*)&mode);
300 if (api_result < 0) return api_result;
301
302 XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0};
303 XDW_GetDocumentInformation(h, &info);
304 XDW_FOUND_HANDLE pFoundHandle = NULL;
305 for (int i = 0; i < info.nPages; i++) {
306 if (keyword[0] != '\0') {
307 api_result = XDW_FindTextInPage(h, i + 1, keyword, NULL, &pFoundHandle, NULL);
308 if (!pFoundHandle) {
309 XDW_CloseFoundHandle(pFoundHandle);
310 continue;
311 }
312 XDW_CloseFoundHandle(pFoundHandle);
313 }
314 for (int j = 0; j < an; j++ ) {
315 XDW_ANNOTATION_HANDLE annoation;
316 int api_result = XDW_AddAnnotation(h, XDW_AID_TEXT, i + 1, x[j], y[j], NULL, &annoation, NULL);
317 if (api_result < 0) return api_result;
318
319 api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, txt[j], 0, NULL);
320 api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, (char*)&sz[j], 0, NULL);
321
322 int color = XDW_COLOR_WHITE;
323 if (tr[j]) {
324 color = XDW_COLOR_NONE;
325 }
326 api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, (char*)&color, 0, NULL);
327 }
328 }
329
330 XDW_SaveDocument(h, NULL);
331 XDW_CloseDocumentHandle(h, NULL);
332
333 return 0;
334 }
335
336 int xdwaddatntool(const char* xdwfile, const char* toolfile, int n, int x, int y) {
337 char filepath[_MAX_PATH];
338 _fullpath(filepath, xdwfile, _MAX_PATH);
339
340 XDW_DOCUMENT_HANDLE h = NULL;
341 XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE};
342
343 int api_result = XDW_OpenDocumentHandle(filepath, &h, (XDW_OPEN_MODE*)&mode);
344 if (api_result < 0) return api_result;
345
346 XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0};
347 XDW_GetDocumentInformation(h, &info);
348
349 _fullpath(filepath, toolfile, _MAX_PATH);
350 for (int i = 0; i < info.nPages; i++) {
351 XDW_ANNOTATION_HANDLE annoation;
352 int api_result = XDW_AddAnnotationFromAnnFile(h, filepath, n, i + 1, NULL, x, y, &annoation, NULL);
353 }
354
355 XDW_SaveDocument(h, NULL);
356 XDW_CloseDocumentHandle(h, NULL);
357
358 return 0;
359 }
360
361
362 */
363 import "C"
364
365 import (
366 "bufio"
367 "regexp"
368 "encoding/json"
369 "flag"
370 "fmt"
371 "log"
372 "os"
373 "path/filepath"
374 "sort"
375 "strings"
376
377 "golang.org/x/text/encoding/japanese"
378 "golang.org/x/text/transform"
379 )
380
381 type Hhs struct {
382 No string
383 Name string
384 Weight int
385 //Text string
386 Xdw []string
387 }
388
389 func(h *Hhs) AppendXdw(xdw string) {
390 h.Xdw = append(h.Xdw, xdw)
391 }
392
393 func(h *Hhs) CsvString() string {
394 return strings.Join([]string{h.No, h.Name}, ",")
395 }
396
397 func(h *Hhs) Dump() string {
398 x := strings.Join(h.Xdw, ",")
399 return strings.Join([]string{h.No, h.Name, fmt.Sprintf("%d", h.Weight), x}, "\t")
400 }
401
402 type Config struct {
403 Indir string
404 Outdir string
405 Workdir string
406 Atnfile[] string
407 }
408
409 var (
410 ver = "0.1"
411 conf Config
412 confjson = "95y.json"
413 logfile = "95y.log"
414 whitexdw = "aiobo.xdw"
415 split_n = 9000
416
417 out_q1 = "q1.xdw"
418 out_q2 = "q2.xdw"
419 out_q3 = "q3.xdw"
420 out_l = "l.csv"
421
422 delfile = "del.list"
423 sortfile = "sort.list"
424
425 tmpfile = "95y.tmp"
426
427 in_xdw []string
428 re_hhsno, re_name *regexp.Regexp
429
430 // option parameters
431 version bool
432 )
433
434 func init() {
435 /* コンフィグファイルは JSON */
436 content, err := os.ReadFile(confjson)
437 if err != nil {
438 log.Fatal(err)
439 }
440 if err := json.Unmarshal(content, &conf); err != nil {
441 log.Fatal(err)
442 }
443
444 out_q1 = filepath.Join(conf.Outdir, out_q1)
445 out_q2 = filepath.Join(conf.Outdir, out_q2)
446 out_q3 = filepath.Join(conf.Outdir, out_q3)
447 out_l = filepath.Join(conf.Outdir, out_l)
448
449 logfile = filepath.Join(conf.Workdir, logfile)
450
451 /* 一時ファイル消去 */
452 os.RemoveAll(conf.Outdir)
453 os.RemoveAll(conf.Workdir)
454 os.Mkdir(conf.Outdir, 0755)
455 os.Mkdir(conf.Workdir, 0755)
456
457 /* 変数初期化 */
458 re_hhsno = regexp.MustCompile(`0[1238]00\d{6}`)
459 re_name = regexp.MustCompile(`管理者 老 松 博 行.{14}`)
460
461 /* Docuworksファイル列挙 */
462 files, err := os.ReadDir(conf.Indir)
463 if err != nil {
464 log.Fatal(err)
465 }
466
467 for _, file := range files {
468 if !strings.HasSuffix(file.Name(), ".xdw") {
469 continue
470 }
471 if strings.HasPrefix(file.Name(), "KDPK016G") || strings.HasPrefix(file.Name(), "KDPK126G") {
472 in_xdw = append(in_xdw, file.Name())
473 }
474 }
475
476 flag.BoolVar(&version, "v", false, "print version")
477 }
478
479 func main() {
480 flag.Parse()
481
482 if version {
483 fmt.Println("95y - version", ver)
484 os.Exit(0)
485 }
486
487 /* 重み付けの準備 */
488 err, sort_list := file2slice(filepath.Join(conf.Indir, sortfile))
489 if err != nil {
490 log.Fatal(err)
491 }
492
493 /* 給付費通知を漁り,構造体を初期化 */
494 hash_Hhs := make(map[string]Hhs)
495 for i, file := range in_xdw {
496 for p, t := range xdw2txtb(filepath.Join(conf.Indir, file), filepath.Join(conf.Workdir, tmpfile)) {
497 hno := re_hhsno.FindString(t)
498 _, ok := hash_Hhs[hno]
499 if strings.Contains(t, "大曲仙北広域市町村圏組合") && !ok {
500 name := re_name.FindString(t)
501 name = strings.Replace(name, "管理者 老 松 博 行", "", 1)
502
503 w := 99
504 for j, s := range sort_list {
505 if strings.Contains(t, s) {
506 w = j
507 }
508 }
509
510 h := Hhs{No: hno, Name: name, Weight: w, Xdw: []string{fmt.Sprintf("%02d_%05d.xdw", i, p + 1)}}
511 hash_Hhs[hno] = h
512 } else {
513 h := hash_Hhs[hno]
514 h.AppendXdw(fmt.Sprintf("%02d_%05d.xdw", i, p + 1))
515 hash_Hhs[hno] = h
516 }
517 //fmt.Println(file, i, "-", p) // <---
518 }
519 }
520 fmt.Println("analize done")
521
522 /* バックグラウンドで給付費通知をバラす */
523 ch := make(chan int)
524 go func() {
525 for i, file := range in_xdw {
526 qtsuchi := filepath.Join(conf.Indir, file)
527 C.xdwsplit1(C.CString(qtsuchi), C.CString(conf.Workdir), C.CString(fmt.Sprintf("%02d_", i)))
528 }
529 ch <- 1
530 fmt.Println("split done")
531 }()
532
533 /* そのあいだに 不要者削除,ソート,マージ */
534 err, del_list := file2slice(filepath.Join(conf.Indir, delfile))
535 if err != nil {
536 log.Fatal(err)
537 }
538 for _, hno := range del_list {
539 delete(hash_Hhs, hno)
540 }
541 fmt.Println("delete done")
542
543 /* ソート */
544 var slice_Hhs []Hhs
545 for _, h := range hash_Hhs {
546 slice_Hhs = append(slice_Hhs, h)
547 }
548 sort.Slice(slice_Hhs, func(i, j int) bool {
549 if slice_Hhs[i].Weight != slice_Hhs[j].Weight {
550 return slice_Hhs[i].Weight < slice_Hhs[j].Weight
551 }
552 if slice_Hhs[i].No != slice_Hhs[j].No {
553 return slice_Hhs[i].No < slice_Hhs[j].No
554 }
555 return false
556 })
557 fmt.Println("sort done")
558
559 /* マージ */
560 var list_q1, list_q2 []string
561 for _, h := range slice_Hhs {
562 if h.Weight < 99 {
563 for _, x := range h.Xdw {
564 buf := filepath.Join(conf.Workdir, x)
565 list_q1 = append(list_q1, buf)
566 }
567 if len(h.Xdw) % 2 == 1 {
568 list_q1 = append(list_q1, whitexdw)
569 }
570 } else {
571 for _, x := range h.Xdw {
572 buf := filepath.Join(conf.Workdir, x)
573 list_q2 = append(list_q2, buf)
574 }
575 if len(h.Xdw) % 2 == 1 {
576 list_q2 = append(list_q2, whitexdw)
577 }
578 }
579 }
580
581 <-ch
582 xdwmerge(list_q1, out_q1)
583 if len(list_q2) <= split_n {
584 xdwmerge(list_q2, out_q2)
585 } else {
586 xdwmerge(list_q2[:split_n], out_q2)
587 xdwmerge(list_q2[split_n:], out_q3)
588 }
589 fmt.Println("merge done")
590
591 /* バックグラウンドで給付費通知を校正 */
592 ch_q1 := make(chan int)
593 go func() {
594 for _, a := range conf.Atnfile {
595 xdwaddatn(out_q1, a)
596 }
597 ch_q1 <- 1
598 fmt.Println("correct1 done")
599 } ()
600
601 ch_q2 := make(chan int)
602 go func() {
603 for _, a := range conf.Atnfile {
604 xdwaddatn(out_q2, a)
605 }
606 ch_q2 <- 1
607 fmt.Println("correct2 done")
608 } ()
609
610 ch_q3 := make(chan int)
611 if len(list_q2) > split_n {
612 go func() {
613 for _, a := range conf.Atnfile {
614 xdwaddatn(out_q3, a)
615 }
616 ch_q3 <- 1
617 fmt.Println("correct3 done")
618 } ()
619 }
620
621 /* リスト出力 & ログダンプ */
622 csvtxt := ""
623 logtxt := ""
624 for _, h := range slice_Hhs {
625 csvtxt += h.CsvString() + "\n"
626 logtxt += h.Dump() + "\n"
627 }
628 csvtxt, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), csvtxt)
629 if err := os.WriteFile(out_l, []byte(csvtxt), 0644); err != nil {
630 log.Fatal(err)
631 }
632 if err := os.WriteFile(logfile, []byte(logtxt), 0644); err != nil {
633 log.Fatal(err)
634 }
635 fmt.Println("logdump done")
636
637 <-ch_q1
638 <-ch_q2
639 if len(list_q2) > split_n {
640 <-ch_q3
641 }
642 }
643
644 func file2slice(file string) (err error, list []string) {
645 f, err := os.Open(file)
646 if err != nil {
647 return err, nil
648 }
649 defer f.Close()
650
651 buf := bufio.NewScanner(f)
652 for buf.Scan() {
653 list = append(list, buf.Text())
654 }
655 return nil, list
656 }
657
658 func xdw2txt(file string) (txt []string) {
659 s := C.GoString(C.xdw2txt(C.CString(file)))
660 r := strings.NewReader(s)
661 tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder())
662 buf := bufio.NewScanner(tr)
663 for buf.Scan() {
664 txt = append(txt, buf.Text())
665 }
666 return
667 }
668
669 func xdw2txtb(xdwfile, txtfile string) (txt []string) {
670 if _, err := os.Stat(txtfile); os.IsExist(err) {
671 os.Remove(txtfile)
672 }
673
674 C.xdw2txtb(C.CString(xdwfile), C.CString(txtfile))
675 content, err := os.ReadFile(txtfile)
676 if err != nil {
677 return nil
678 }
679
680 r := strings.NewReader(string(content))
681 tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder())
682 buf := bufio.NewScanner(tr)
683 for buf.Scan() {
684 txt = append(txt, buf.Text())
685 }
686 return
687 }
688
689 func xdwmerge(list []string, outfile string) (err error) {
690 order := strings.Join(list, "\n")
691 order, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), order)
692 orderfile := fmt.Sprintf("%s_order.txt", outfile)
693 if err := os.WriteFile(orderfile, []byte(order), 0644); err != nil {
694 return err
695 }
696 C.xdwmerge(C.CString(orderfile), C.CString(outfile))
697 return nil
698 }
699
700 func xdwaddatn(xdwfile, atnfile string) (err error) {
701 C.xdwaddatn(C.CString(xdwfile), C.CString(atnfile))
702 return nil
703 }
704
705 func xdwaddatntool(xdwfile, toolfile string, n, x, y int) (err error) {
706 C.xdwaddatntool(C.CString(xdwfile), C.CString(toolfile), C.int(n), C.int(x), C.int(y))
707 return nil
708 }
709