Beispiel #1
0
zathura_error_t
djvu_document_save_as(zathura_document_t* document, djvu_document_t* djvu_document, const char* path)
{
  if (document == NULL || djvu_document == NULL || path == NULL) {
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

  FILE* fp = fopen(path, "w");
  if (fp == NULL) {
    return ZATHURA_ERROR_UNKNOWN;
  }

  const char* extension = get_extension(path);

  ddjvu_job_t* job = NULL;
  if (extension != NULL && g_strcmp0(extension, "ps") == 0) {
    job = ddjvu_document_print(djvu_document->document, fp, 0, NULL);
  } else {
    job = ddjvu_document_save(djvu_document->document, fp, 0, NULL);
  }
  while (ddjvu_job_done(job) != true) {
      handle_messages(djvu_document, true);
  }

  fclose(fp);

  return ZATHURA_ERROR_OK;
}
int
main(int argc, char **argv)
{
  int i;
  int optc = 0;
  char **optv;
  const char *infile = 0;
  const char *outfile = 0;
  FILE *fout;

  /* Sort options */
  if (! (optv = (char**)malloc(argc*sizeof(char*))))
    die(i18n("Out of memory"));
  for (i=1; i<argc; i++)
    {
      char *s = argv[i];
      if (s[0]=='-' && s[1]=='-')
        s = s+1;
      if (!strcmp(s,"-verbose"))
        verbose = true;
      else if (check_option(s))
        optv[optc++] = s;
      else if (s[0]=='-' && s[1])
        usage();
      else if (s[0] && !infile)
        infile = s;
      else if (s[0] && !outfile)
        outfile = s;
      else
        die(i18n("Incorrect arguments. Try option --help."));
    }
  if (! infile)
    infile = "-";
  if (! outfile)
    outfile = "-";
  /* Open document */
  if (! (ctx = ddjvu_context_create(argv[0])))
    die(i18n("Cannot create djvu context."));
  if (! (doc = ddjvu_document_create_by_filename(ctx, infile, TRUE)))
    die(i18n("Cannot open djvu document '%s'."), infile);
  while (! ddjvu_document_decoding_done(doc))
    handle(TRUE);
  /* Open output file */
  if (! strcmp(outfile,"-")) 
    {
      fout = stdout;
#if defined(__CYGWIN32__)
      setmode(fileno(fout), O_BINARY);
#elif defined(WIN32)
      _setmode(_fileno(fout), _O_BINARY);
#endif
    } 
  else if (! (fout = fopen(outfile, "wb")))
    die(i18n("Cannot open output file '%s'."), outfile);
  /* Create printing job */
  if (! (job = ddjvu_document_print(doc, fout, optc, optv)))
    die(i18n("Cannot create PostScript conversion job."));
  /* Wait until completion and cleanup */
  while (! ddjvu_job_done(job))
    handle(TRUE);
  if (verbose)
    fprintf(stderr,"\n");
  /* Make sure we get error messages */
  tryhelp = false;
  if (ddjvu_job_error(job))
    handle(FALSE);
  if (ddjvu_job_error(job))
    die(i18n("PostScript conversion job failed."));
  /* Close */
  fclose(fout);
  if (job)
    ddjvu_job_release(job);
  if (doc)
    ddjvu_document_release(doc);
  if (ctx)
    ddjvu_context_release(ctx);
  return 0;
}