How to make the platform independent filepath slash "/" with GLib? (GJS) - gtk

I have a folderPath which has a directory string:
/home/bastian/Pictures
and I have a variable fileName which contains the name.
I can concatenate the two strings together like this, but it only works on UNIX systems:
let filePath = folderPath + '/' + fileName;
Is there a way with GLib I can concatenate the two to each other without making assumptions about the slash or backslash (to stay fx Windows-compatible)?

With help from guadec, I found out I could use GLib's g_build_filenamev () function.
let filePath = GLib.build_filenamev([folderPath, fileName]);
This builds a path to the file and respects the platform at the same time.
Note: it requires that you import GLib first at the top of your GJS file, like this:
const { GLib } = imports.gi;

If you happen to be using a Gio.File object to manipulate the path, you can also do something like this:
const folder = Gio.File.new_for_path(folderPath);
const file = folder.get_child(fileName);

Related

%3d instead of = in file path, then i try to open file from resources

I write some tests and to get absolute path from relative path i use this function
private def getAbsolutePath(filePath: String): String = {
getClass.getResource(filePath).getFile
}
and then i do:
println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/"))
println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/own_loading_id=1/partition_column=test/"))
i get:
/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/
/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/own_loading_id%3d1/partition_column%3dtest/
As you can see, instead of =, I get some strange symbol. At the same time, when I try to read these files with a park, he can read the path without %3d, and with %3d he gets the error "Path does not exist".
How can I fix this?
Seems like its URL encoded, maybe because using stuff from files and resources are designed to work with Universal Resource Locators. You can URLDecode it like so:
import java.net.URLDecoder
def getAbsolutePath(filePath: String): String = {
val path = getClass.getResource(filePath).getFile
URLDecoder.decode(path, "UTF-8")
}

Replace backSlash '\' with a normal slash '/' in dart

Hi guys I have an imageUrl with backSlashes('\\') I want to replaceAll the backSlashes with a normal slash ('/')
this is an imageUrl example :
public\images\providerProfilePictures\2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg
I want to make it with normal slashes like this one :
public/images/providerProfilePictures/2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg
this is my code :
path = decoded['profilePictureUrl'];
if (path!=''){
path.replaceAll('/', '\\');
path=ApiConstants.BASE_URL+path;
print(" my path now : "+path);
I'm getting a wrong result with this function any help would be appreciated
I think you got the 2 parameters swapped.
You have:
path.replaceAll('/', '\\');
It should be:
path.replaceAll('\\', '/');
Unrelated, but instead of if (path!='') you could write if (path.isNotEmpty) for improved readability.
Also, you can use the letter r in front of a String to make it verbatim.
Try this:
final s = r'public\images\providerProfilePictures\2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg';
final url = s.replaceAll('\\', '/');
print(url);
Console output:
flutter:
public/images/providerProfilePictures/2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg

AKAudioFile exportAsynchronously path errors

I'm trying to use AKAudioFile.exportAsynchronously to convert wav to m4a (based on the sample code here: https://audiokit.io/playgrounds/Playback/Exporting%20Files/). I've chosen .documents as my BaseDirectory, but I just keep getting directory <my_dir> isn't valid errors — e.g.:
AKAudioFile+ProcessingAsynchronously.swift:exportAsynchronously(name:baseDir:exportFormat:fromSample:toSample:callback:):379:ERROR AKAudioFile export: directory "/var/mobile/Containers/Data/Application/20C913AD-B2F4-4F26-AAD2-0DFA0C65A886/Documents/All Of Me.mp4" isn't valid
That URL looks completely reasonable, to me, so what's up?
Okay, following #jake's tip, the solution was to handle the spaces explicitly before passing into AKAudioFile's exportAsynchronously(name:baseDir:exportFormat:callback:). I just did:
var name = String(cafURL.lastPathComponent.split(separator: ".")[0])
name = name.replacingOccurrences(of: " ", with: "%20")
let exportFile = try AKAudioFile(readFileName: "\(name).wav", baseDir: .documents)
exportFile.exportAsynchronously(name: name, baseDir: .documents, exportFormat: .m4a, callback: self.callback)

Matlab: check if string has a correct file path syntax

I want to check if a string represents a full path of a file, like this:
p = 'C:\my\custom\path.txt'
The file does not exist, so commands like isdir and exist return false to me, but still the format of the string represents a valid path for my operating system, while the following one does not because it has an invalid character for the file name:
p = 'C:\my\custom\:path.txt'
So I'd like to know how to check if a string represents a valid file path without needing that the file actually exists.
You might want to use the regexp function with a regular expression to match Windows paths.
if isempty(regexp(p, '^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$', 'once'))
// This is not a path
end
You can also let Matlab try for you:
if ~isdir(p)
[status, msg] = mkdir(p);
if status
isdir(p)
rmdir(p)
else
error(msg)
end
end
First, you check if the folder exists, if not you try to create it. If you succeed then you delete it, and if not you throw an error.
This is not recommended for checking many strings but has the advantage of being cross-platform.
function bool = isLegalPath(str)
bool = true;
try
java.io.File(str).toPath;
catch
bool = false;
end
end

How do I find the full directory for an IFile in Cake?

I'm writing a script to produce some artefacts from my build so I want to clean up unwanted files first. I'm using CleanDirectory(dirPath, predicate).
I'm finding it disturbingly hard to work out the directory for a file. If I use GetDirectoryName() that seems to just get me the immediate parent, not the full directory path.
Func<IFileSystemInfo, bool> predicate =
fileSystemInfo => {
// Dont filter out any directories
if (fileSystemInfo is IDirectory)
return false;
var path = fileSystemInfo.Path.FullPath;
var directory = ((DirectoryPath)path).GetDirectoryName();
...
}
Obviously I can use the .NET Framework System.IO classes to do this easily but then I get strings with the slashes in the wrong direction, and things do not smoothly inter-operate with Cake which uses POSIX paths.
OK I've worked out a solution. The key to IFileSystemInfo is to try and cast the Path to various derived types/interfaces, which then provide the functionality you are probably looking for. Example:
Func<IFileSystemInfo, bool> predicate =
fileSystemInfo => {
// Dont filter out any directories
if (fileSystemInfo is IDirectory)
return false;
// We can try and cast Path as an FilePath as know it's not a directory
var file = (FilePath) fileSystemInfo.Path;
if (file.FullPath.EndsWith("Help.xml", StringComparison.OrdinalIgnoreCase))
return false;
// GetDirectory() returns a Path of type DirectoryPath
var directory = file.GetDirectory().FullPath;
...
}