view src/kaigo/fwgo/fw_tmpl.cpp @ 57:05f3d51ad966

add fwgo.
author pyon@macmini
date Wed, 15 Jul 2020 18:18:24 +0900
parents
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <windows.h>
#include <xdw_api.h>
#include <xdwapian.h>

#define MAXLINE 12000
#define BLOCKSZ   128

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

	int api_result = XDW_GetFullText(h, txtfile, NULL); 
	if (api_result < 0) {
		printf("Error: cannot write text\n");
		return;
	}
	XDW_CloseDocumentHandle(h, NULL);
}

void xdwsplit1(const char* xdwfile) {
	char in_path[_MAX_PATH];
    _fullpath(in_path, xdwfile, _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", xdwfile);
		return;
	}

	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, "fwtmp_%05d.xdw", i);
		_fullpath(in_path, buf, _MAX_PATH);

		int api_result = XDW_GetPage(h, i, in_path, NULL);
		if (api_result < 0) {
			printf("XDW Error: cannot get page\n");
			return;
		}
	}
	XDW_CloseDocumentHandle(h, NULL);
}

void xdwmerge(const char* list, const char* output) {
	
	FILE *fp;
	if ((fp = fopen(list, "r")) == NULL) {
		fprintf(stderr, "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, "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, "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, "fwtmp_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, "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, "fwtmp_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, "can't merge [2]\n");
		exit(1);
	}

	// merge blocks
	for (int b = 0; b < bn; b++) {
		sprintf(buf, "fwtmp_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, "can't merge [3]\n");
		exit(1);
	}

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

int xdwaddannotation(XDW_DOCUMENT_HANDLE h, int page, int x, int y, char* string, int* sz, int tr)
{
	XDW_ANNOTATION_HANDLE annoation;
	int api_result = XDW_AddAnnotation(h, XDW_AID_TEXT, page, x, y, NULL, &annoation, NULL);
	if (api_result < 0) {
		fprintf(stderr, "can't make annotation\n");
		return -1;
	}

	api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, string, 0, NULL);
	api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, (char*)(sz), 0, NULL);
    if (tr) {
		int color = XDW_COLOR_NONE;
		api_result = XDW_SetAnnotationAttribute(h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, (char*)(&color), 0, NULL);
    }
	return 0;
}

void xdwaddpage(const char* file, int sp, int atena) {
	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(file, &h, (XDW_OPEN_MODE*)&mode);
	if (api_result < 0) {
		fprintf(stderr, "can't open file\n");
		exit(1);
	}

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

    int sz = 80;
    int tr = 1;
    char pagenum[10];

    for (int p = 0; p < last_page; p++) {
        sprintf(pagenum, "FW-%05d", p + sp);
        api_result = xdwaddannotation(h, p + 1, 8598, 335, pagenum, &sz, tr);
		if (atena) api_result = xdwaddannotation(h, p + 1, 1270, 23615, pagenum, &sz, tr);
        if (api_result < 0) break;
    }

	if (api_result >= 0) api_result = XDW_SaveDocument(h, NULL);

	XDW_CloseDocumentHandle(h, NULL);
}