Back
1 //------------------------------------------------------------------------------
2 // Module: ThrConvert.cpp //
3 // //
4 // TThread derivate that calls TConv2HTML::Convert() in its Execute //
5 // function //
6 // //
7 // Copyright (c) 2000-2005 by Lars Haendel //
8 // home: http://www.newty.de //
9 // //
10 // This program is free software and can be used under the terms of the //
11 // GNU licence. See header-file for further information and disclaimer. //
12 // //
13 //------------------------------------------------------------------------------
14
15
16
17 #include <vcl.h>
18 #pragma hdrstop
19
20 #include "ThrConvert.h"
21 #pragma package(smart_init)
22
23 #include <vcl/filectrl.hpp> // due to: MinimizeName()
24 #include "Conv2HTML.h"
25
26
27
28 //----------------------------------------------------------------------------------------------------------------------
29 // constructor
30 __fastcall ThreadFunc::ThreadFunc(bool CreateSuspended, TFMain* _form) : TThread(CreateSuspended)
31 {
32 Priority = tpNormal;
33 FreeOnTerminate = true;
34 OnTerminate = &OnFinish;
35
36 form = _form;
37 }
38
39
40 //----------------------------------------------------------------------------------------------------------------------
41 // convert all files of the filelists via TConv2HTML::Convert() to Html
42 void __fastcall ThreadFunc::Execute()
43 {
44 //-------------------------------------------------------------------------------------------------------------------
45 // 1. create conversion object
46 TConv2HTML* conv = new TConv2HTML(form->style, form->lang, &(form->project));
47
48
49 //-------------------------------------------------------------------------------------------------------------------
50 // 2. estimate file sizes
51 size = new int[form->project.nFiles()]; // array to store file sizes
52
53 for(int i=0;i<form->project.nFiles();i++) // for each file in file list
54 {
55 // get filename
56 const char* szSrcFileName = form->project.GetSourceFileName(i);
57
58 // open file and determine file size
59 ifstream srcFile(szSrcFileName, ios::in);
60
61 srcFile.seekg(0L, ios::end); // seek end of file
62 size[i] = srcFile.tellg(); // estimate size
63
64 form->toDo += size[i]; // sum up file sizes
65 }
66
67
68 //-------------------------------------------------------------------------------------------------------------------
69 // 3. convert all files
70 for(fileId=0;fileId<form->project.nFiles();fileId++) // for each file in file list
71 {
72 // get filenames
73 const char* szSrcFileName = form->project.GetSourceFileName(fileId);
74 const char* szDesFileName = form->project.GetDestinationFileName(fileId);
75
76 // try to open input and output files and check success
77 ifstream src(szSrcFileName, ios::in);
78 ofstream des(szDesFileName, ios::out);
79
80
81 // if file open fails: display message
82 if(!des)
83 {
84 msg = "Error: Unable to open output file '" + (AnsiString) szDesFileName + "'";
85 Synchronize(AddMessage);
86 }
87 else
88 if(!src)
89 {
90 msg = "Error: Unable to open input file '" + (AnsiString) szSrcFileName + "'";
91 Synchronize(AddMessage);
92 }
93
94 // else: convert file
95 else
96 {
97 // a) (re-)set progress display
98 Synchronize(ShowFileName);
99
100
101 // b) display message
102 msg = "Converting " + (AnsiString) szSrcFileName;
103 Synchronize(AddMessage);
104
105
106 // c) convert via TConv2HTML::Convert()
107 try{
108 // initialize back link text ...
109 char szBackLink[STS];
110 if(form->project.f_UserDefinedBackLink)
111 // ... either as user specified text
112 strcpy(szBackLink, form->project.szBackLinkText);
113 else
114 // ... or as link to generated overview file
115 strcpy(szBackLink, TFMain::MakeRelativeLink(szDesFileName, form->wrapper.szFileName));
116
117 // call conversion routine
118 int res = conv->Convert(src, des, form->f_Stop, form->progress, szSrcFileName, szBackLink);
119
120
121 // d) check return value
122 if(res==WRITE_FAILED)
123 {
124 msg = "Unable to write to file " + (AnsiString) szDesFileName + "! Maybe disk is full?";
125 Synchronize(AddMessage);
126 }
127
128
129 // e) check stop flag
130 if(form->f_Stop)
131 {
132 msg = "Conversion stopped due to User Break!";
133 Synchronize(AddMessage);
134 break;
135 }
136 }
137 catch(...)
138 {
139 msg = "Error in conversion routine! Process aborted!";
140 Synchronize(AddMessage);
141 }
142 }
143
144 // indicate progress
145 form->done += form->progress;
146
147 } // end for
148
149
150 //-------------------------------------------------------------------------------------------------------------------
151 // 4. cleanup
152 delete conv;
153 }
154
155
156 //----------------------------------------------------------------------------------------------------------------------
157 // (re-)set progress-counter/gauges and display actually converted filename
158 void __fastcall ThreadFunc::ShowFileName(void)
159 {
160 form->inv5Percent = 20.0/size[fileId];
161 form->progress =0;
162 form->gaugePos =0;
163 form->ProgressBar1->Position=0;
164
165 form->LaProgress2->Caption = MinimizeName(form->project.GetSourceFileName(fileId), form->LaProgress2->Canvas
166 , (form->ProgressBar1->Width-form->LaProgress2->Width)*0.75);
167 }
168
169
170 //----------------------------------------------------------------------------------------------------------------------
171 void __fastcall ThreadFunc::AddMessage(void) { form->AddMessage(msg); }
172 void __fastcall ThreadFunc::OnFinish(TObject *Sender) { delete[] size; form->Finish(); }
Top |