view src/kaigo/qtuti/95_tmpl.cpp @ 65:0369656be06c default tip

many changes.
author pyon@macmini
date Fri, 20 May 2022 06:30:34 +0900
parents 49656dc40069
children
line wrap: on
line source

//
// 95.cpp: Qfuhi Tsuchi
//  Last Change: 2020-09-08 火 15:20:43.
//

#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
#define ATN_N     20

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)) {
		fprintf(stderr, "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;
}

void xdw2txtb(const char* xdwfile, const char* txtfile) {
	char in_path[_MAX_PATH], out_path[_MAX_PATH];
    _fullpath(in_path, xdwfile, _MAX_PATH);
    _fullpath(out_path, txtfile, _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)) {
		fprintf(stderr, "Error: cannot open %s\n", xdwfile);
		return;
	}

	XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0}; // 総ページ数を得る
	XDW_GetDocumentInformation(h, &info);
    int nPage = info.nPages;

	FILE *fp;
	if ((fp = fopen(out_path, "w")) == NULL) {
		fprintf(stderr, "Error: cannot open %s\n", out_path);
		return;
	}

	long datasize;
    char* lpszvalue;

    for (int i = 1; i <= nPage; i++) {
        datasize = XDW_GetPageTextToMemory(h, i, NULL, 0, NULL);
        lpszvalue = (char*)malloc(sizeof(char)*datasize);
        XDW_GetPageTextToMemory(h, i, lpszvalue, datasize, NULL);
        fprintf(fp, "%s\n", lpszvalue);
        free(lpszvalue);
    }

	fclose(fp);
	XDW_CloseDocumentHandle(h, NULL); // 文書ハンドルを閉じる
	return;
}

int xdwsplit1(const char* file, const char* workdir, const char* prefix) {
	char file_path[_MAX_PATH];
    _fullpath(file_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(file_path, &h, (XDW_OPEN_MODE*)&mode)) {
		fprintf(stderr, "Error: cannot open %s\n", file);
		return -1;
	}

	XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0};
	XDW_GetDocumentInformation(h, &info);
    int nPage = info.nPages;

	char buf[_MAX_PATH];
    for (int i = 1; i <= nPage; i++) {
		sprintf(buf, "%s/%s%05d.xdw", workdir, prefix, i);
		//sprintf(buf, "%s_%05d.xdw", prefix, i);
		_fullpath(file_path, buf, _MAX_PATH);

		int api_result = XDW_GetPage(h, i, file_path, NULL);
		if (api_result < 0) {
			fprintf(stderr, "XDW Error: cannot get page (%s p=%d)\n", file, i);
			return -1;
		}
	}

	XDW_CloseDocumentHandle(h, NULL);
	return 0;
}

int xdwextpage(const char* infile, const int p, const char* outfile) {
	char file_path[_MAX_PATH];
    _fullpath(file_path, infile, _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(file_path, &h, (XDW_OPEN_MODE*)&mode)) {
		fprintf(stderr, "Error: cannot open %s\n", infile);
		return -1;
	}

	XDW_DOCUMENT_INFO info = {sizeof(XDW_DOCUMENT_INFO), 0, 0, 0};
	XDW_GetDocumentInformation(h, &info);
    int nPage = info.nPages;

	char buf[_MAX_PATH];
	_fullpath(file_path, outfile, _MAX_PATH);

	int api_result = XDW_GetPage(h, p, file_path, NULL);
	if (api_result < 0) {
		fprintf(stderr, "XDW Error: cannot get page (%s p=%d)\n", infile, p);
		return -1;
	}

	XDW_CloseDocumentHandle(h, NULL);
	return 0;
}

void xdwmerge(const char* list, const char* output) {
	FILE *fp;

	if ((fp = fopen(list, "r")) == NULL) {
		fprintf(stderr, "XDW Error: can't open file [%s]\n", list);
		exit(1);
	}

	char *all_path = (char*)malloc(MAXLINE * sizeof(char) * _MAX_PATH);

	if (all_path == NULL) {
		fprintf(stderr, "XDW Error: can't allocate memory\n");
		exit(1);
	}

	int n = 0;
	char *q;
	char buf[_MAX_PATH];

	while (fgets(buf, sizeof buf, fp)) {
		if ((q = strchr(buf, '\n')) != NULL) {
			*q = '\0';
		}
		_fullpath(buf, buf, _MAX_PATH);
		strncpy(&all_path[n * _MAX_PATH], buf, _MAX_PATH);
		n++;
	}
	fclose(fp);

	char *blk_path = (char*)malloc(BLOCKSZ * sizeof(char) * _MAX_PATH);
	const char **blk_path_addr = (const char**)malloc((n / BLOCKSZ + 1) * sizeof(char*) * _MAX_PATH);
	if (blk_path == NULL || blk_path_addr == NULL) {
		fprintf(stderr, "XDW Error: can't allocate memory\n");
		exit(1);
	}

	// process by block
	int api_result;
	int bn = 0;
	for (int p = 0, m = 0; p < n; p++) {
		m = p % BLOCKSZ;
		if (m == 0 && p > 0) {
			sprintf(buf, "tmp_b%04d.xdw", ++bn);
			_fullpath(buf, buf, _MAX_PATH);
			api_result = XDW_MergeXdwFiles(blk_path_addr, BLOCKSZ, buf, NULL);
			if (api_result < 0) {
				fprintf(stderr, "XDW Error: can't merge [1] (p = %d, m = %d)\n", p, m);
				exit(1);
			}
		} 
		strncpy(&blk_path[m * _MAX_PATH], &all_path[p * _MAX_PATH], _MAX_PATH);
		blk_path_addr[m] = &blk_path[m * _MAX_PATH];
	}

	sprintf(buf, "tmp_b%04d.xdw", ++bn);
	_fullpath(buf, buf, _MAX_PATH);

	int mod = n % BLOCKSZ;
	if (mod == 0) mod = BLOCKSZ; 
	api_result = XDW_MergeXdwFiles(blk_path_addr, mod, buf, NULL);

	if (api_result < 0) {
		fprintf(stderr, "XDW Error: can't merge [2]\n");
		exit(1);
	}

	// merge blocks
	for (int b = 0; b < bn; b++) {
		sprintf(buf, "tmp_b%04d.xdw", b + 1);
		_fullpath(buf, buf, _MAX_PATH);
		strncpy(&blk_path[b * _MAX_PATH], buf, _MAX_PATH);
		blk_path_addr[b] = &blk_path[b * _MAX_PATH];
	}

	_fullpath(buf, output, _MAX_PATH );
	api_result = XDW_MergeXdwFiles(blk_path_addr, bn, buf, NULL);

	if (api_result < 0) {
		fprintf(stderr, "XDW Error: can't merge [3]\n");
		exit(1);
	}

	free(all_path);
	free(blk_path);
	free(blk_path_addr);

	for (int b = 0; b < bn; b++) {
		sprintf(buf, "tmp_b%04d.xdw", b + 1);
		_fullpath(buf, buf, _MAX_PATH);
		remove(buf);
	}
}

int xdwaddatn(const char* xdwfile, const char* atnfile) {
	FILE *fp;
	char filepath[_MAX_PATH];
	_fullpath(filepath, atnfile, _MAX_PATH);

	if ((fp = fopen(filepath, "r")) == NULL) {
		fprintf(stderr, "Error: cannot open %s\n", filepath);
		return -1;
	}

	char keyword[128];
	char *q;
	fgets(keyword, sizeof keyword, fp);
	if ((q = strchr(keyword, '\n')) != NULL) {
		*q = '\0';
	}

	char buf[_MAX_PATH];
	int x[ATN_N], y[ATN_N], sz[ATN_N], tr[ATN_N];
	char txt[ATN_N][256];
	int an = 0;
	while (fgets(buf, sizeof buf, fp)) {
		if ((q = strchr(buf, '\n')) != NULL) {
			*q = '\0';
		}

		x[an]   = atoi(strtok(buf,  ","));
		y[an]   = atoi(strtok(NULL, ","));
		sz[an]  = atoi(strtok(NULL, ","));
		tr[an]  = atoi(strtok(NULL, ","));
		strcpy(txt[an], strtok(NULL, ","));
		an++;
		//printf("x=%d y=%d txt=%s sz=%d tr=%d\n", x[an], y[an], txt[an], sz[an], tr[an]);
	}
	fclose(fp);


	XDW_DOCUMENT_HANDLE h = NULL;
	XDW_OPEN_MODE_EX mode = {sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE};

	_fullpath(filepath, xdwfile, _MAX_PATH);
	int api_result = XDW_OpenDocumentHandle(filepath, &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++) {
		if (keyword[0] != '\0') {
			api_result = XDW_FindTextInPage(h, i + 1, keyword, NULL, &pFoundHandle, NULL);
			if (!pFoundHandle) {
				XDW_CloseFoundHandle(pFoundHandle);
				continue;
			}
			XDW_CloseFoundHandle(pFoundHandle);
		}
		for (int j = 0; j < an; j++ ) {
			XDW_ANNOTATION_HANDLE annoation;
			int api_result = XDW_AddAnnotation(h, XDW_AID_TEXT, i + 1, x[j], y[j], NULL, &annoation, NULL);
			if (api_result < 0) return api_result;

			api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, txt[j], 0, NULL);
			api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, (char*)&sz[j], 0, NULL);

			int color = XDW_COLOR_WHITE;
			if (tr[j]) {
				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);

	return 0;
}

int xdwaddatntool(const char* xdwfile, const char* toolfile, int n, int x, int y) {
	char filepath[_MAX_PATH];
	_fullpath(filepath, xdwfile, _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(filepath, &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);

	_fullpath(filepath, toolfile, _MAX_PATH);
	for (int i = 0; i < info.nPages; i++) {
		XDW_ANNOTATION_HANDLE annoation;
		int api_result = XDW_AddAnnotationFromAnnFile(h, filepath, n, i + 1, NULL, x, y, &annoation, NULL);
	}

	XDW_SaveDocument(h, NULL);
	XDW_CloseDocumentHandle(h, NULL);

	return 0;
}