summaryrefslogtreecommitdiff
path: root/support/dktools/DkWxBmepsThread.cpt
blob: 2a156112eac7dd7f0c6fecba66618027543a3355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2011-xxxx
SPDX-License-Identifier:	BSD-3-Clause


%%	header


/**	Worker thread for wxbmeps.
*/
class DkWxBmepsThread : public wxThread
{
  protected:

    /**	Conversion options.
    */
    dk3_bm_eps_options_t	bmeo;

    /**	Communication object to communicate with main thread.
    */
    DkWxCommunicator		*pComm;

    /**	Localized messages, wxChar strings.
    */
    wxChar const * const	*sTexts;

    /**	Localized messages, dkChar strings.
    */
    dkChar const * const	*msg;

    /**	Directory or file name.
    */
    wxChar const		*sDirectory;

    /**	Application helper.
    */
    DkWxAppHelper		*pHelper;

    /**	Encoding for wxChar strings.
    */
    int			 	 wxenc;

    /**	Encoding for dkChar strings.
    */
    int				 dkenc;

  public:

    /**	Constructor.
    	@param	pc	Communication object.
	@param	st	Localized messages, wxChar strings.
	@param	m	Localized messages, dkChar strings.
	@param	sd	Directory or file name.
	@param	ph	Application helper.
	@param	wxe	Encoding for wxChar strings.
	@param	dke	Encoding for dkChar strings.
	@param	bmeoptr	Conversion options.
    */
    DkWxBmepsThread(
      DkWxCommunicator		*pc,
      wxChar const * const	*st,
      dkChar const * const	*m,
      wxChar const		*sd,
      DkWxAppHelper		*ph,
      int			 wxe,
      int			 dke,
      dk3_bm_eps_options_t	*bmeoptr
    );

    /**	Destructor.
    */
    ~DkWxBmepsThread();

    /**	Check setup (memory allocations succeeded).
    	@return	Test result, true on success, false on error.
    */
    bool
    checkSetup();

    /**	Thread function.
    	@return	A pointer which is ignored.
    */
    virtual
    void *
    Entry();

    /**	Function executed at end of thread, does nothing.
    */
    virtual
    void
    OnExit();

    /**	Run for one file.
    	@param	fn	File name.
	@param	ming	Minimum value for progress bar.
	@param	maxg	Maximum value for progress bar.
    */
    void
    runForFile(wxChar const *fn, int ming, int maxg);

    /**	Run for one directory.
    	@param	dn	Directory name.
    */
    void
    runForDirectory(wxChar const *dn);

    /**	Check whether we must run for a file.
    	@param	sfn	Source file name.
	@return	true for must run, false for not necessary.
    */
    bool
    checkMustRun(wxChar const *sfn);

    /**	Get short file name.
    	@param	longName	Full file name.
	@return	Pointer to short file name on success, NULL on error.
    */
    wxChar const *
    shortFileName(wxChar const *longName);
};



%%	module

#include "dk3conf.h"
#include "wxbmeps.h"

$!trace-include



/**	File name separator.
*/
static wxChar const	dkwxbmeps_fnsep[] = {
DK3_WX_CHAR_SEP , wxT('\0')
};



static wxChar const * const	dkwxbmeps_output_suffixes[] = {
wxT(".ps"),
wxT(".eps"),
wxT(".pdf"),
wxT(".bb")
};




/**	Non-localized  keywords used by this module.
*/
static wxChar const * const	dkwxbmeps_thread_kw[] = {
$!string-table macro=wxT
#
#   0: File specification.
#
*.*
$!end
};



/**	File suffixes we can handle.
*/
static wxChar const * const	dkwxbmeps_thread_suffixes[] = {
#if DK3_HAVE_PNG_H
wxT(".png"),
#endif
#if DK3_HAVE_JPEGLIB_H
wxT(".jpg"),
wxT(".jpeg"),
#endif
#if DK3_HAVE_TIFF_H
wxT(".tif"),
wxT(".tiff"),
#endif
NULL
};



DkWxBmepsThread::DkWxBmepsThread(
      DkWxCommunicator		*pc,
      wxChar const * const	*st,
      dkChar const * const	*m,
      wxChar const		*sd,
      DkWxAppHelper		*ph,
      int			 wxe,
      int			 dke,
      dk3_bm_eps_options_t	*bmeoptr
)
{
  $? "+ DkWxBmepsThread::DkWxBmepsThread"
  pComm = pc;
  sTexts = st;
  msg = m;
  sDirectory = dk3wxs_dup_app(sd, ph->getApp());
  pHelper = ph;
  wxenc = wxe;
  dkenc = dke;
  dk3mem_cpy((void *)(&bmeo), (void *)bmeoptr, sizeof(dk3_bm_eps_options_t));
  bmeo.to_stdout = 0;
  $? "- DkWxBmepsThread::DkWxBmepsThread"
}



bool
DkWxBmepsThread::checkSetup()
{
  bool		back = false;
  $? "+ DkWxBmepsThread::checkSetup"
  if(sDirectory) {
    back = true;
  } $? "- DkWxBmepsThread::checkSetup %d", (back ? 1 : 0)
  return back;
}



DkWxBmepsThread::~DkWxBmepsThread()
{
  $? "+ DkWxBmepsThread::~DkWxBmepsThread"
  dk3_release(sDirectory);
  $? "- DkWxBmepsThread::~DkWxBmepsThread"
}



void *
DkWxBmepsThread::Entry()
{
#if 0
  /*
	Simple test thread without practical use.
  */
  int	i = 0;
  int	cc = 1;
  int	numbreak = -1;
  do {
    this->Sleep(100);
    i++;
    if(i >= 10) {
      cc = 0;
    }
    pComm->setUpdates(wxT("test-file"), (100*i));
    if(pComm->getCanContinue() == 0) {
      if(numbreak == -1) {
        numbreak = i;
      } else {
        if(i >= numbreak + 15) {
	  cc = 0;
	}
      }
    }
    pComm->addWxText(wxT("Test mit einer ganz ganz ganz ganz langen Zeile\n"));
  } while(cc);
  pComm->setRunning(0);
#else
  $? "+ DkWxBmepsThread::Entry"
  if(wxFileName::DirExists(sDirectory)) {	$? ". handle directory"
    runForDirectory(sDirectory);
  } else {					$? ". check for single file"
    if(wxFileName::FileExists(sDirectory)) {	$? ". file"
      runForFile(sDirectory, 0, 1000);
    } else {					$? "! no such file"
      /* ##### ERROR: No such file or directory! */
    }
  }
  pComm->setRunning(0); $? "- DkWxBmepsThread::Entry"
#endif
  return NULL;
}



void
DkWxBmepsThread::OnExit()
{
  /* Doing nothing. */
  $? "= DkWxBmepsThread::OnExit"
}



/**	Compare two wxChar file names.
	@param	l	Left file name.
	@param	r	Right file name.
	@param	cr	Comparison criteria (ignored).
	@return	Comparison result.
*/
static
int
dkwxbmeps_compare_filenames(void const *l, void const *r, int WXUNUSED(cr))
{
  int		back	= 0;
  if(l) {
    if(r) {
#if DK3_HAVE_FNCASEINS
      back = dk3wxs_casecmp((wxChar const *)l, (wxChar const *)r);
#else
      back = dk3wxs_cmp((wxChar const *)l, (wxChar const *)r);
#endif
      if(back < -1) back = -1;
      if(back >  1) back =  1;
    } else {
      back = 1;
    }
  } else {
    if(r) {
      back = -1;
    }
  }
  return back;
}



void
DkWxBmepsThread::runForFile(wxChar const *fn, int ming, int maxg)
{
  dkChar	 sfn[DK3_MAX_PATH];
  wxChar const	*shortname;
  $? "+ DkWxBmepsThread::runForFile \"%ls\"", fn
  shortname = shortFileName(fn);
  if(dk3wxs_to_dkstr(
      sfn,
      DK3_SIZEOF(sfn,dkChar),
      pHelper->getDkEncoding(),
      fn,
      pHelper->getWxEncoding()
    )
  )
  {
    /* PROGRESS MESSAGE Start */
    pComm->addWxText(sTexts[114]);
    pComm->addWxText(shortname);
    pComm->addWxText(sTexts[115]);
    pComm->addWxText(sTexts[110]);
    (void)dk3bm_process_file_name(
      &bmeo,
      sfn,
      DK3_BIF_IMAGE_TYPE_UNKNOWN,
      ming,
      maxg,
      (void *)pComm
    );
    /* PROGRESS MESSAGE Finished */
#if 0
    pComm->addWxText(sTexts[116]);
    pComm->addWxText(shortname);
    pComm->addWxText(sTexts[117]);
    pComm->addWxText(sTexts[110]);
#endif
    pComm->addWxText(sTexts[110]);
  } else { $? "! failed to convert file name to dkChar string"
    /* ERROR: Failed to convert file name! */
    pComm->addWxText(sTexts[118]);
    pComm->addWxText(shortname);
    pComm->addWxText(sTexts[119]);
    pComm->addWxText(sTexts[110]);
    pComm->setLogLevel(DK3_LL_ERROR);
  }
  pComm->setUpdates(NULL, maxg);
  $? "- DkWxBmepsThread::runForFile"
}






void
DkWxBmepsThread::runForDirectory(wxChar const *dn)
{
  wxChar		namein[DK3_MAX_PATH];
  wxChar const		*currentfn;
  wxChar const		*cfilename;
  wxChar		*suffix;
  dk3_sto_t		*sFiles;
  dk3_sto_it_t		*iFiles;
  size_t		fnlgt;
  int			namesFound;
  int			i;
  int			promille;
  int			cc;

  $? "+ DkWxBmepsThread::runForDirectory \"%ls\"", dn
  pComm->addWxText(sTexts[106]);
  pComm->addWxText(dn);
  pComm->addWxText(sTexts[107]);
  pComm->addWxText(sTexts[110]);
  pComm->addWxText(sTexts[110]);
  sFiles = dk3sto_open_app(pHelper->getApp());
  if(sFiles) {
    dk3sto_set_comp(sFiles, dkwxbmeps_compare_filenames, 0);
    iFiles = dk3sto_it_open(sFiles);
    if(iFiles) {
      /*
      	Find all input filename candidates.
      */
      namesFound = 0;
      {
        wxLogNull	lognull;
	bool		canContinue;
        wxDir directory(sDirectory);
	if(directory.IsOpened()) {
	  wxString	filename;
	  canContinue = directory.GetFirst(
	    &filename, dkwxbmeps_thread_kw[0], wxDIR_FILES
	  );
	  while(canContinue) {
	    wxCStrData filenamestrdata = filename.c_str();
	    cfilename = (wxChar const *)filenamestrdata;
	    if(cfilename) {
	      if(dk3wxs_len(cfilename) < DK3_SIZEOF(namein,wxChar)) {
	        dk3wxs_cpy(namein, cfilename);
		suffix = dk3wxs_get_suffix(namein);
		if(suffix) {
		  if(dk3wxs_array_index(dkwxbmeps_thread_suffixes,suffix,0)
		     > -1
		  )
		  {
		    suffix = dk3wxs_dup_app(namein, pHelper->getApp());
		    if(suffix) {
		      if(dk3sto_add(sFiles, suffix)) {
		        namesFound++; $? ". registered \"%ls\"", suffix
		      } else {
		        /* ERROR: Memory */
			dk3_release(suffix);
			pComm->addWxText(sTexts[105]);
			pComm->addWxText(sTexts[110]);
			pComm->setLogLevel(DK3_LL_ERROR);
		      }
		    } else {
		      /* ERROR: Memory */
		      pComm->addWxText(sTexts[105]);
		      pComm->addWxText(sTexts[110]);
		      pComm->setLogLevel(DK3_LL_ERROR);
		    }
		  }
		}
	      } else {
	        /* ERROR: Name too long! */
		pComm->addWxText(sTexts[111]);
		pComm->addWxText(cfilename);
		pComm->addWxText(sTexts[112]);
		pComm->addWxText(sTexts[110]);
		pComm->setLogLevel(DK3_LL_ERROR);
	      }
	    }
	    canContinue = directory.GetNext(&filename);
	  }
	}
      }
      pComm->setUpdates(NULL, 50);
      /*
      	Process all input file name candidates.
      */
      if(namesFound > 0) {
        dk3sto_it_reset(iFiles);
	i = 0;
#if VERSION_BEFORE_20150821
	/* Immediately set to 0 at loop begin */
	cc = 1;
#endif
	do {
	  cc = 0;
	  cfilename = (wxChar const *)dk3sto_it_next(iFiles);
	  if(cfilename) {
	    cc = 1;
	    promille = 50 + (950 * i )/namesFound;
	    if(promille >= 1000) { promille = 999; }
	    pComm->setUpdates(cfilename, promille);
	    $? ". file \"%ls\"", cfilename
	    fnlgt = dk3wxs_len(sDirectory);
	    fnlgt += dk3wxs_len(cfilename);
	    fnlgt += 1;
	    if(fnlgt < DK3_SIZEOF(namein,wxChar)) {
	      dk3wxs_cpy(namein, sDirectory);
	      dk3wxs_cat(namein, dkwxbmeps_fnsep);
	      dk3wxs_cat(namein, cfilename);
	      if(checkMustRun(namein)) {
	        runForFile(
	          namein,
		  50 + (950 * i )/namesFound,
		  50 + (950 * (i + 1))/namesFound
	        );
	      }
	    } else {
	      /* ERROR: File name too long for buffer */
	      pComm->addWxText(sTexts[111]);
	      pComm->addWxText(sDirectory);
	      pComm->addWxText(dkwxbmeps_fnsep);
	      pComm->addWxText(cfilename);
	      pComm->addWxText(sTexts[112]);
	      pComm->addWxText(sTexts[110]);
	      pComm->setLogLevel(DK3_LL_ERROR);
	    }
	    i++;
	    promille = 50 + (950 * i )/namesFound;
	    if(promille >= 1000) { promille = 999; }
	    pComm->setUpdates(cfilename, promille);
	    if(!(pComm->getCanContinue())) {
	      cc = 0;
	    }
	  }
	} while(cc);
	pComm->setUpdates(cfilename, 1000);
      } else {
        /* ERROR: No names found! */
	pComm->addWxText(sTexts[113]);
	pComm->addWxText(sTexts[110]);
	pComm->setLogLevel(DK3_LL_ERROR);
      }
      /*
      	Release the file names.
      */
      dk3sto_it_reset(iFiles);
      while((currentfn = (wxChar const *)dk3sto_it_next(iFiles)) != NULL) {
        dk3_release(currentfn);
      }
      dk3sto_it_close(iFiles);
    } else {
      pComm->addWxText(sTexts[105]);
      pComm->addWxText(sTexts[110]);
      pComm->setLogLevel(DK3_LL_ERROR);
    }
    dk3sto_close(sFiles);
  } else {
    pComm->addWxText(sTexts[105]);
    pComm->addWxText(sTexts[110]);
    pComm->setLogLevel(DK3_LL_ERROR);
  }
  pComm->addWxText(sTexts[108]);
  pComm->addWxText(dn);
  pComm->addWxText(sTexts[109]);
  pComm->addWxText(sTexts[110]);
  $? "- DkWxBmepsThread::runForDirectory"
}



bool
DkWxBmepsThread::checkMustRun(wxChar const *sfn)
{
  wxChar	nameout[DK3_MAX_PATH];
  wxChar	*suffix;
  wxChar const	*nsuffix;
  bool		back	= true;
  if(bmeo.make) {
    dk3wxs_cpy(nameout, sfn);
    suffix = dk3wxs_get_suffix(nameout);
    if(suffix) {
      *suffix = wxT('\0');
      nsuffix = NULL;
      switch(bmeo.dr) {
        case DK3_BMEPS_DRIVER_PS: {
	  nsuffix = dkwxbmeps_output_suffixes[0];
	} break;
	case DK3_BMEPS_DRIVER_EPS: {
	  nsuffix = dkwxbmeps_output_suffixes[1];
	} break;
	case DK3_BMEPS_DRIVER_PDF: {
	  nsuffix = dkwxbmeps_output_suffixes[2];
	} break;
	case DK3_BMEPS_DRIVER_BB: {
	  nsuffix = dkwxbmeps_output_suffixes[3];
	} break;
      }
      if(nsuffix) {
        if((dk3wxs_len(nameout) + dk3wxs_len(nsuffix))
	   < DK3_SIZEOF(nameout,wxChar)
	)
	{
	  dk3wxs_cat(nameout, nsuffix);
	  if(wxFileName::FileExists(sfn)) {
	    if(wxFileName::FileExists(nameout)) {
	      wxFileName	fni(sfn);
	      wxFileName	fno(nameout);
	      if(fno.GetModificationTime() > fni.GetModificationTime()) {
	        back = false;
	      }
	    }
	  } else {
	    /* ##### BUG: Source file does not exist, should not happen. */
	  }
	} else {
	  /* ##### ERROR: Name too long! */
	}
      } else {		$? "! Bug, illegal output driver"
        /* BUG: Illegal output driver, should not happen. */
      }
    }
  }
  return back;
}



wxChar const *
DkWxBmepsThread::shortFileName(wxChar const *longName)
{
  wxChar const	*back	= NULL;
  wxChar const	*ptr;

  if(longName) {
    ptr = longName;
    while(*ptr) {
      if(*ptr == DK3_WX_CHAR_SEP) {
        back = ptr;
      }
      ptr++;
    }
    if(back) { back++; }
    else { back = longName; }
  }
  return back;
}