AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Cutting Folder from a String (https://forums.alliedmods.net/showthread.php?t=225737)

Kia 09-08-2013 13:18

Cutting Folder from a String
 
Hey everybody,

For my plugin I need to cut-off the folder of a string to create it if it doesn't exist, but I couldn't find any existing method to do that. Anyone has an Idea how to do that?

String Format (Examples) :

Quote:

maps/de_tuscan.bsp
models/de_tuscan/tuscan_grass1a.mdl
gfx/env/dustup.tga
What I need:

Quote:

maps
models/de_tuscan
gfx/env

Clauu 09-08-2013 14:33

Re: Cutting Folder from a String
 
Why don;t you use replace and then check if altered string is a valid folder?

Kia 09-08-2013 14:52

Re: Cutting Folder from a String
 
What do you want to replace?

fysiks 09-08-2013 15:04

Re: Cutting Folder from a String
 
You could iterate backward through the string and when you find the '/', replace it with a 0.

Kia 09-08-2013 15:15

Re: Cutting Folder from a String
 
How do I that backward? :o

fysiks 09-08-2013 15:17

Re: Cutting Folder from a String
 
Quote:

Originally Posted by Kia (Post 2030845)
How do I that backward? :o

Start at strlen() and in your for loop, do i-- for the incrementer. When you find the '/' set it to 0 then break.

Clauu 09-08-2013 15:35

Re: Cutting Folder from a String
 
Quote:

Originally Posted by Kia (Post 2030825)
What do you want to replace?

replace("maps/de_tuscan.bsp",len,"de_tuscan.bsp","")

Kia 09-08-2013 15:51

Re: Cutting Folder from a String
 
It's not always the same string, I was giving an example only.

fysiks 09-08-2013 15:51

Re: Cutting Folder from a String
 
Quote:

Originally Posted by Clauu (Post 2030860)
replace("maps/de_tuscan.bsp",len,"de_tuscan.bsp","")

That requires you to know the filename which makes this approach not very useful.

Clauu 09-08-2013 16:13

Re: Cutting Folder from a String
 
Well then as it been said you could iterate bacward because starting from the end you will have less to count.
As a ideea when you hit the first '/' from the end then stop and replace the iterator position with a null terminated char then copyc until you find that null char, or use that position and make a strmid function something like substr.


All times are GMT -4. The time now is 19:13.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.