// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // Creation Date: Sept 22, 1999 // // // // // dirname( string $path ) // // // Strips the last level of the given pathname. On Windows, all '\' characters // in $path (input as "\\") are replaced with '/'. // // // string $path Path string to strip // // // string: all but the last level of the given pathname // // // string $path = "/usr/people/gamera/gamera.mel"; // string $dirname = dirname( $path ); // // Result: /usr/people/gamera // // string $ntPath = "C:/aw/godzilla/hello.c"; // dirname( $ntPath ); // // Result: C:/aw/godzilla // // // // // // This proc strips the last slash from the path, // provided it's not a 'significant' slash. That is, // if the path looks like "C:/" or "/" proc string stripLastSlash( string $path ) { if ( (match( "^/$", $path ) == "") && // Root dir (match( "^[A-Za-z]:/$", $path ) == "" ) ) // X:/ on Windows { // Strip off last '/', if it exists $path = substitute("/$", $path, ""); } return $path; } global proc string dirname( string $path ) { if ( `about -nt` || `about -mac`) $path = convert( $path ); $path = stripLastSlash( $path ); string $dirname; // If the path starts with "//" we'll assume this is a UNC path and // treat this as root... if ( match( "^//", $path ) != "" ) { // Match first two slashes and everything up to the last slash $dirname = match( "^//.*/", $path ); // Maybe there is no slash other than these first two - // we'll keep the path we entered this block with if ( size( $dirname ) == 0 ) $dirname = $path; else $dirname = stripLastSlash( $dirname ); } else { // Match the beginning up to the first slash $dirname = match( "^.*/", $path ); } $dirname = stripLastSlash( $dirname ); return $dirname; }