From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!news.osn.de!diablo1.news.osn.de!noris.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Get the path and namefile in run time From: KlausJ@web.de (Klaus Jeschke) References: Organization: No company Message-ID: User-Agent: Xnews/4.05.03 Date: 05 Oct 2009 19:43:31 GMT NNTP-Posting-Date: 05 Oct 2009 21:43:31 CEST NNTP-Posting-Host: 320c766e.newsspool4.arcor-online.net X-Trace: DXC=7ckFe3REFmi;iVb[J9ZZP`4IUK559;`Jn^WiR9boY;9DVCm X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8587 Date: 2009-10-05T21:43:31+02:00 List-Id: Pablo wrote in news:c199874e-f1af-4a1d-a52d- 88b59e27148d@a7g2000yqo.googlegroups.com: > 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