// This contains routines specific to geocaching, probably for geocaching.com's // website. // // More general navigation algorithms are in navigation.frink // Convert a gscode to a number. The input value is a string and the output // is an integer. // // See http://gsak.net/board/index.php?showtopic=20930&st=0&#entry151466 gsCodeToNum[code] := { code = uc[code] if length[code] <= 3 return parseInt[code, 16] // 3 or fewer characters parse as hex. if code =~ %r/^[0-9A-F]{4}\$/ // Code is exactly 4 hex digits <= FFFF ? return parseInt[code, 16] // if so, parse as hex // Otherwise parse as base 31 with special character set (no ILOSU) // and subtract 411120 from the value. str = tr[uc[code], "0123456789ABCDEFGHJKMNPQRTVWXYZ", "0123456789ABCDEFGHIJKLMNOPQRSTU"] return parseInt[str, 31] - 411120 } // Convert a number to a gscode. The number should be passed in as a number, // and not a string (i.e. if it's a string call parseInt[str] on it first.) numToGSCode[num] := { // If the number fits into 4 hex characters, return as hex. if num <= 0xFFFF return uc[num -> base16] // Otherwise add 411120 to it and convert to base 31 with special character // set (no ILOSU). str = num + 411120 -> base31 return tr[uc[str], "0123456789ABCDEFGHIJKLMNOPQRSTU", "0123456789ABCDEFGHJKMNPQRTVWXYZ"] } // A simplified "tr" function like Perl. TODO: Make this part of Frink? tr[string, fromChars, toChars] := { trdict = new dict[zip[charList[fromChars], charList[toChars]]] result = new array for c = charList[string] if trdict@c == undef result.push[c] else result.push[trdict@c] return join["", result] } // Call these like: // // gsCodeToNum["103WQ"] // // numToGSCode[516144] // // (The above calls should be symmetrical.)