Re: Get the path and namefile in run time
- From: KlausJ@xxxxxx (Klaus Jeschke)
- Date: 05 Oct 2009 19:43:31 GMT
Pablo <pablittto@xxxxxxxxx> wrote in news:c199874e-f1af-4a1d-a52d-
88b59e27148d@xxxxxxxxxxxxxxxxxxxxxxxxxxx:
How can I set a string in run-time with the name of the file and other
with the path?
Say us, If the exec file is in c:\myexecutable\main.exe, I want a way
to set a String in such a way that String1="main.exe" "and c:
\myexecutable\".
Thanks.
Hi Pablo,
you did not mention, which OS you use. If you are working on Windows, then
maybe the following may help you. I hope, that the news client does not
reformat the code.
------------------------------------- Program_Name.ads
--
---------------------------------------------------------------------------
--
-- This package provides functions to get the name of the current program
or --
-- parts of it.
--
--
--
-- Maintenance level: 1.01A00
08.10.2008 --
--
---------------------------------------------------------------------------
--
package Program_Name is
function Get_Path_Only return String;
-- This returns the path where the program is located (without prog-name
and extension)
function Get_Full_Path return String;
-- This returns the full name including path and extension
function Get_Full_Path_Length return Integer;
-- This returns the length of the program name including path and
extension
function Get_Name_Length return Integer;
-- This returns the length of the program name without path and without
extension
function Get_Program_Name return String;
-- This returns the name of the program without path and without extension
function Get_Program_Name_First (N : Positive) return String;
-- This returns the first N characters of the program name.
-- If the name is shorter than N characters a 'Constraint_Error' is
raised.
function Get_Program_Name_Last (N : Positive) return String;
-- This returns the last N characters of the program name.
-- If the name is shorter than N characters a 'Constraint_Error' is
raised.
function Get_Program_Name_Before (C : Character) return String;
-- This returns the characters from the beginning up to (but not
including)
-- the first occurrence of the specified character.
-- If there is no character 'C' or 'C' is the first character of the name
-- then an empty string is returned.
function Get_Program_Name_After (C : Character) return String;
-- This returns the characters after the last occurrence of 'C' till the
-- end of the program name
-- If there is no character 'C' then an empty string is returned.
end Program_Name;
------------------------------------- End Program_Name.ads
------------------------------------- Program_Name.adb
--
---------------------------------------------------------------------------
--
-- This package provides functions to get the name of the current program
or --
-- parts of it.
--
--
--
-- Maintenance level: 1.01A00
08.10.2008 --
--
---------------------------------------------------------------------------
--
with Win32;
with Win32.Winbase;
with System;
with Unchecked_Conversion;
package body Program_Name is
function To_PCHAR is new Unchecked_Conversion (System.Address,
Win32.PCHAR);
function Get_Full_Path return String is
Erg : Win32.DWORD;
Path : String (1..1024);
begin
Erg := Win32.Winbase.GetModuleFilename (System.Null_Address, To_PCHAR
(Path'Address), Path'Length);
return Path (1 .. Integer (Erg));
end Get_Full_Path;
function Get_Path_Only return String is
Full_Path : String := Get_Full_Path;
i, j : Integer;
begin
j := Full_Path'Last;
while j > 0
loop
if Full_Path (j) = '\'
then
exit;
end if;
j := j - 1;
end loop;
if j = 0 then return ""; end if;
if Full_Path (j) = '\' then return Full_Path (1 .. j - 1); end if;
return "";
end Get_Path_Only;
function Get_Full_Path_Length return Integer is
Erg : Win32.DWORD;
Path : String (1..1024);
begin
Erg := Win32.Winbase.GetModuleFilename (System.Null_Address, To_PCHAR
(Path'Address), Path'Length);
return Integer (Erg);
end Get_Full_Path_Length;
function Get_Name_Length return Integer is
Name : String := Get_Program_Name;
begin
return Name'Length;
end Get_Name_Length;
function Get_Program_Name return String is
Full_Path : String := Get_Full_Path;
i, j : Integer;
begin
j := Full_Path'Last;
while j > 0
loop
if Full_Path (j) = '.' or Full_Path (j) = '\'
then
exit;
end if;
j := j - 1;
end loop;
if j = 0 then return Full_Path; end if;
if Full_Path (j) = '\' then return Full_Path (j+1 .. Full_Path'Last);
end if;
if Full_Path (j) = '.'
then
j := j - 1;
i := j;
while i > 0
loop
if Full_Path (i) = '\'
then
exit;
end if;
i := i - 1;
end loop;
return Full_Path (i+1 .. j);
else
return "";
end if;
end Get_Program_Name;
function Get_Program_Name_First (N : Positive) return String is
Name : String := Get_Program_Name;
begin
if N > Name'Length
then
raise Constraint_Error;
else
return Name (Name'First .. Name'First + N - 1);
end if;
end Get_Program_Name_First;
function Get_Program_Name_Last (N : Positive) return String is
Name : String := Get_Program_Name;
begin
if N > Name'Length
then
raise Constraint_Error;
else
return Name (Name'Last - N + 1 .. Name'Last);
end if;
end Get_Program_Name_Last;
function Get_Program_Name_Before (C : Character) return String is
Name : String := Get_Program_Name;
i : Integer;
begin
i := Name'First - 1;
while i < Name'Last
loop
i := i + 1;
if Name (i) = C then exit; end if;
end loop;
if Name (i) = C
then
return Name (Name'First .. i - 1);
else
return "";
end if;
end Get_Program_Name_Before;
function Get_Program_Name_After (C : Character) return String is
Name : String := Get_Program_Name;
i : Integer;
begin
i := Name'Last + 1;
while i > Name'First
loop
i := i - 1;
if Name (i) = C then exit; end if;
end loop;
if Name (i) = C
then
return Name (i + 1 .. Name'Last);
else
return "";
end if;
end Get_Program_Name_After;
end Program_Name;
------------------------------------- End Program_Name.adb
.
- References:
- Get the path and namefile in run time
- From: Pablo
- Get the path and namefile in run time
- Prev by Date: Discriminant and type extensions
- Next by Date: Re: Discriminant and type extensions
- Previous by thread: Re: Get the path and namefile in run time
- Next by thread: Gtk Toolbars
- Index(es):
Relevant Pages
|