Share your advanced PureBasic knowledge/code with the community.
Seldon
Enthusiast
Posts: 405 Joined: Fri Aug 22, 2003 7:12 am
Location: Italia
Post
by Seldon » Mon Sep 21, 2009 9:49 pm
Just a tiny function I needed. Maybe it can be useful for someone else.
Code: Select all
Declare.s RemoveExtensionPart(File$)
FileName$="pinco.exe"
Debug RemoveExtensionPart(FileName$)
Procedure.s RemoveExtensionPart(File$)
Protected tmp$
tmp$=GetExtensionPart(File$)
If tmp$
tmp$="."+tmp$
ProcedureReturn(RemoveString(File$,tmp$))
EndIf
ProcedureReturn("")
EndProcedure
ts-soft
Always Here
Posts: 5756 Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany
Post
by ts-soft » Mon Sep 21, 2009 9:54 pm
thx,
here a bit shorter (not a procedure)
Code: Select all
FileName$="pinco.exe"
Debug Left(FileName$, Len(FileName$) - Len(GetExtensionPart(FileName$)) - 1)
Greetings
Thomas
Demivec
Addict
Posts: 4270 Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA
Post
by Demivec » Mon Sep 21, 2009 11:25 pm
There seem to be some inconsistencies with the version by Seldon and by ts-soft.
I did a variation of Seldon's code, calling it RemoveExtensionPart2(), that corrects the return value when there is no extension present.
Code: Select all
Procedure.s RemoveExtensionPart(File$)
Protected tmp$
tmp$=GetExtensionPart(File$)
If tmp$
tmp$="."+tmp$
ProcedureReturn(RemoveString(File$,tmp$))
EndIf
ProcedureReturn("")
EndProcedure
Procedure.s RemoveExtensionPart2(File$)
Protected tmp$
tmp$=GetExtensionPart(File$)
If tmp$
tmp$="."+tmp$
ProcedureReturn(RemoveString(File$,tmp$))
EndIf
ProcedureReturn File$
EndProcedure
Debug "filename: " + Chr(34) + filename$ + Chr(34)
filename$="pinco.exe"
Debug RemoveExtensionPart(filename$)
Debug RemoveExtensionPart2(filename$)
Debug Left(filename$, Len(filename$) - Len(GetExtensionPart(filename$)) - 1)
filename$="pinco."
Debug "filename: " + Chr(34) + filename$ + Chr(34)
Debug RemoveExtensionPart(filename$) ;fails
Debug RemoveExtensionPart2(filename$)
Debug Left(filename$, Len(filename$) - Len(GetExtensionPart(filename$)) - 1)
filename$="pinco"
Debug "filename: " + Chr(34) + filename$ + Chr(34)
Debug RemoveExtensionPart(filename$) ;fails
Debug RemoveExtensionPart2(filename$)
Debug Left(filename$, Len(filename$) - Len(GetExtensionPart(filename$)) - 1) ;fails
Seymour Clufley
Addict
Posts: 1265 Joined: Wed Feb 28, 2007 9:13 am
Location: London
Post
by Seymour Clufley » Tue Sep 22, 2009 2:13 am
Here's what I always do:
Code: Select all
ext.s = StringField(filename+".",2,".")
Safer:
Code: Select all
dots = CountString(filename,".")
ext.s = StringField(filename+".",dots+1,".")
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Little John
Addict
Posts: 4791 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Tue Sep 22, 2009 5:57 am
Demivec wrote: I did a variation of Seldon's code, calling it RemoveExtensionPart2(), that corrects the return value when there is no extension present.
Yes, that correction was necessary.
Here is my variation:
Code: Select all
Procedure.s RemoveExtensionPart (file$)
Protected lenExt
lenExt = Len(GetExtensionPart(file$))
If lenExt
ProcedureReturn(Left(file$, Len(file$)-lenExt-1))
Else
ProcedureReturn file$
EndIf
EndProcedure
Regards, Little John
ts-soft
Always Here
Posts: 5756 Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany
Post
by ts-soft » Tue Sep 22, 2009 6:29 am
saferversion:
Code: Select all
FileName$ = "pinco"
If GetExtensionPart(FileName$)
Debug Left(FileName$, Len(FileName$) - Len(GetExtensionPart(FileName$)) - 1)
Else
Debug FileName$
EndIf
Little John
Addict
Posts: 4791 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Tue Sep 22, 2009 6:47 am
ts-soft wrote: saferversion:
Thomas, do you mean that this version is safer than mine? If so, why do you think so? Both versions are almost equal.
Regards, Little John
ts-soft
Always Here
Posts: 5756 Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany
Post
by ts-soft » Tue Sep 22, 2009 6:51 am
Little John wrote: ts-soft wrote: saferversion:
Thomas, do you mean that this version is safer than mine? If so, why do you think so? Both versions are almost equal.
Regards, Little John
Is Safer than my first version
blueznl
PureBasic Expert
Posts: 6166 Joined: Sat May 17, 2003 11:31 am
Contact:
Post
by blueznl » Tue Sep 22, 2009 8:30 am
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide
right here ... )
breeze4me
Enthusiast
Posts: 633 Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor
Post
by breeze4me » Tue Sep 22, 2009 11:02 am
Windows only.
Code: Select all
file.s{1024} = "c:\my files\my data.ico file.ico"
PathRemoveExtension_(@file)
Debug file ; c:\my files\my data.ico file
Seldon
Enthusiast
Posts: 405 Joined: Fri Aug 22, 2003 7:12 am
Location: Italia
Post
by Seldon » Tue Sep 22, 2009 1:42 pm
Thanks for all suggestions. OK, I've just collected the best solutions (IMHO).
Code: Select all
Declare.s RemoveExtensionPart(File$)
FileName$="pinco.exe"
Debug RemoveExtensionPart(FileName$)
Procedure.s RemoveExtensionPart(File$)
CompilerIf #PB_Compiler_OS<>#PB_OS_Windows
Protected extension_length
extension_length=Len(GetExtensionPart(File$))
If extension_length
ProcedureReturn(Left(File$,Len(File$)-extension_length-1))
EndIf
ProcedureReturn(File$)
CompilerElse
Protected tmp$=File$
PathRemoveExtension_(@tmp$)
ProcedureReturn(tmp$)
CompilerEndIf
EndProcedure
Joakim Christiansen
Addict
Posts: 2452 Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:
Post
by Joakim Christiansen » Wed Sep 23, 2009 10:27 am
ts-soft wrote: saferversion:
Code: Select all
FileName$ = "pinco"
If GetExtensionPart(FileName$)
Debug Left(FileName$, Len(FileName$) - Len(GetExtensionPart(FileName$)) - 1)
Else
Debug FileName$
EndIf
Also safe:
Code: Select all
filename$ = "this file has no extension"
Debug StringField(filename$,1,".")
filename$ = "this file has a extension.txt"
Debug StringField(filename$,1,".")
Can I get a cookie now?
sorry for fooling around, I coluld make a uber nice procedure another day
I like logic, hence I dislike humans but love computers.
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Wed Sep 23, 2009 10:47 am
Joakim Christiansen wrote: Also safe:
Code: Select all
filename$ = "this file has no extension"
Debug StringField(filename$,1,".")
filename$ = "this file has a extension.txt"
Debug StringField(filename$,1,".")
Code: Select all
filename$ = "Review 2009.09.23.doc"
Debug StringField(filename$,1,".")
pwned
Joakim Christiansen wrote: Can I get a cookie now?
NOPE!
oh... and have a nice day.
Kurzer
Enthusiast
Posts: 676 Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg
Post
by Kurzer » Wed Sep 23, 2009 10:49 am
No, its not safe. Try this example:
FileName$ = "this file has a extension.txt but its a.doc"
Debug StringField(FileName$,1,".")
No cookie this time!
Edit: Yeah, frogs rulez!
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
Hroudtwolf
Addict
Posts: 803 Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:
Post
by Hroudtwolf » Wed Sep 23, 2009 11:35 am
Hi,
Just another way...
Code: Select all
; 2009 Hroudtwolf
; PureBasic 4.x
Structure tChar
StructureUnion
c.c
s.s {1}
EndStructureUnion
EndStructure
Procedure.s removeExtensionPart (sSource.s)
Protected *sourceStart = @ sSource
Protected *sourceEnd .tChar = *sourceStart
If Not (*sourceStart)
ProcedureReturn sSource
EndIf
*sourceEnd + StringByteLength (sSource)
While *sourceEnd > *sourceStart
If *sourceEnd\c = '.'
*sourceEnd\c = #Null
Break
EndIf
*sourceEnd - SizeOf (tChar)
Wend
ProcedureReturn sSource
EndProcedure
Debug removeExtensionPart ("thisismyfilename.ext.ext.bla.blup")
Regards
Wolf