2017-07-17 07:36:45 +02:00
|
|
|
|
/*
|
|
|
|
|
Translate file names for OpenDrive
|
|
|
|
|
|
|
|
|
|
OpenDrive reserved characters
|
|
|
|
|
|
|
|
|
|
The following characters are OpenDrive reserved characters, and can't
|
|
|
|
|
be used in OpenDrive folder and file names.
|
|
|
|
|
|
2018-04-26 23:02:31 +02:00
|
|
|
|
\ / : * ? " < > |
|
|
|
|
|
|
|
|
|
|
OpenDrive files and folders can't have leading or trailing spaces also.
|
2017-07-17 07:36:45 +02:00
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package opendrive
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// charMap holds replacements for characters
|
|
|
|
|
//
|
2018-04-26 23:02:31 +02:00
|
|
|
|
// OpenDrive has a restricted set of characters compared to other cloud
|
2017-07-17 07:36:45 +02:00
|
|
|
|
// storage systems, so we to map these to the FULLWIDTH unicode
|
|
|
|
|
// equivalents
|
|
|
|
|
//
|
|
|
|
|
// http://unicode-search.net/unicode-namesearch.pl?term=SOLIDUS
|
|
|
|
|
var (
|
|
|
|
|
charMap = map[rune]rune{
|
|
|
|
|
'\\': '\', // FULLWIDTH REVERSE SOLIDUS
|
2018-04-26 23:02:31 +02:00
|
|
|
|
':': ':', // FULLWIDTH COLON
|
2017-07-17 07:36:45 +02:00
|
|
|
|
'*': '*', // FULLWIDTH ASTERISK
|
2018-04-26 23:02:31 +02:00
|
|
|
|
'?': '?', // FULLWIDTH QUESTION MARK
|
|
|
|
|
'"': '"', // FULLWIDTH QUOTATION MARK
|
2017-07-17 07:36:45 +02:00
|
|
|
|
'<': '<', // FULLWIDTH LESS-THAN SIGN
|
|
|
|
|
'>': '>', // FULLWIDTH GREATER-THAN SIGN
|
|
|
|
|
'|': '|', // FULLWIDTH VERTICAL LINE
|
|
|
|
|
' ': '␠', // SYMBOL FOR SPACE
|
|
|
|
|
}
|
|
|
|
|
fixStartingWithSpace = regexp.MustCompile(`(/|^) `)
|
2018-04-26 23:02:31 +02:00
|
|
|
|
fixEndingWithSpace = regexp.MustCompile(` (/|$)`)
|
|
|
|
|
invCharMap map[rune]rune
|
2017-07-17 07:36:45 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
// Create inverse charMap
|
|
|
|
|
invCharMap = make(map[rune]rune, len(charMap))
|
|
|
|
|
for k, v := range charMap {
|
|
|
|
|
invCharMap[v] = k
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// replaceReservedChars takes a path and substitutes any reserved
|
|
|
|
|
// characters in it
|
|
|
|
|
func replaceReservedChars(in string) string {
|
2018-04-26 23:02:31 +02:00
|
|
|
|
// Filenames can't start with space
|
2017-07-17 07:36:45 +02:00
|
|
|
|
in = fixStartingWithSpace.ReplaceAllString(in, "$1"+string(charMap[' ']))
|
2018-04-26 23:02:31 +02:00
|
|
|
|
// Filenames can't end with space
|
|
|
|
|
in = fixEndingWithSpace.ReplaceAllString(in, string(charMap[' '])+"$1")
|
2017-07-17 07:36:45 +02:00
|
|
|
|
return strings.Map(func(c rune) rune {
|
2018-04-26 23:02:31 +02:00
|
|
|
|
if replacement, ok := charMap[c]; ok && c != ' ' {
|
2017-07-17 07:36:45 +02:00
|
|
|
|
return replacement
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}, in)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// restoreReservedChars takes a path and undoes any substitutions
|
|
|
|
|
// made by replaceReservedChars
|
|
|
|
|
func restoreReservedChars(in string) string {
|
|
|
|
|
return strings.Map(func(c rune) rune {
|
|
|
|
|
if replacement, ok := invCharMap[c]; ok {
|
|
|
|
|
return replacement
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}, in)
|
|
|
|
|
}
|