Mercurial > mercurial > hgweb_golang.cgi
diff src/kaigo/Perori/bk.go @ 41:34a474fb83c3
add perori/porori/nk.
author | pyon@macmini |
---|---|
date | Wed, 04 Mar 2020 23:46:59 +0900 |
parents | |
children | c58172a59534 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/kaigo/Perori/bk.go Wed Mar 04 23:46:59 2020 +0900 @@ -0,0 +1,437 @@ +/* + bk.go: Insert Biko & Hatsuban + + Last Change: 2020-03-04 水 16:48:05. +*/ + +package main + +/* +#cgo LDFLAGS: -L. -lxdwapi -static +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <io.h> +#include <windows.h> +#include <xdw_api.h> +#include <xdwapian.h> + +#define MAXCOL 1024 +#define MAXLINE 9999 +#define BLOCKSZ 128 + +char* xdw2txt(const char* file) { + char in_path[_MAX_PATH]; + _fullpath(in_path, file, _MAX_PATH); + + XDW_DOCUMENT_HANDLE h = NULL; // 文書ハンドルを開く + XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_READONLY, XDW_AUTH_NODIALOGUE}; + if (XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode)) { + printf("Error: cannot open %s\n", file); + return NULL; + } + + XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0}; // 総ページ数を得る + XDW_GetDocumentInformation(h, &info); + int nPage = info.nPages; + + // メイン処理 + char *lpszvalue, *all_lpszvalue; + long datasize[9999]; + for (int i = 1; i <= nPage; i++) { + datasize[i] = XDW_GetPageTextToMemory(h, i, NULL, 0, NULL); + datasize[0] += datasize[i]; + } + datasize[0] += nPage - 1; // for "\n" + all_lpszvalue = (char*)malloc(sizeof(char)*datasize[0]); + all_lpszvalue[0] = '\0'; + for (int i = 1; i <= nPage; i++) { + if (i<nPage) datasize[i]++; // for "\n" + lpszvalue = (char*)malloc(sizeof(char)*(datasize[i])); + XDW_GetPageTextToMemory(h, i, lpszvalue, datasize[i], NULL); + strcat(all_lpszvalue, lpszvalue); + if (i < nPage) strcat(all_lpszvalue, "\n"); + free(lpszvalue); + } + + XDW_CloseDocumentHandle(h, NULL); // 文書ハンドルを閉じる + return all_lpszvalue; +} + +int xdwhb(const char* in_file, const char* out_file, char* hb, const char* stxt) { + int x = 18000; + int y = 1200; + int sz = 110; + + char in_path[_MAX_PATH], out_path[_MAX_PATH]; + _fullpath(in_path, in_file, _MAX_PATH); + _fullpath(out_path, out_file, _MAX_PATH); + + XDW_DOCUMENT_HANDLE h = NULL; + XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE}; + + int api_result = XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode); + if (api_result < 0) return api_result; + + XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0}; + XDW_GetDocumentInformation(h, &info); + + XDW_FOUND_HANDLE pFoundHandle = NULL; + for (int i = 0; i < info.nPages; i++) { + api_result = XDW_FindTextInPage(h, i + 1, stxt, NULL, &pFoundHandle, NULL); + if (pFoundHandle == NULL) continue; + + XDW_ANNOTATION_HANDLE annoation; + int api_result = XDW_AddAnnotation(h, XDW_AID_TEXT, i + 1, x, y, NULL, &annoation, NULL); + if (api_result < 0) return api_result; + + api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, hb, 0, NULL); + api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, (char*)&sz, 0, NULL); + + int color = XDW_COLOR_NONE; + api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, (char*)&color, 0, NULL); + } + + XDW_SaveDocument(h, NULL); + XDW_CloseDocumentHandle(h, NULL); + + api_result = XDW_OptimizeDocument(in_path, out_path, NULL); + return 0; +} + +int xdwbiko(const char* in_file, const int page, const int r, char* biko, char* kubun) { + int x1 = 2000; + int y1 = 4680 + r * 2720; + int sz = 90; + int x2 = 18950; + int y2 = 5090 + r * 2720; + + char in_path[_MAX_PATH]; + _fullpath(in_path, in_file, _MAX_PATH); + + XDW_DOCUMENT_HANDLE h = NULL; + XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE}; + + int api_result = XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode); + if (api_result < 0) return api_result; + + XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0}; + XDW_GetDocumentInformation(h, &info); + + XDW_ANNOTATION_HANDLE annoation1, annoation2; + int color = XDW_COLOR_NONE; + + api_result = XDW_AddAnnotation(h, XDW_AID_TEXT, page, x1, y1, NULL, &annoation1, NULL); + if (api_result < 0) return api_result; + api_result = XDW_AddAnnotation(h, XDW_AID_TEXT, page, x2, y2, NULL, &annoation2, NULL); + if (api_result < 0) return api_result; + + api_result = XDW_SetAnnotationAttribute(h, annoation1, XDW_ATN_Text, XDW_ATYPE_STRING, biko, 0, NULL); + api_result = XDW_SetAnnotationAttribute(h, annoation1, XDW_ATN_FontSize, XDW_ATYPE_INT, (char*)&sz, 0, NULL); + api_result = XDW_SetAnnotationAttribute(h, annoation1, XDW_ATN_BackColor, XDW_ATYPE_INT, (char*)&color, 0, NULL); + + api_result = XDW_SetAnnotationAttribute(h, annoation2, XDW_ATN_Text, XDW_ATYPE_STRING, kubun, 0, NULL); + api_result = XDW_SetAnnotationAttribute(h, annoation2, XDW_ATN_FontSize, XDW_ATYPE_INT, (char*)&sz, 0, NULL); + api_result = XDW_SetAnnotationAttribute(h, annoation2, XDW_ATN_BackColor, XDW_ATYPE_INT, (char*)&color, 0, NULL); + + XDW_SaveDocument(h, NULL); + XDW_CloseDocumentHandle(h, NULL); + + return 0; +} + +*/ +import "C" + +import ( + "bufio" + "encoding/csv" + "fmt" + "flag" + "io/ioutil" + "log" + "os" + "regexp" + "strconv" + "strings" + "time" + + "golang.org/x/text/encoding/japanese" + "golang.org/x/text/transform" +) + +// Constants +const ( + version = "0.3" + default_ccsvfile = "chosairai.csv" + default_icsvfile = "ikenshoirai.csv" + default_cxdwfile = "KBPC116G.xdw" + default_ixdwfile = "KBPB116G.xdw" + default_tmpxdw = "tmp.xdw" + default_cout = "outc.xdw" + default_iout = "outi.xdw" + default_lout = "xyz.csv" +) + +var ( + hbi, hbc string // 発番 + start time.Time + + flg_hb int + flg_time bool + + re_hhsno, re_date, re_city, re_name, re_zensp *regexp.Regexp +) + +func init(){ + os.Remove(default_cout) + os.Remove(default_iout) + os.Remove(default_lout) + + re_hhsno = regexp.MustCompile(`0[1238]\d{8}`) + re_date = regexp.MustCompile(`((明治)|(大正)|(昭和)|(平成)|(令和)).{1,2}年.\d月.\d日`) + re_city = regexp.MustCompile(`(((平成)|(令和)).{1,2}年.\d月.\d日){2}...`) + re_name = regexp.MustCompile(`0[1238]\d{8}.*((平成)|(令和)).{1,2}年.\d月.\d日`) + re_zensp = regexp.MustCompile(` {2,}`) +} + +func main() { + start = time.Now() + /* PRINT HEADER */ + fmt.Println("=======================================") + fmt.Println(" 備考を...する") + fmt.Printf(" - bk [ver %s] -\n", version) + fmt.Println("=======================================\n") + + + /* INITIALIZE FLAGS */ + today := time.Now().Format("20060102") + + flag.StringVar(&today, "d", today, "irai ymd (default today)") + flag.IntVar(&flg_hb, "b", 0, "set hatsuban (default 0)") + flag.BoolVar(&flg_time, "t", false, "print time") + + flag.Parse() + + /* USER INPUT */ + hbi = fmt.Sprintf("%d", flg_hb) + if flg_hb == 0 { + fmt.Print("発番 > ") + fmt.Scan(&hbi) + } + i, err := strconv.Atoi(hbi) + if err != nil { + log.Fatal(err) + } + hbc = fmt.Sprintf("%d", i + 1) + + /* READ BIKO FROM CSV */ + bhash, err := getBiko_fromCSV(default_ccsvfile, today) + if err != nil { + log.Fatal(err) + } + fmt.Println(" 備考データ読込") + step_start := print_time(start) + + /* READ KUBUN FROM CSV */ + khash, err := getKubun_fromCSV(default_icsvfile, bhash) + if err != nil { + log.Fatal(err) + } + fmt.Println(" 申請区分データ読込") + step_start = print_time(start) + + /* INSERT BIKO */ + var ctxt []string + copy_tmp(default_cxdwfile) + for p, t := range xdw2txt(default_cxdwfile) { + for r, hno := range re_hhsno.FindAllString(t, -1) { + if bk, ok := bhash[hno]; ok { + C.xdwbiko(C.CString(default_tmpxdw), C.int(p + 1), C.int(r), C.CString(bk), C.CString(khash[hno])) + } + } + ctxt = append(ctxt, t) + } + fmt.Println(" 備考と申請区分を追加") + + /* INSERT HATSU-BAN & OPTIMIZE */ + stxt, _, _ := transform.String(japanese.ShiftJIS.NewEncoder(), "大仙広介") + C.xdwhb(C.CString(default_tmpxdw), C.CString(default_cout), C.CString(hbi), C.CString(stxt)) + fmt.Println(" 発番追加(調査依頼ファイル)") + step_start = print_time(step_start) + + copy_tmp(default_ixdwfile) + C.xdwhb(C.CString(default_tmpxdw), C.CString(default_iout), C.CString(hbc), C.CString(stxt)) + fmt.Println(" 発番追加( 意見書依頼ファイル)") + step_start = print_time(step_start) + + /* CHOSA IRAI LIST */ + f, err := os.Create(default_lout) + if err != nil { + log.Fatal(err) + } + defer f.Close() + w := bufio.NewWriter(transform.NewWriter(f, japanese.ShiftJIS.NewEncoder())) + + header := []string{"申請日", "被保番", "氏名", "生年月日", "市町村", "-", "-", "-", "依頼日"} + fmt.Fprintln(w, strings.Join(header, ",")) + + var req string + for _, txt := range ctxt { + str := strings.TrimRight(txt, " ") + if strings.HasSuffix(str, "依頼書") { + req = re_date.FindString(str) + req = strings.Replace(req, " ", "", -1) + } else { + row := strings.Split(str, "〒") + for i := 0; i < len(row) - 1; i++ { + var app, hhsno, name, birth, city, empty string + + d := re_date.FindAllString(row[i], -1) + if len(d) > 0 { + birth = strings.Replace(d[0], " ", "", -1) + app = strings.Replace(d[1], " ", "", -1) + } + if re_hhsno.MatchString(row[i]) { + hhsno = "=\"" + re_hhsno.FindString(row[i]) + "\"" + } + if re_name.MatchString(row[i]) { + n := []rune(re_name.FindString(row[i])) + kana := string(n[10:36]) + kana = strings.Trim(kana, " ") + name = string(n[37:55]) + name = strings.Trim(name, " ") + name = re_zensp.ReplaceAllString(name, "") + name += "(" + kana + ")" + } + if re_city.MatchString(row[i]) { + c := []rune(re_city.FindString(row[i])) + city = string(c[len(c)-3:]) + city = strings.Replace(city, "仙北郡", "美郷町", -1) + } + + if hhsno != "" { + fields := []string{app, hhsno, name, birth, city, empty, empty, empty, req} + fmt.Fprintln(w, strings.Join(fields, ",")) + } + } + } + } + w.Flush() + + fmt.Println(" 調査依頼リスト作成") + step_start = print_time(step_start) + + /* CLEAN */ + os.Remove(default_tmpxdw) + fmt.Println(" 終了") + step_start = print_time(step_start) +} + +func getBiko_fromCSV(file, date string) (bikohash map[string]string, err error) { + bikohash = make(map[string]string) + + data, err := ioutil.ReadFile(file) + if err != nil { + return bikohash, err + } + + r := csv.NewReader(strings.NewReader(string(data))) + records, err := r.ReadAll() + if err != nil { + return bikohash, err + } + + for _, record := range records { + hno := strings.TrimSpace(record[0]) + iraiymd := strings.TrimSpace(record[5]) + if iraiymd != date { + continue + } + bikohash[hno] = record[3] + } + + return bikohash, nil +} + +func getKubun_fromCSV(file string, hhshash map[string]string) (kubunhash map[string]string, err error) { + kubunhash = make(map[string]string) + ymdhash := make(map[string]string) + + data, err := ioutil.ReadFile(file) + if err != nil { + return kubunhash, err + } + + r := csv.NewReader(strings.NewReader(string(data))) + records, err := r.ReadAll() + if err != nil { + return kubunhash, err + } + + for _, record := range records { + hno := strings.TrimSpace(record[0]) + if _, ok := hhshash[hno]; !ok { + continue + } + if ymd, ok := ymdhash[hno]; !ok || ymd < record[8] { + ymdhash[hno] = record[8] + } + var buf string + switch strings.TrimSpace(record[9]) { + case "01": + buf = "[新規]" + case "02": + buf = "[更新]" + case "10": + buf = "[支介]" + case "05": + buf = "[区変]" + case "03": + buf = "[転入]" + case "09": + buf = "[証交]" + } + kubun, _, _ := transform.String(japanese.ShiftJIS.NewEncoder(), buf) + kubunhash[hno] = kubun + } + + return kubunhash, nil +} + + +func copy_tmp(file string) error { + os.Remove(default_tmpxdw) + b, err := ioutil.ReadFile(file) + if err != nil { + return err + } + if err := ioutil.WriteFile(default_tmpxdw, b, 0644); err != nil { + return err + } + return nil +} + +func xdw2txt(file string) (txt []string) { + s := C.GoString(C.xdw2txt(C.CString(file))) + r := strings.NewReader(s) + tr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder()) + buf := bufio.NewScanner(tr) + for buf.Scan() { + txt = append(txt, buf.Text()) + } + return +} + +func print_time(t time.Time) time.Time { + now := time.Now() + if !flg_time { + return now + } + elapsed := now.Sub(t) + total := now.Sub(start) + s := fmt.Sprintf("---- Elapsed: %v (total = %v) @ %02d:%02d\n", elapsed, total, now.Hour(), now.Minute()) + fmt.Print(s) + return now +} +