PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/sq/tmp/sq/js/strings.js

https://github.com/wheresjames/winglib
JavaScript | 308 lines | 185 code | 58 blank | 65 comment | 58 complexity | bdf27fd0d90f402c85c95ea0ac6d97d0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*------------------------------------------------------------------
  2. // strings.js
  3. // Robert Umbehant
  4. // rumbehant@wheresjames.com
  5. // WheresJames Software (www.wheresjames.com)
  6. //
  7. // This file is covered by a 'BSD' style license as follows...
  8. //
  9. // Copyright (c) 2000, 2003, 2004
  10. // Robert Umbehant
  11. // All rights reserved.
  12. //
  13. // Redistribution and use in source and binary forms, with or
  14. // without modification, are permitted for commercial and
  15. // non-commercial purposes, provided that the following
  16. // conditions are met:
  17. //
  18. // * Redistributions of source code must retain the above copyright
  19. // notice, this list of conditions and the following disclaimer.
  20. // * Redistributions in binary form must reproduce the above
  21. // copyright notice, this list of conditions and the following
  22. // disclaimer in the documentation and/or other materials
  23. // provided with the distribution.
  24. // * Neither the name of WheresJames, Robert Umbehant, or the names
  25. // of its contributors may be used to endorse or promote products
  26. // derived from this software without specific prior written
  27. // permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  32. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  40. // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  41. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. //----------------------------------------------------------------*/
  43. function MakeParams( x_params )
  44. {
  45. var ret = '';
  46. for ( var key in x_params )
  47. {
  48. if ( key && x_params[ key ] && x_params[ key ].constructor != Function )
  49. {
  50. // Continue link
  51. if ( ret ) ret += '&';
  52. if( x_params[ key ].constructor == Array ||
  53. x_params[ key ].constructor == Object )
  54. {
  55. ret += key + '=?' + escape( MakeParams( x_params[ key ] ) );
  56. }
  57. else ret += key + '=' + escape( x_params[ key ] );
  58. } // end if
  59. }
  60. return ret;
  61. }
  62. function ParseParams( x_params )
  63. {
  64. var ret = {};
  65. var parr = x_params.split( '&' );
  66. for ( var i = 0; i < parr.length; i++ )
  67. {
  68. kv = parr[ i ].split( '=' );
  69. if ( kv && kv[ 1 ] != null )
  70. { if ( kv[ 1 ].charAt( 0 ) != '?' )
  71. ret[ kv[ 0 ] ] = unescape( kv[ 1 ] );
  72. else ret[ kv[ 0 ] ] = ParseParams( unescape( kv[ 1 ].substr( 1 ) ) );
  73. } // end if
  74. }
  75. return ret;
  76. }
  77. function SetDivTxt( x_div, x_txt )
  78. { var t = document.getElementById( x_div );
  79. if ( t ) { t.innerHTML = x_txt; }
  80. }
  81. function SetDivImg( x_div, x_img )
  82. { SetDivTxt( x_div, '<img src="' + x_img + '">' );
  83. }
  84. function print_r( theObj )
  85. {
  86. var str = "";
  87. if( theObj.constructor != Function )
  88. {
  89. str = "<ul>";
  90. for( var p in theObj )
  91. {
  92. if ( theObj[ p ].constructor != Function )
  93. {
  94. if( theObj[ p ].constructor == Array ||
  95. theObj[ p ].constructor == Object )
  96. {
  97. str += "<li>" + p + " = Array(...)</li>";
  98. str += "<ul>";
  99. str += print_r( theObj[ p ] );
  100. str += "</ul>";
  101. }
  102. else
  103. {
  104. str += "<li>" + p + " = " + theObj[ p ] + " </li>";
  105. }
  106. }
  107. }
  108. str += "</ul>";
  109. }
  110. return str;
  111. }
  112. function PadNum( n, pre, post, c )
  113. { n = n.toString();
  114. if ( !c ) c = '0';
  115. while( n.length < pre ) n = c + n;
  116. while( n.length < post ) n = n + c;
  117. return n;
  118. }
  119. function sprintf( tmpl )
  120. {
  121. var i = 0;
  122. var str = '';
  123. var argn = 1;
  124. // Process the template
  125. while ( i < tmpl.length )
  126. {
  127. // If not the escape character
  128. if ( tmpl.charAt( i ) != '%' ) str += tmpl.charAt( i ).toString();
  129. // Replace escape sequence
  130. else
  131. {
  132. // Skip %
  133. i++;
  134. var pre = 0, post = 0;
  135. // Get prefix
  136. if ( tmpl.charAt( i ) >= '0' && tmpl.charAt( i ) <= '9' )
  137. pre = tmpl.charAt( i++ );
  138. // Get postfix
  139. if ( tmpl.charAt( i ) == '.' &&
  140. tmpl.charAt( ++i ) >= '0' &&
  141. tmpl.charAt( i ) <= '9' )
  142. post = tmpl.charAt( i++ );
  143. switch( tmpl.charAt( i ) )
  144. {
  145. case 'u' :
  146. str += parseInt( arguments[ argn++ ] );
  147. break;
  148. case 'f' :
  149. var n = arguments[ argn++ ];
  150. n = Math.round( parseFloat( n ) * 100 ) / 100;
  151. fstr = new String( n );
  152. d = fstr.indexOf( '.' );
  153. if ( d >= 0 )
  154. { var n1 = parseInt( fstr.substr( 0, d ) );
  155. var n2 = parseInt( fstr.substr( d + 1 ) );
  156. if ( isNaN( n1 ) ) n1 = 0;
  157. if ( isNaN( n2 ) ) n2 = 0;
  158. str = PadNum( n1.toString(), pre, 0 );
  159. str += '.';
  160. str += PadNum( n2.toString(), 0, post );
  161. } // end if
  162. else
  163. {
  164. if ( isNaN( n ) ) str = PadNum( 0, 0, pre );
  165. else str = PadNum( n.toString(), pre, 0 );
  166. str += '.';
  167. str += PadNum( 0, 0, post );
  168. } // end else
  169. break;
  170. default :
  171. str += tmpl.charAt( i ).toString();
  172. break;
  173. }; // end switch
  174. } // end else
  175. // Next character
  176. i++;
  177. } // end while
  178. return str;
  179. }
  180. function ScsSerialize( x_params )
  181. {
  182. var ret = '';
  183. for ( var key in x_params )
  184. {
  185. if ( key && x_params[ key ] )
  186. {
  187. // Continue link
  188. if ( ret ) ret += ',';
  189. // Save the key
  190. ret += escape( key );
  191. if( x_params[ key ].constructor == Array ||
  192. x_params[ key ].constructor == Object )
  193. {
  194. ret += '{' + ScsSerialize( x_params[ key ] ) + '}';
  195. }
  196. else ret += '=' + escape( x_params[ key ] );
  197. } // end if
  198. } // end for
  199. return ret;
  200. }
  201. function ScsDeserialize( x_params, x_arr )
  202. {
  203. // if ( x_arr == "undefined" )
  204. // x_arr = {};
  205. x_params += ',';
  206. var l = x_params.length, s = 0, e = 0;
  207. while ( e < l )
  208. {
  209. switch( x_params[ e ] )
  210. {
  211. case ',' : case '}' :
  212. {
  213. var a = x_params.substr( s, e - s ).split( '=' );
  214. if ( 1 < e - s )
  215. {
  216. // Valid?
  217. if ( null == a[ 0 ] ) a[ 0 ] = 0;
  218. // Decode
  219. else a[ 0 ] = unescape( a[ 0 ] );
  220. // Single value?
  221. if ( null == a[ 1 ] ) x_arr[ 0 ] = '';
  222. // Key / value pair
  223. else x_arr[ a[ 0 ] ] = unescape( a[ 1 ] );
  224. } // end if
  225. // Next data
  226. s = e + 1;
  227. // Punt if end of array
  228. if ( '}' == x_params[ e ] ) return e + 1;
  229. } break;
  230. case '{' :
  231. {
  232. // Get the key
  233. var k = x_params.substr( s, e - s );
  234. if ( k.length )
  235. {
  236. // Decode the key
  237. k = unescape( k );
  238. // Decode array
  239. x_arr[ k ] = Array();
  240. e += ScsDeserialize( x_params.substr( e ), x_arr[ k ] );
  241. } // end if
  242. // Next data
  243. s = e + 1;
  244. } break;
  245. } // end switch
  246. // Next e
  247. e++;
  248. } // end while
  249. return e;
  250. }