Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 3, 2025 17:50
Show Gist options
  • Save thinkphp/093e35b2df8438730e18f5042b3708b1 to your computer and use it in GitHub Desktop.
Save thinkphp/093e35b2df8438730e18f5042b3708b1 to your computer and use it in GitHub Desktop.
audiofile.java
public void parsePathname( String path ) {
this.pathname = ""
this.filename = ""
if(path == null || path.trim().isEmpty()) {
return;
}
//trim() este o functie care elimina spatiile de la inceput si sfarsit
path = path.trim(); // c:\\asdas
String separator = System.getProperty("file.separator");
//normalizam operatorii de cale
String normalizedPath = path;
if(isWindows()) {
//inlocuim / cu \ pe windows
normalizedPath = normalizedPath.replace('/','\\');
//gestionm secventele de separatori
while(normalizedPath.contains("\\\\")) normalizedPath = normalizedPath.replace("\\\\","\\");
} else {
//unix/linux/mac inlocuim \ cu /
normalizedPath = normalizedPath.replace('\\','/');
//tratam literele pentru non-windows
if(normalizedPath.length() >= 2 && Character.isLetter(normalizedPath.charAt(0))
&& normalizedPath.charAt(1) == ':') {
char driveLetter = normalizedPath.charAt(0);
normalizedPath = '/' + driveLetter + normalizedPath.substring(2);
}
//gestionam secventele de separatori
while(normalizedPath.contains("//")) normalizedPath = normalizedPath.replace("//","/");
}
//setam pathname
this.pathname = normalizedPath;
int lastSeparatorIndex = -1;
if(isWindows()) {
lastSeparatorIndex = normalizedPath.lastIndexOf("\\");
} else {
lastSeparatorIndex = normalizedPath.lastIndexOf("/");
}
if(lastSeparatorIndex != -1 && lastSeparatorIndex < normalizedPath.length() - 1) {
this.filename = normalizedPath.substring(lastSeparatorIndex + 1);
} else if(lastSeparatorIndex == -1) {
//nu exista separator , intregul path este filename
this.filename = normalizedPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment