ios sdk URL filename, remove the url addon special character "\" - ios

In iOS sdk (swift). Lets say i have a three files "mary'sCat.mp3", "mary\s.mp3", "mary\\s.mp3"
(the special character \ is part of the real filename)
When I use the below code to get the urls
FileManager.default.contentsOfDirectory(at: documentDir, includingPropertiesForKeys: nil)
and use the below code to get filename
fileUrl.standardizedFileURL.lastPathComponent
I will have "mary\'sCat.mp3", "mary\\s.mp3", "mary\\\\s.mp3"
So.. is there any way i can remove the system addon special characters \ in a correct way? so i can get back to original file name "mary'sCat.mp3", "mary\s.mp3", "mary\\s.mp3"? I noticed that when using XCode output window you wont see the addon special character, but when you actually see it in debug watch, u will see the addon special character. That is nightmare when doing String compare as below
let mediaUrl_filename = "mary\\s.mp3" \\<-- this value from url
let db_filename = "mary\s.mp3" \\ <-- this value from sqlite
if mediaUrl_filename == db_filename {
print("It is equal")
}
So is there any way to solve this problem?

Actually '\' is used as escape sequence in String objects so while comparing the file name if the file name is 'mary\s.mp3' you will have to write like this
if mediaUrl_filename == "mary\\s.mp3"{
print("It is equal")
}
I suggest that you replace the '\' with other character like '_' in the file name to ignore such confusions.

Related

Katakana character ジ in URL being encoded incorrectly

I need to construct a URL with a string path received from my application server which contains the character: ジ
However, in Swift, the fileURLWithPath seems to encode it incorrectly.
let path = "ジ"
print(URL(fileURLWithPath: path))
print(URL(fileURLWithPath: path.precomposedStringWithCanonicalMapping))
Both print:
%E3%82%B7%E3%82%99
This expected URL path should be:
%E3%82%B8
What am I missing or doing wrong? Any help is appreciated.
There are two different characters, ジ and ジ. They may look the same, but they have different internal representations.
The former is “katakana letter zi”, comprised of a single Unicode scalar which percent-encodes as %E3%82%B8.
The latter is still a single Swift character, but is comprised of two Unicode scalars (the “katakana letter si” and “combining voiced sound mark”), and these two Unicode scalars percent-encode to %E3%82%B7%E3%82%99.
One can normalize characters in a string with precomposedStringWithCanonicalMapping, for example. That can convert a character with the two Unicode scalars into a character with a single Unicode scalar.
But your local file system (or, init(fileURLWithPath:), at least) decomposes diacritics. It is logical that the local file system ensures that diacritics are encoded in some consistent manner. (See Diacritics in file names on macOS behave strangely.) The fact that they are decomposed rather than precomposed is, for the sake of this discussion, a bit academic. When you send it to the server, you want it precomposed, regardless of what is happening in your local file system.
Now, you tell us that the “url path is rejected by the server”. That does not make sense. One would generally not provide a local file system URL to a remote server. One would generally extract a file name from a local file system URL and send that to the server. This might be done in a variety of ways:
You can use precomposedStringWithCanonicalMapping when adding a filename to a server URL, and it honors that mapping, unlike a file URL:
let path = "ジ" // actually `%E3%82%B7%E3%82%99` variant
let url = URL(string: "https://example.com")!
.appendingPathComponent(path.precomposedStringWithCanonicalMapping)
print(url) // https://example.com/%E3%82%B8
If sending it in the body of a request, use precomposedStringWithCanonicalMapping. E.g. if a filename in a multipart/form-data request:
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(filePathKey)\"; filename=\"\(filename.precomposedStringWithCanonicalMapping)\"\r\n")
body.append("Content-Type: \(mimeType)\r\n\r\n")
body.append(data)
body.append("\r\n")
Now, those are two random examples of how a filename might be provided to the server. Yours may vary. But the idea is that when you provide the filename, that you precompose the string in its canonical format, rather than relying upon what a file URL in your local file system uses.
But I would advise avoiding URL(fileURLWithPath:) for manipulating strings provided by the server. It is only to be used when actually referring to files within your local file system. If you just want to percent-encode strings, I would advise using the String method addingPercentEncoding(withAllowedCharacters: .urlPathAllowed). That will not override the precomposedStringWithCanonicalMapping output.
you could try this approach using dataRepresentation:
if let path = "ジ".data(using: .utf8),
let url = URL(dataRepresentation: path, relativeTo: nil) {
print("\n---> url: \(url) \n") //---> url: %E3%82%B8
}

JavaScript: Escape (encode?) special characters in filenames

I have a JavaScript that runs an external program (mediainfo) on local files. However, some of my files have characters that mess up my code.
The code:
objMedInfo = JSON.parse(proc.execSync('mediainfo "' + currentfilename + '" --output=JSON').toString());
The problem is that if currentfilename contains a quotation mark or some other extended characters, the command fails.
Is there a way to escape or encode currentfilename to prevent this? It's not feasible to rename all the potential files.
Use execFileSync instead of execSync. This allows you to supply the arguments in an array, rather than putting everything in a string that has to be parsed by the shell.
objMedInfo = JSON.parse(proc.execFileSync('mediainfo', [currentfilename, '--output=JSON']).toString());

SSRS Goto URL decoding and encoding

I am facing problem when passing value for url through data field.
I am passing value in goto url like this
="javascript:void(window.open('file:" &Fields!url.Value &"','_blank'))"
url value = /servername/foldername/FormuláriodeCalibração.xls
After report deployed and opened in internet explorer and clicked on the url. It is changing the url like this
/servername/foldername/FormuláriodeCalibração.xls
because of which I am unable to open the file.
Please help me in this.
Finally we come up with a solution of replacing non ASCII characters of Portuguese with HTML ASCII Codes.
For e.g. this is the actual file name of the attachment
TE-5180FormuláriodeCalibração(modelo)1271440308393(2)1338379011084.xls
We replaced the Portuguese characters with HTML ASCII Codes.
TE-5180FormuláriodeCalibração(modelo)1271440308393(2)1338379011084.xls
After these changes the above modified URL is passed in the place of actual URL and when it hits the server it was decoded properly and worked as expected.

iOS Localization: Extract strings

I find this tool genstrings, but it fails on line: NSLocalizedString(key,comment);
-(NSString*)toLocalizableString:(NSString*)key
withComment:(NSString*)comment{
NSString* stringResult = nil;
if (key){
stringResult = NSLocalizedString(key, comment);
}
NSLog(#"(*) key: <%#> (&) value: <%#> (*)",key,comment);
return stringResult;
}
Also, steps of localization:
Find all strings and wrap them with NSLocalizedStringFromTable
Extract strings with genstrings
Put localizable files and find translations
well done!
I stuck at first step.
How find all strings, that are needed to have translations?
Or i'm wrong?
Thanks for explanation!
I use Ltools to help with this. It's a two step process:
Search for #" in your code, and wrap the appropriate strings in L()
Run L.sh as part of your build process
This will make sure that every string that needs translating has a translation.
This is not exactly what you are searching for, but for me it doesn't make sense to translate all strings, e.g. you likely do not want to translate strings (#"") from an NSPredicate.
If you moved all relevant strings to NSLocalizableString / NSLocalizableStringFromTable, then I can recommend Linguan. You just pass in your XCode project and you are ready to start translating.

BlackBerry - language support for Chinese

I have localised my app by adding the correct resource files for various European languages / dialects.
I have the required folder in my project: ./res/com/demo/localization
It contains the required files e.g. Demo.rrh, Demo.rrc, Demo_de.rrc etc.
I want to add support for 2 Chinese dialects, and I have the translations in an Excel file. On iPhone, they are referred to by the codes zh_TW & zh_CM. Following the pattern with German, I created 2 extra files called Demo_zh_TW.rrc & Demo_zh_CN.rrc.
I opened file Demo_zh_CN.rrc using Eclipse's text editor, and pasted in line of the Chinese translation using the normal resource file format:
START_LOCATION#0="开始位置";
When I tried to save the file, I got Eclipse's error about the Cp1252 character encoding:
Save could not be completed.
Reason:
Some characters cannot be mapped using "Cp1252" character encoding.
Either change the encoding or remove the characters which are not
supported by the "Cp1252" character encoding.
It seems the Eclipse editor will accept the Chinese characters, but the resource tool expects that these characters must be saved in the resource file as Java Unicode /u encoding.
How do I add language support for these 2 regions without manually copy n pasting in each string?
Is there maybe a tool that I can use to Java Unicode /u encode the strings from Excel so they can be saved in Code page 1252 Latin chars only?
I'm not aware of any readily available tools for working with BlackBerry's peculiar localization style.
Here's a snippet of Java-SE code I use to convert the UTF-8 strings I get for use with BlackBerry:
private static String unicodeEscape(String value, CharsetEncoder encoder) {
StringBuilder sb = new StringBuilder();
for(char c : value.toCharArray()) {
if(encoder.canEncode(c)) {
sb.append(c);
} else {
sb.append("\\u");
sb.append(hex4(c));
}
}
return sb.toString();
}
private static String hex4(char c) {
String ret = Integer.toHexString(c);
while(ret.length() < 4) {
ret = "0" + ret;
}
return ret;
}
Call unicodeEscape with the 8859-1 encoder with Charset.forName("ISO-8859-1").newEncoder()
I suggest you look at Blackberry Hindi and Gujarati text display
You need to use the resource editor to make these files with the right encoding. Eclipse will escape the characters automatically.
This is a problem with the encoding of your resource file. 1252 Code Page contains Latin characters only.
I have never worked with Eclipse, but there should be somewhere you specify the encoding of the file, you should set your default encoding for files to UTF-8 if possible. This will handle your chinese characters.
You could also use a good editor like Notepad++ or EMEditor to set the encoding of your file.
See here for how you can configure Eclipse to use UTF-8 by default.

Resources