在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例Android手机应用开发 → c++ postscript转txt 实例

c++ postscript转txt 实例

Android手机应用开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.04M
  • 下载次数:14
  • 浏览次数:195
  • 发布时间:2016-08-11
  • 实例类别:Android手机应用开发
  • 发 布 人:crax
  • 文件格式:.gz
  • 所需积分:2
 相关标签: c++ c

实例介绍

【实例简介】
【实例截图】

【核心代码】

/* Copyright (C) 1995-1998, Digital Equipment Corporation.    */
/* All rights reserved.                                       */
/* See the file pstotext.txt for a full description.          */
/* Last modified on Fri Jan 09 21:17:00 AEST 2004 by rjl      */
/*      modified on Sat Jun 02 15:04:00 AEST 2001 by rjl      */
/*      modified on Fri Oct 16 16:27:54 PDT 1998 by mcjones   */
/*      modified on Thu Nov 16 13:33:13 PST 1995 by deutsch   */
/*
 * Modifications by rjl:
 *      Use mkstemp not tempnam.
 *      ANSI C Function prototypes.
 *      Fixed const warnings.
 *      Applied debian pstotext-1.8g-6 patches.
 *
 * Modified on 27-MAY-1998 13:08 by Hunter Goatley
 *      Ported to VMS.  Various minor changes to allow it to work on
 *      both VAX and Alpha (VAX C and DEC C).  VMS pipes don't work
 *      right, so the GS output is dumped to a temporary file that's
 *      read, instead of reading from pipes (which is, of course, how
 *      VMS implements pipes anyway).  Also added -output option.
 */

#ifdef VMS
#include "vms.h"
#else
#include <unistd.h>
#include <sys/param.h>
#include <sys/wait.h>
#endif

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bundle.h"
#include "ocr.h"
#include "rot270.h"
#include "rot90.h"
#include "ptotdll.h"

#define BOOLEAN int
#define FALSE 0
#define TRUE 1

#define LINELEN 2000 /* longest allowable line from gs ocr.ps output */

extern BUNDLE ocr, rot270, rot90;

static BOOLEAN cork = FALSE;
static BOOLEAN debug = FALSE;
static const char *gs_cmd = "gs";
static const char *outfile = "";

static char *cmd; /* = argv[0] */

static enum {
  portrait,
  landscape,
  landscapeOther} orientation = portrait;

static BOOLEAN bboxes = FALSE;

static int explicitFiles = 0; /* count of explicit file arguments */

static void usage(void) {
  fprintf(stderr, "pstotext 1.9 of 2003-01-09\n");
  fprintf(stderr, "Copyright (C) 1995-1998, Digital Equipment Corporation.\n");
  fprintf(stderr, "Modified by Ghostgum Software Pty Ltd.\n");
  fprintf(stderr, "Comments to {mcjones,birrell}@pa.dec.com\n\n");
#ifdef VMS
  fprintf(stderr, "VMS Comments to goathunter@madgoat.com\n\n");
#endif
  fprintf(stderr, "Usage: %s [option|file]...\n", cmd);
  fprintf(stderr, "Options:\n");
  fprintf(stderr, "  -cork            assume Cork encoding for dvips output\n");
  fprintf(stderr, "  -landscape       rotate 270 degrees\n");
#ifdef VMS
  fprintf(stderr, "  -landscapeother  rotate 90 degrees\n");
#else
  fprintf(stderr, "  -landscapeOther  rotate 90 degrees\n");
#endif
  fprintf(stderr, "  -portrait        don't rotate (default)\n");
  fprintf(stderr, "  -bboxes          output one word per line with bounding box\n");
  fprintf(stderr, "  -debug           show Ghostscript output and error messages\n");
  fprintf(stderr, "  -gs \"command\"    Ghostscript command\n");
  fprintf(stderr, "  -                read from stdin (default if no files specified)\n");
  fprintf(stderr, "  -output file     output results to \"file\" (default is stdout)\n");
}

static char *make_temp(BUNDLE b) {
  /* Return pathname of temporary file containing bundle "b".  Caller
     should unlink file (and, technically, free pathname). */
  FILE *f;
  char *path = NULL;
#ifdef VMS
  path = tempnam("SYS$SCRATCH:", ".ps2t");
#else
  const char *pattern = "/tmp/ps2tXXXXXX";
  char *templ = (char*)malloc(strlen(pattern) 1);
  int fd;

  strcpy(templ, pattern);
  fd = mkstemp(templ);
  if (fd == -1) {
fprintf(stderr, "mkstemp() failed");
exit(1);
  }
  close(fd);
  path = (char*)malloc(strlen(templ) 1);
  strcpy(path, templ);
#endif
  f = fopen(path, "w");
  if (f==NULL) {perror(cmd); exit(1);}
  putbundle(b, f);
  fclose(f);
  return path;
}

static char *ocr_path = NULL, *rotate_path = NULL;
static FILE *gs = NULL;
static void *instance; /* pstotext state */
#ifdef VMS
static char *cmdfile = NULL, *gsoutfile = NULL;
#endif

static int cleanup(void) {
  int gsstatus, status = 0;
  pstotextExit(instance);
  if (gs!=NULL) {
#ifdef VMS
    gsstatus = fclose(gs);
#else
    gsstatus = pclose(gs);
#endif
    if (WIFEXITED(gsstatus)) {
      if (WEXITSTATUS(gsstatus)!=0) status = 3;
      else if (WIFSIGNALED(gsstatus)) status = 4;
    }
  }
  if ((rotate_path!=NULL) && (strcmp(rotate_path, "")!=0)) {
    unlink(rotate_path);
    free(rotate_path);
    rotate_path = NULL;
  }
  if (ocr_path!=NULL) {
    unlink(ocr_path);
    free(ocr_path);
    ocr_path = NULL;
  }
#ifdef VMS
  if (cmdfile!=NULL) unlink(cmdfile);
  if (gsoutfile!=NULL) unlink(gsoutfile);
#endif
  return status;
}

static void handler(int x) {
  int status = cleanup();
  if (status!=0) exit(status);
#ifdef VMS
  exit(1);
#else
  exit(2);
#endif
}

static int do_it(char *path) {
  /* If "path" is NULL, then "stdin" should be processed. */
  char *gs_cmdline;
  char *input;
  int status;
  char norotate[] = "";
  FILE *fileout;
#ifdef VMS
  FILE *cfile;
#endif

  fileout = stdout;
  if (strlen(outfile) != 0) {
#ifdef VMS
     fileout = fopen(outfile, "w", "rfm=var","rat=cr");
#else
     fileout = fopen(outfile, "w");
#endif /* VMS */
     if (fileout == NULL) {perror(cmd); exit(1);}
  }

  signal(SIGINT, handler);
  signal(SIGHUP, handler);

  ocr_path = make_temp(ocr);

  switch (orientation) {
  case portrait: rotate_path = norotate; break;
  case landscape: rotate_path = make_temp(rot270); break;
  case landscapeOther: rotate_path = make_temp(rot90); break;
  }
  if ((ocr_path == NULL) || (rotate_path == NULL)) {
    fprintf(stderr,"No memory available\n");
    cleanup();
    exit(1);
  }

  if (path==NULL) {
    input = (char*)malloc(2);
    if (input == NULL) {
      fprintf(stderr,"No memory available\n");
      cleanup();
      exit(1);
    }
    strcpy(input, "-");
  } else {
    input = (char*)malloc(strlen(path) 6);
    if (input == NULL) {
      fprintf(stderr,"No memory available\n");
      cleanup();
      exit(1);
    }
    strcpy(input, "-- '"); strcat(input, path); strcat(input, "'");
  }

  gs_cmdline = (char*)malloc(strlen(gs_cmd) strlen(rotate_path)
strlen(ocr_path) strlen(input) 128);

  if (gs_cmdline == NULL) {
    fprintf(stderr, "No memory available\n");
    cleanup();
    exit(1);
  }

  sprintf(
    gs_cmdline,
#ifdef VMS
    "%s -r72 \"-dNODISPLAY\" \"-dFIXEDMEDIA\" \"-dDELAYBIND\" \"-dWRITESYSTEMDICT\" %s \"-dNOPAUSE\" %s %s %s",
#else
    "%s -r72 -dNODISPLAY -dFIXEDMEDIA -dDELAYBIND -dWRITESYSTEMDICT %s -dNOPAUSE %s %s %s",
#endif
    gs_cmd,
    (debug ? "" : "-q"),
    rotate_path,
    ocr_path,
    input
    );
  if (debug) fprintf(stderr, "%s\n", gs_cmdline);
#ifdef VMS
  cmdfile = tempnam("SYS$SCRATCH:","PS2TGS");
  gsoutfile = tempnam("SYS$SCRATCH:","GSRES");
  if ((cfile = fopen(cmdfile,"w")) == NULL) {perror(cmd);exit(1);}
  fprintf (cfile, "$ define/user sys$output %s\n", gsoutfile);
  fprintf (cfile, "$ %s\n", gs_cmdline);
  fprintf (cfile, "$ deletee/nolog %s;*\n", cmdfile);
  fputs ("$ exit\n", cfile);
  fclose (cfile);
  sprintf(gs_cmdline, "@%s.", cmdfile);
  system(gs_cmdline);
  if ((gs = fopen(gsoutfile, "r")) == NULL) {
fprintf(stderr, "Error opening output file %s from GS command\n", gsoutfile);
perror(cmd);
exit(1);
  }
#else
  gs = popen(gs_cmdline, "r");
  if (gs==0) {perror(cmd); exit(1);}
#endif
  status = pstotextInit(&instance);
  if (status!=0) {
    fprintf(stderr, "%s: internal error %d\n", cmd, status);
    exit(5);
  }
  if (cork) {
    status = pstotextSetCork(instance, TRUE);
    if (status!=0) {
      fprintf(stderr, "%s: internal error %d\n", cmd, status);
      exit(5);
    }
  }
  while (TRUE) {
    char line[LINELEN];
    const char *pre, *word, *post;
    int llx, lly, urx, ury;
    if (fgets(line, LINELEN, gs)==NULL) break;
    if (debug) fputs(line, stderr);
    status = pstotextFilter(
      instance, line, &pre, &word, &post, &llx, &lly, &urx, &ury);
    if (status!=0) {
      fprintf(stderr, "%s: internal error %d\n", cmd, status);
      exit(5);
    }
    if (word!=NULL) {
      if (!bboxes) {
        fputs(pre, fileout); fputs(word, fileout); fputs(post, fileout);
        if ( debug ) fputc('\n', stderr);
      }
      else {
if (pre) {
   if (*pre == ' ')
pre ;
   fputs(pre, fileout);
}
        fprintf(fileout, "%6d\t%6d\t%6d\t%6d\t%s\n", llx, lly, urx, ury, word);
if (post)
   fputs(post, fileout);
      }
    }
  }
  if (fileout != stdout) fclose(fileout);
  status = cleanup();
  if (status!=0) exit(status);
  return status;
}

int main(int argc, char *argv[]) {
  int i;
  char *arg;
  cmd = argv[0];
  for (i = 1; i<argc; i ) {
    arg = argv[i];
    if (strcasecmp(arg, "-landscape")==0) orientation = landscape;
    else if (strcasecmp(arg, "-cork")==0) cork = TRUE;
    else if (strcasecmp(arg, "-landscapeOther")==0) orientation = landscapeOther;
    else if (strcasecmp(arg, "-portrait")==0) orientation = portrait;
    else if (strcasecmp(arg, "-bboxes")==0) bboxes = TRUE;
    else if (strcasecmp(arg, "-debug")==0) debug = TRUE;
    else if (strcasecmp(arg, "-gs")==0) {
      i ;
      if (i>=argc) {usage(); exit(1);}
      gs_cmd = argv[i];
    }
    else if (strcasecmp(arg, "-output")==0) {
      i ;
      if (i>=argc) {usage(); exit(1);}
      outfile = argv[i];
    }
    else if (strcmp(arg, "-")==0) do_it(NULL);
    else if (arg[0] == '-') {usage(); exit(1);}
    else /* file */ {
      explicitFiles ;
      do_it(arg);
    }
  }
  if (explicitFiles==0) do_it(NULL);
  exit(0);
}



标签: c++ c

实例下载地址

c++ postscript转txt 实例

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警