File: viewer.cpp

package info (click to toggle)
rtklib 2.4.3%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 41,796 kB
  • sloc: cpp: 51,592; ansic: 50,584; fortran: 987; makefile: 861; sh: 45
file content (159 lines) | stat: -rw-r--r-- 4,218 bytes parent folder | download | duplicates (2)
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
//---------------------------------------------------------------------------
#include <stdio.h>
#include <vcl.h>
#pragma hdrstop

#include "rtklib.h"
#include "viewer.h"
#include "vieweropt.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TTextViewer *TextViewer;
TColor TTextViewer::Color1,TTextViewer::Color2;
TFont *TTextViewer::FontD;
//---------------------------------------------------------------------------
__fastcall TTextViewer::TTextViewer(TComponent* Owner)
	: TForm(Owner)
{
	Option=1;
	TextStr=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::FormShow(TObject *Sender)
{
	if (Option==0) {
		BtnReload->Visible=false;
		BtnRead  ->Visible=false;
	}
	else if (Option==2) {
		BtnReload->Visible=false;
		BtnRead  ->Caption="Save...";
	}
	UpdateText();
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::BtnReloadClick(TObject *Sender)
{
	Read(File);
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::BtnReadClick(TObject *Sender)
{
	if (BtnRead->Caption=="Save...") {
		SaveDialog->FileName=File;
		if (!SaveDialog->Execute()) return;
		Save(SaveDialog->FileName);
	}
	else {
		OpenDialog->FileName=File;
		if (!OpenDialog->Execute()) return;
		Read(OpenDialog->FileName);
	}
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::BtnOptClick(TObject *Sender)
{
	ViewerOptDialog->Left=Left+Width/2-ViewerOptDialog->Width/2;
	ViewerOptDialog->Top=Top+Height/2-ViewerOptDialog->Height/2;
	if (ViewerOptDialog->ShowModal()!=mrOk) return;
	UpdateText();
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::BtnCloseClick(TObject *Sender)
{
	Release();
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::BtnFindClick(TObject *Sender)
{
	wchar_t *p,*str=FindStr->Text.c_str();
	
	if (!TextStr) return;
	
	if (Text->SelLength>0) p=TextStr+Text->SelStart+1;
	else p=TextStr+Text->SelStart;
	
	if ((p=wcsstr(p,str))) {
		Text->SelStart=(int)(p-TextStr);
		Text->SelLength=wcslen(str);
	}
	else {
		Text->SelStart=0;
		Text->SelLength=0;
	}
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::FindStrKeyPress(TObject *Sender, char &Key)
{
	if (Key=='\r') BtnFindClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::Read(AnsiString file)
{
	char s[256],*path[]={s};
	
	if (expath(file.c_str(),path,1)<1) return;
	AnsiString str(path[0]);
	Screen->Cursor=crHourGlass;
	try {
		Text->Lines->LoadFromFile(str);
		
		// read text for search
		ReadText(str);
	}
	catch (...) {
		Screen->Cursor=crDefault;
		return;
	}
	Screen->Cursor=crDefault;
	Caption=str;
	File=file;
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::ReadText(AnsiString file)
{
	FILE *fp;
	int i,len,n=0,nmax=0;
	char buff[1024];
	wchar_t wbuff[1024];
	
	free(TextStr); TextStr=NULL;
	
	if (!(fp=fopen(file.c_str(),"r"))) return;
		
	for (i=0;fgets(buff,sizeof(buff),fp)&&i<MAXLINE;i++) {
		::MultiByteToWideChar(CP_UTF8,0,buff,-1,wbuff,1024);
		len=wcslen(wbuff);
		if (n+len+1>=nmax) {
			nmax=nmax<=0?16384:nmax*2;
			if (!(TextStr=(wchar_t *)realloc(TextStr,sizeof(wchar_t)*nmax))) break;
		}
		wcscpy(TextStr+n,wbuff);
		n+=len;
	}
	fclose(fp);
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::Save(AnsiString file)
{
	Screen->Cursor=crHourGlass;
	try {
		Text->Lines->SaveToFile(file);
	}
	catch (...) {
		Screen->Cursor=crDefault;
		return;
	}
	Screen->Cursor=crDefault;
	File=file;
}
//---------------------------------------------------------------------------
void __fastcall TTextViewer::UpdateText(void)
{
	Text->Font=FontD;
	Text->Font->Color=Color1;
	Text->Color=Color2;
}
//---------------------------------------------------------------------------