source: trunk/NewView/StartupUnit.pas@ 82

Last change on this file since 82 was 82, checked in by RBRi, 19 years ago

file util refactoring and many more unit tests

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1Unit StartupUnit;
2
3// NewView - a new OS/2 Help Viewer
4// Copyright 2003 Aaron Lawrence (aaronl at consultant dot com)
5// This software is released under the Gnu Public License - see readme.txt
6
7// Code related to startup and finding help files.
8// Shared between NewView.exe and ViewStub.exe
9
10Interface
11
12uses
13 OS2Def,
14 Classes,
15 GlobalFilelistUnit,
16 SharedMemoryUnit,
17 CmdLineParameterUnit;
18
19const
20 OWN_HELP_MARKER = '[NVHELP]';
21
22
23function AccessSharedMemory: TSuballocatedSharedMemory;
24
25// Look for any items that are actually specifiying environment
26// variables, and expand them to the contents of the variables
27Procedure TranslateIPFEnvironmentVars( Items: TStrings;
28 ExpandedItems: TStrings );
29
30// Given a filename, which may or may not contain a path or extension,
31// finds the actual file. This can involve searching
32// the help and bookshelf paths.
33Function FindHelpFile( FileName: string ): string;
34Procedure PostNewViewTextMessage( Window: HWND;
35 MessageType: ULONG;
36 s: string );
37
38var
39 SharedMemory: TSubAllocatedSharedMemory;
40 GlobalFilelist: TGlobalFilelist;
41
42Implementation
43
44uses
45 Dos,
46 SysUtils,
47 DebugUnit,
48 PMWin,
49 ACLStringUtility,
50 HelpManagerUnit,
51 FileUtilsUnit;
52
53// Look for any items that are actually specifiying environment
54// variables, and expand them to the contents of the variables
55Procedure TranslateIPFEnvironmentVars( Items: TStrings;
56 ExpandedItems: TStrings );
57var
58 i: longint;
59 Item: string;
60 EnvironmentVarValue: string;
61begin
62 LogEvent(LogStartup, 'Translating environment vars' );
63 for i := 0 to Items.Count - 1 do
64 begin
65 Item := Items[ i ];
66
67 Item := StrUnQuote( Item ); // remove single quotes
68 Item := StrUnDoubleQuote( Item ); // remove double quotes
69
70 LogEvent(LogStartup, ' Item: ' + Item );
71 EnvironmentVarValue := GetEnv( Uppercase( Item ) );
72 if DosError = 0 then
73 begin
74 // environment var exists - use it's value
75 LogEvent(LogStartup, ' Translated: ' + EnvironmentVarValue );
76 while EnvironmentVarValue <> '' do
77 begin
78 Item := ExtractNextValue( EnvironmentVarValue, '+' );
79 ExpandedItems.Add( Item );
80 end;
81 end
82 else
83 begin
84 // not an environment var
85 ExpandedItems.Add( Item );
86 end;
87 end;
88end;
89
90// Given a filename, which may or may not contain a path or extension,
91// finds the actual file. This can involve searching
92// the help and bookshelf paths.
93Function FindHelpFile( FileName: string ): string;
94var
95 AlternativeFileName: string;
96begin
97 Result := '';
98
99 AlternativeFileName := '';
100 if ExtractFileExt( Filename ) = '' then
101 begin
102 Filename := ChangeFileExt( Filename, '.inf' );
103 AlternativeFileName := ChangeFileExt( Filename, '.hlp' );
104 end;
105
106 if ExtractFilePath( FileName ) <> '' then
107 begin
108 // Path specified; just see if it exists
109
110 // expand out relative paths
111 Filename := ExpandFileName( FileName );
112 AlternativeFilename := ExpandFileName( AlternativeFilename );
113
114 if FileExists( Filename ) then
115 Result := Filename
116 else if FileExists( AlternativeFilename ) then
117 Result := AlternativeFilename;
118
119 end
120 else
121 begin
122 // Path not specified; search current
123 if FileExists( ExpandFileName( FileName ) ) then
124 begin
125 Result := ExpandFileName( FileName );
126 exit;
127 end;
128
129 if FileExists( ExpandFileName( AlternativeFilename ) ) then
130 begin
131 Result := ExpandFileName( AlternativeFilename );
132 exit;
133 end;
134
135 // Search help paths
136
137 if not SearchHelpPaths( FileName,
138 Result,
139 false // don't search our app dir
140 ) then
141 begin
142 // Didn't find as specified or as .inf, try .hlp
143 if AlternativeFilename <> '' then
144 begin
145 if not SearchHelpPaths( AlternativeFileName,
146 Result,
147 false // don't search our app dir
148 ) then
149 begin
150 Result := '';
151 end;
152 end;
153 end;
154 end;
155end;
156
157
158function AccessSharedMemory: TSuballocatedSharedMemory;
159begin
160 Result := TSuballocatedSharedMemory.Create( SharedMemName,
161 SharedMemSize,
162 SharedMemReserveSize );
163end;
164
165procedure PostNewViewTextMessage( Window: HWND;
166 MessageType: ULONG;
167 s: string );
168var
169 ps: pchar;
170begin
171 SharedMemory.Allocate( ps, length( s ) + 1 );
172 ps ^ := s;
173 WinPostMsg( Window,
174 MessageType,
175 LONG( ps ),
176 0 );
177end;
178
179
180Initialization
181End.
Note: See TracBrowser for help on using the repository browser.