home *** CD-ROM | disk | FTP | other *** search
- unit frs_IBStartParams;
-
- {*************************************************************************
- * *
- * UNIT: frs_IBStartParams.pas *
- * *
- * DESCRIPTION: This unit encapsulates standard requirements for *
- * reading of startup params. It has been tailored *
- * specifically to work with InterBase. *
- * *
- * AUTHOR: Paul Reeves *
- * Fleet River Software *
- * http://www.fleetriver.demon.co.uk *
- * *
- * Copyright Paul Reeves ⌐1998-1999 *
- * *
- * This code may be freely used, as long as this header remains intact. *
- * *
- **************************************************************************}
-
- {
- This object just does basic reading and storage of the command line. It is up to the
- developer to write the code that responds to the values.
-
- Params checked for:
- The Base type checks for the following:
-
- /AL - AutoLogin
- /H - Display help screen (only applicable to console applications.)
- /M - Set Monitor on - Use this in code to set 'debug' labels visible at runtime.
- /NS - No Splash Screen - Use this to remove a splash screne while debugging startup code.
- /SQL- Test for this to write an SQL statement to file, prior to execution
-
- The InterBase type checks for these params:
-
- /B - Buffers
- /C - Character Set
- /D - Database name
- /N - Network Protocol
- /P - Password
- /R - SQL Role
- /S - Server
- /U - Username
-
- Usage:
- Declare a variable of the appropriate type and read the properties.
- Params that are qualifed with string should be separated from the param.
- ie: /d c:\data\mydatabase.gdb
- Do not use quotes around the string.
-
- Interbase Network Protocols:
- There is an undocumented command line switch for WISQL that uses numbers
- to specify the network protocol. This object follows the convention. See the code
- for GetIBDatabase for more details.
-
- If a server is specified a connection protocol must be specified, otherwise a local
- connection will be attempted and the server name ignored.
-
- }
-
- interface
-
- uses
- Windows
- , SysUtils
- ;
-
- type
- TStartUpParams = class(TObject)
- private
- FAppName: String; {Application name}
- FAppDir: String; {Directory the application is running in}
- FAutoLogin: Boolean;
- FConfirmRecDelete: Boolean;
- FConfirmAppExit: Boolean;
- FMonitorOn: Boolean;
- FSaveSQL: Boolean;
- FSplashOn: Boolean;
- FToolBarTop: Integer;
- FToolBarLeft: Integer;
- FUserNetId: String;
- FUseAppName: Boolean;
- FWinDir: String;
- FDisplayHelp: Boolean;
- public
- Constructor Create; virtual;
- destructor Destroy; override;
- class function GetNetUserName: String;
- class function GetEnvVar(EnvVar: String): String;
- published
- Property AppName: String read FAppName write FAppName; {Application name}
- Property AppDir: String read FAppDir write FAppDir; {Directory the application is running in}
- Property AutoLogin: Boolean read FAutoLogin;
- property ConfirmRecDelete: Boolean read FConfirmRecDelete write FConfirmRecDelete;
- property ConfirmAppExit: Boolean read FConfirmAppExit write FConfirmAppExit;
- property DisplayHelp: Boolean read FDisplayHelp write FDisplayHelp;
- Property MonitorOn: Boolean read FMonitorOn;
- Property SaveSQL: Boolean read FSaveSQL write FSaveSQL;
- Property SplashOn: Boolean read FSplashOn default True;
- property ToolBarTop: Integer read fToolBarTop write fToolBarTop;
- property ToolBarLeft: Integer read fToolBarLeft write fToolBarLeft;
- property UseAppName: Boolean read FUseAppName write FUseAppName default True;
- property UserNetId: String read FUserNetId write FUserNetId;
- property WinDir: String read FWinDir write FWinDir;
- end;
-
- TIBStartUpParams = class(TStartUpParams)
- private
- FIBDatabase: String;
- FIBPassword: String;
- FIBProtocol: Integer;
- FIBServer: String;
- FIBUsername: String;
- FIBRole: String;
- FIBCharSet: String;
- FIBBuffers: Integer;
-
- Function GetProtocol: String; //protocol as a string
- Procedure SetProtocol(AProtocol: String);
- protected
- public
- Constructor Create; override;
- destructor Destroy; override;
- Function GetIBDatabase: String; //returns server, protocol and database
- published
- Property IBBuffers: Integer read FIBBuffers write FIBBuffers;
- Property IBCharSet: String read FIBCharSet write FIBCharSet;
- Property IBDatabase: String read GetIBDatabase;
- Property IBDatabaseName: String read FIBDatabase write FIBDatabase;
- Property IBPassword: String read FIBPassword write FIBPassword;
- Property IBProtocol: String read GetProtocol write SetProtocol;
- Property IBRole: String read FIBRole write FIBRole;
- Property IBServer: String read FIBServer write FIBServer;
- Property IBUsername: String read FIBUsername write FIBUsername ;
- end;
-
-
- //For InterBase applications there is no point in declaring a variable of
- //type TStartupParams.
- var
- IBStartupParams: TIBStartUpParams;
-
-
- implementation
-
- Constructor TStartUpParams.create;
- var
- i: integer;
-
- begin
-
- inherited create;
-
- FAppName:=ChangeFileExt(extractFileName(paramstr(0)),'');
- FAppDir:=extractFilePath(paramstr(0));
-
- try
- FUserNetId:=getNetUserName;
- except
- FUserNetId:='NO_NET_ID';
- end;
-
- {SplashOn} {if set false then don't show the splash screen}
- FSplashOn:=True;
- for i:=0 to paramcount do
- if ((uppercase(paramstr(i))='-NS') or
- (uppercase(paramstr(i))='/NS')) then begin
- FSplashOn:=False;
- break;
- end;
-
- {AutoLogin} {if true then connect to db without login prompt}
- for i:=0 to paramcount do
- if ((uppercase(paramstr(i))='-AL') or
- (uppercase(paramstr(i))='/AL')) then begin
- FAutoLogin:=True;
- break;
- end;
-
- {DisplayHelp} {if true then write help info to console (n/a to windows apps) }
- for i:=0 to paramcount do
- if ((uppercase(paramstr(i))='-H') or
- (uppercase(paramstr(i))='/H')) then begin
- FDisplayHelp:=True;
- break;
- end;
-
- {MonitorOn} {if true then set some controls visible to enable monitoring}
- for i:=0 to paramcount do
- if ((uppercase(paramstr(i))='-M') or
- (uppercase(paramstr(i))='/M')) then begin
- FMonitorOn:=True;
- break;
- end;
-
- {SaveSQL } {if true then save SQL statements where appropriate}
- for i:=0 to paramcount do
- if (uppercase(paramstr(i))='-SQL') or
- (uppercase(paramstr(i))='/SQL') then begin
- FSaveSQL:=True;
- break;
- end;
- end;
-
- Destructor TStartUpParams.Destroy;
- begin
-
- inherited Destroy;
- end;
-
- class function TStartUpParams.GetNetUserName: String;
- var
- Len: DWORD;
- Name: array[0..255] of Char;
- begin
- Len := SizeOf(Name);
- WNetGetUser({$ifdef Win32}nil, {$endif}Name, Len);
- Result := StrPas(Name);
- end;
-
- class function TStartUpParams.GetEnvVar(EnvVar: String): String;
- var
- tempstr : array[0..255] of char;
- begin
- FillChar(tempstr,255,#0);
- GetEnvironmentVariable(PChar(EnvVar),tempstr,255);
- result:=tempstr;
- end;
-
- {TIBStartUpParams=============================================}
- Constructor TIBStartUpParams.Create;
- var
- i: integer;
- begin
- inherited Create;
-
- {IBBuffers}
- FIBBuffers:=0;
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-B') or
- (uppercase(paramstr(i))='/B') then begin
- FIBBuffers:=StrToInt(paramstr(i+1));
- break;
- end;
- end;
-
- {IBDatabase}
- FIBDatabase:='';
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-D') or
- (uppercase(paramstr(i))='/D') then begin
- FIBDatabase:=paramstr(i+1);
- break;
- end;
- end;
-
- {IBPassword}
- FIBPassword:='';
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-P') or
- (uppercase(paramstr(i))='/P') then begin
- FIBPassword:=paramstr(i+1);
- break;
- end;
- end;
- //if still no password then check environment
- if FIBPAssword='' then
- FIBPAssword:=GetEnvVar('ISC_PASSWORD');
-
- {IBServer}
- FIBServer:='';
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-S') or
- (uppercase(paramstr(i))='/S') then begin
- FIBServer:=paramstr(i+1);
- break;
- end;
- end;
-
- {IBUsername}
- FIBUserName:='';
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-U') or
- (uppercase(paramstr(i))='/U') then begin
- FIBUsername:=paramstr(i+1);
- break;
- end;
- end;
- //if still no username then check environment
- if FIBUsername='' then
- FIBUsername:=GetEnvVar('ISC_USER');
-
-
- {IBProtocol}
- FIBProtocol:=3; //default to local connection
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-N') or
- (uppercase(paramstr(i))='/N') then begin
- FIBProtocol:=StrToInt(paramstr(i+1));
- break;
- end;
- end;
-
- {IBRole}
- FIBRole:='';
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-R') or
- (uppercase(paramstr(i))='/R') then begin
- FIBRole:=paramstr(i+1);
- break;
- end;
- end;
-
- {IBCharSet}
- FIBCharSet:='';
- for i:=0 to paramcount do begin
- if (uppercase(paramstr(i))='-C') or
- (uppercase(paramstr(i))='/C') then begin
- FIBCharSet:=paramstr(i+1);
- break;
- end;
- end;
-
- end;
-
- Destructor TIBStartUpParams.Destroy;
- begin
- //Nothing to do
-
- inherited Destroy;
- end;
-
- Function TIBStartUpParams.GetIBDatabase: string;
- begin
- case FIBProtocol of
- 0 : result:='\\'+FIBServer+'\'+FIBDatabase; //netbeui;
- 1 : result:=FIBServer+'@'+FIBDatabase; //ipx/spx - not really supported
- 2 : result:=FIBServer+':'+FIBDatabase; // tcp/ip
- else
- result:=FIBDatabase;
- end;
- end;
-
- Function TIBStartUpParams.GetProtocol: string;
- begin
- case FIBProtocol of
- 0 : result:='netbeui';
- 1 : result:='ipx/spx';
- 2 : result:='tcp/ip';
- else
- result:='local';
- end;
- end;
-
- Procedure TIBStartUpParams.SetProtocol(AProtocol: String);
- begin
- if uppercase(AProtocol)='NETBEUI' then
- FIBProtocol:=0
- else
- if uppercase(AProtocol)='IPX/SPX' then
- FIBProtocol:=1
- else
- if uppercase(AProtocol)='TCP/IP' then
- FIBProtocol:=2
- else
- FIBProtocol:=3;
-
- end;
-
- initialization
-
- IBStartUpParams:=TIBStartUpParams.create;
-
- finalization
- if assigned(IBStartUpParams) then
- IBStartUpParams.free;
-
-
- end.
-