Thread: Google didn't help, looking for directory extracter

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Google didn't help, looking for directory extracter

    let's say I have a path like "shaders/vfxapps.ini" or "shaders\\vfxapps.ini" is there a standard way to extract just the directory part or will I need to write my own function for it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Is this in your own OS, or something we might be aware of?
    basename(3) - Linux manual page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by awsdert View Post
    let's say I have a path like "shaders/vfxapps.ini" or "shaders\\vfxapps.ini" is there a standard way to extract just the directory part or will I need to write my own function for it?
    strtok() or write your own function using strtok() to split a more complex path.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Salem View Post
    Is this in your own OS, or something we might be aware of?
    basename(3) - Linux manual page
    Wrong one but did direct me to the one I was after, dirname(), now I just need one for _WIN32

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Have a look at _splitpath:
    _splitpath, _wsplitpath | Microsoft Docs

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by thmm View Post
    Have a look at _splitpath:
    _splitpath, _wsplitpath | Microsoft Docs
    ty I'm now using this:
    Code:
    #ifdef _WIN32
    char* dirname( char* path )
    {
    	_splitpath( path, NULL, path, NULL, NULL );
    	return path;
    }
    #else
    #include <libgen.h>
    #endif
    I don't expect there to be issues but fi anyone sees any please let me know

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > _splitpath( path, NULL, path, NULL, NULL );
    Yeah, pretty sure that having input and output being the same memory would be UB.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Salem View Post
    > _splitpath( path, NULL, path, NULL, NULL );
    Yeah, pretty sure that having input and output being the same memory would be UB.
    Well the docs for dirname says it modifies path anyways, since _splitpath likely just loops through each character 1 by 1 it will probably set 0 before it checks the next character which would naturally result in the string breaking the intended way, perhaps it's even intended to be used that way, if you're on windows I welcome you to do a test run though, I somehow doubt the function changed it's method of execution since it's creation. Anyways I've got gospel to go to so adios for now.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    We're talking about splitpath, and the docs say the input is declared const char *.
    So yeah, whether it says so or not, overwriting your input with your output is a crap shoot.

    Also, I've no interest in testing it.
    Because no matter how many times the test 'works', the fact remains that what you propose is a dumb idea.

    Enjoy your bug hunting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The guy says he's "got gospel to go to", so clearly reality is no impediment.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  11. #11
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by john.c View Post
    The guy says he's "got gospel to go to", so clearly reality is no impediment.
    You realise God is not only a reality but the reason we all exist right? You could think of God as the perfect quantum computer that has no pyhsical limitations, not a great comparison due to computers being inferior to God but you get the point right?

    Quote Originally Posted by Salem View Post
    We're talking about splitpath, and the docs say the input is declared const char *.
    So yeah, whether it says so or not, overwriting your input with your output is a crap shoot.

    Also, I've no interest in testing it.
    Because no matter how many times the test 'works', the fact remains that what you propose is a dumb idea.

    Enjoy your bug hunting.
    I never actually tested it, it's just a stop gap measure that in theory will work, I do have printf statements that output it's contents prior to usage so at the very least if it causes issues it will be spotted in the output (of which it would show up early since it's used to construct the relative paths of shader files, if they fail to load there won't be much output to check).

  12. #12
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    The code compiles and runs on VS2019 and GCC (tdm64-1) 9.2.0 on Windows 8.1.

  13. #13
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by thmm View Post
    The code compiles and runs on VS2019 and GCC (tdm64-1) 9.2.0 on Windows 8.1.
    Good to know thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-30-2010, 06:13 PM
  2. I didn't know
    By madmax2006 in forum General Discussions
    Replies: 2
    Last Post: 02-10-2010, 11:02 PM
  3. Reading Google Sketchup (.skp) or Google Earth (.kmz)
    By Zeeshan in forum Game Programming
    Replies: 9
    Last Post: 03-07-2008, 05:25 PM
  4. CS chat extracter
    By _Cl0wn_ in forum Game Programming
    Replies: 4
    Last Post: 09-19-2003, 03:22 PM

Tags for this Thread