summaryrefslogtreecommitdiff
path: root/docview/src/dvHelpers.pas
blob: 25e68c92d3c6580ce23922eb254819507a05a0ba (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
unit dvHelpers;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpg_base;

const
  OWN_HELP_MARKER = '[DOCVIEWHELP]';


function GetOwnHelpFileName: String;
// Given a filename, which may or may not contain a path or extension,
// finds the actual file. This can involve searching
// the help and bookshelf paths.
function FindHelpFile(const AFilename: TfpgString): TfpgString;

function SearchHelpPaths( const Filename: TfpgString; var ResultFilename: TfpgString; const IncludeAppDir: boolean ): boolean;

function GetApplicationDir: TfpgString;
function SearchPath( PathEnvVar: TfpgString; Filename: TfpgString; var FilenameFound: string ): boolean;

implementation

uses
  fpg_utils
  ,dvConstants
  ,nvUtilities
  ;


function GetOwnHelpFileName: String;
begin
  { TODO -oGraeme -cown help : Maybe later we will have different language versions }
  result := fpgExtractFilePath(ParamStr(0)) + cDocViewHelpFile;
  if not fpgFileExists(Result) then
  begin
     Result := FindHelpFile(cDocViewHelpFile);
  end;
end;

// Given a "filename" which may include a path, find it in various paths and extensions
function FindHelpFile(const AFilename: TfpgString): TfpgString;
var
  AlternativeFileName: TfpgString;
  lFilename: TfpgString;
begin
  lFilename := AFilename;
  if lFileName = OWN_HELP_MARKER then
  begin
    Result := GetOwnHelpFileName;
    exit;
  end;

  Result := '';

  AlternativeFileName := '';
  if fpgExtractFileExt( lFilename ) = '' then
  begin
    lFilename := fpgChangeFileExt(lFilename, '.inf');
    AlternativeFileName := fpgChangeFileExt(lFilename, '.hlp');
  end;

  if fpgExtractFilePath( lFileName ) <> '' then
  begin
    // Path specified; just see if it exists

    // expand out relative paths
    lFilename := fpgExpandFileName( lFileName );
    AlternativeFilename := fpgExpandFileName( AlternativeFilename );

    if fpgFileExists( lFilename ) then
      Result := lFilename
    else if fpgFileExists( AlternativeFilename ) then
      Result := AlternativeFilename;

  end
  else
  begin
    // Path not specified; search current
    if fpgFileExists( fpgExpandFileName( lFileName ) ) then
    begin
      Result := fpgExpandFileName( lFileName );
      exit;
    end;

    if (AlternativeFilename <> '') and fpgFileExists(fpgExpandFileName(AlternativeFilename)) then
    begin
      Result := fpgExpandFileName( AlternativeFilename );
      exit;
    end;

    // Search help paths
    if not SearchHelpPaths( lFileName,
                            Result,
                            false // don't search our app dir
                             ) then
    begin
      // Didn't find as specified or as .inf, try .hlp
      if AlternativeFilename <> '' then
      begin
        if not SearchHelpPaths( AlternativeFileName,
                                Result,
                                false // don't search our app dir
                                ) then
        begin
          Result := '';
        end;
      end;
    end;
  end;
//  Result := AFileName;
end;

Function SearchHelpPaths( const Filename: TfpgString;
                          var ResultFilename: TfpgString;
                          const IncludeAppDir: boolean ): boolean;
begin
  Result := SearchPath( HelpPathEnvironmentVar,
                        FileName,
                        ResultFilename );
  if not Result then
    Result := SearchPath( BookshelfEnvironmentVar,
                          FileName,
                          ResultFilename );
  if ( not Result ) and IncludeAppDir then
  begin
    ResultFilename := fpgAppendPathDelim(GetApplicationDir)
                      + Filename;
    Result := fpgFileExists( ResultFilename );
    if not Result then
      ResultFilename := '';
  end;

end;

function GetApplicationDir: TfpgString;
begin
  Result := fpgExtractFilePath(ParamStr(0));
end;

function SearchPath( PathEnvVar: TfpgString; Filename: TfpgString; var FilenameFound: string ): boolean;
var
  lFilename: string;
  lDir: TfpgString;
  fl: TStrings;
  i: integer;
begin
  Result := False;
  FilenameFound := '';

  lDir := GetEnvironmentVariable(PathEnvVar);

  fl := TStringList.Create;
  ListFilesInDirectory(lDir, AllFilesMask, True, fl);
  TStringList(fl).Sort;
  for i := 0 to fl.Count-1 do
  begin
    lFilename := fpgExtractFileName(fl[i]);
    if SameText(lFilename, Filename) then
    begin
      FilenameFound := fl[i];
      Result := True;
      Exit;
    end;
  end;
end;


end.