PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lbreakout2-2.6.4/common/tools.c

#
C | 385 lines | 254 code | 53 blank | 78 comment | 73 complexity | 3da1eb99b6a0806050119485afbabf4f MD5 | raw file
Possible License(s): AGPL-1.0
  1. /***************************************************************************
  2. tools.c - description
  3. -------------------
  4. begin : Fri Jan 19 2001
  5. copyright : (C) 2001 by Michael Speck
  6. email : kulkanie@gmx.net
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include <time.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "tools.h"
  21. /* compares to strings and returns true if their first strlen(str1) chars are equal */
  22. int strequal( const char *str1, const char *str2 )
  23. {
  24. if ( strlen( str1 ) != strlen( str2 ) ) return 0;
  25. return ( !strncmp( str1, str2, strlen( str1 ) ) );
  26. }
  27. /* set delay to ms milliseconds */
  28. void delay_set( Delay *delay, int ms )
  29. {
  30. delay->limit = ms;
  31. delay->cur = 0;
  32. }
  33. /* reset delay ( cur = 0 )*/
  34. void delay_reset( Delay *delay )
  35. {
  36. delay->cur = 0;
  37. }
  38. /* check if times out and reset */
  39. int delay_timed_out( Delay *delay, int ms )
  40. {
  41. delay->cur += ms;
  42. if ( delay->cur >= delay->limit ) {
  43. delay->cur = 0;
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. void goto_tile( int *x, int *y, int d )
  49. {
  50. /* 0 -up, clockwise, 5 - left up */
  51. switch ( d ) {
  52. case 1:
  53. if ( !( (*x) & 1 ) )
  54. (*y)--;
  55. (*x)++;
  56. break;
  57. case 2:
  58. if ( (*x) & 1 )
  59. (*y)++;
  60. (*x)++;
  61. break;
  62. case 4:
  63. if ( (*x) & 1 )
  64. (*y)++;
  65. (*x)--;
  66. break;
  67. case 5:
  68. if ( !( (*x) & 1 ) )
  69. (*y)--;
  70. (*x)--;
  71. break;
  72. }
  73. }
  74. /* return distance between to map positions */
  75. int get_dist( int x1, int y1, int x2, int y2 )
  76. {
  77. int range = 0;
  78. while ( x1 != x2 || y1 != y2 ) {
  79. /* approach to x2,y2 */
  80. /* 0 -up, clockwise, 5 - left up */
  81. if ( y1 < y2 ) {
  82. if ( x1 < x2 )
  83. goto_tile( &x1, &y1, 2 );
  84. else
  85. if ( x1 > x2 )
  86. goto_tile( &x1, &y1, 4 );
  87. else
  88. y1++;
  89. }
  90. else
  91. if ( y1 > y2 ) {
  92. if ( x1 < x2 )
  93. goto_tile( &x1, &y1, 1 );
  94. else
  95. if ( x1 > x2 )
  96. goto_tile( &x1, &y1, 5 );
  97. else
  98. y1--;
  99. }
  100. else {
  101. if ( x1 < x2 )
  102. x1++;
  103. else
  104. if ( x1 > x2 )
  105. x1--;
  106. }
  107. range++;
  108. }
  109. return range;
  110. }
  111. /* init random seed by using ftime */
  112. void set_random_seed()
  113. {
  114. srand( (unsigned int)time( 0 ) );
  115. }
  116. /* get coordinates from string */
  117. void get_coord( char *str, int *x, int *y )
  118. {
  119. int i;
  120. char *cur_arg = 0;
  121. *x = *y = 0;
  122. /* get position of comma */
  123. for ( i = 0; i < strlen( str ); i++ )
  124. if ( str[i] == ',' ) break;
  125. if ( i == strlen( str ) ) {
  126. fprintf( stderr, "get_coord: no comma found in pair of coordinates '%s'\n", str );
  127. return; /* no comma found */
  128. }
  129. /* y */
  130. cur_arg = str + i + 1;
  131. if ( cur_arg[0] == 0 )
  132. fprintf( stderr, "get_coord: warning: y-coordinate is empty (maybe you left a space between x and comma?)\n" );
  133. *y = atoi( cur_arg );
  134. /* x */
  135. cur_arg = strdup( str ); cur_arg[i] = 0;
  136. *x = atoi( cur_arg );
  137. FREE( cur_arg );
  138. }
  139. /* replace new_lines with spaces in text */
  140. void repl_new_lines( char *text )
  141. {
  142. int i;
  143. for ( i = 0; i < strlen( text ); i++ )
  144. if ( text[i] < 32 )
  145. text[i] = 32;
  146. }
  147. // convert a str into text ( for listbox ) //
  148. // char width is the width of a line in characters //
  149. Text* create_text( char *orig_str, int char_width )
  150. {
  151. int i, j;
  152. char line[256]; /* a line should not exceed this length */
  153. int pos;
  154. int last_space;
  155. int new_line;
  156. Text *text = 0;
  157. char *str = 0;
  158. text = calloc ( 1, sizeof( Text ) );
  159. // maybe orig_str is a constant expression; duplicate for safety //
  160. str = strdup( orig_str );
  161. // replace original new_lines with spaces //
  162. repl_new_lines( str );
  163. /* change some spaces to new_lines, so that the new text fits the wanted line_length */
  164. /* NOTE: '#' means new_line ! */
  165. // if character width is 0 it's just a single line //
  166. if ( char_width > 0 ) {
  167. pos = 0;
  168. while ( pos < strlen( str ) ) {
  169. last_space = 0;
  170. new_line = 0;
  171. i = 0;
  172. while ( !new_line && i < char_width && i + pos < strlen( str ) ) {
  173. switch ( str[pos + i] ) {
  174. case '#': new_line = 1;
  175. case 32: last_space = i; break;
  176. }
  177. i++;
  178. }
  179. if ( i + pos >= strlen( str ) ) break;
  180. if ( last_space == 0 ) {
  181. /* uhh... much to long, we'll have to cut a word into pieces ... */
  182. last_space = char_width / 2;
  183. }
  184. str[pos + last_space] = 10;
  185. pos += last_space;
  186. }
  187. }
  188. /* count lines */
  189. if ( char_width > 0 ) {
  190. for ( i = 0; i < strlen( str ); i++ )
  191. if ( str[i] == 10 )
  192. text->count++;
  193. /* maybe one unfinished line */
  194. if ( str[strlen( str ) - 1] != 10 )
  195. text->count++;
  196. }
  197. else
  198. text->count = 1;
  199. /* get mem */
  200. text->lines = calloc( text->count, sizeof( char* ) );
  201. pos = 0;
  202. /* get all lines */
  203. for ( j = 0; j < text->count; j++ ) {
  204. i = 0;
  205. while ( pos + i < strlen( str ) && str[pos + i] != 10 ) {
  206. line[i] = str[i + pos];
  207. i++;
  208. }
  209. pos += i; pos++;
  210. line[i] = 0;
  211. text->lines[j] = strdup( line );
  212. }
  213. if ( text->count == 0 )
  214. fprintf( stderr, "conv_to_text: warning: line_count is 0\n" );
  215. free( str );
  216. return text;
  217. }
  218. // delete text //
  219. void delete_text( Text *text )
  220. {
  221. int i;
  222. if ( text == 0 ) return;
  223. /*
  224. if ( lines[1][1] == 'e' && lines[1][0] == '<' )
  225. printf( "hallo\n" );
  226. printf( "--- deleting:\n" );
  227. for ( i = 0; i < line_count; i++ ) {
  228. printf( lines[i] );
  229. printf( "\n" );
  230. }*/
  231. if ( text->lines ) {
  232. for ( i = 0; i < text->count; i++ )
  233. if ( text->lines[i] )
  234. free( text->lines[i] );
  235. free( text->lines );
  236. }
  237. free( text );
  238. }
  239. /*
  240. ====================================================================
  241. Get type and prefix from string:
  242. type::prefix
  243. Set both pointers 0 if failure.
  244. ====================================================================
  245. */
  246. void get_type_and_prefix( char *arg, char **type, char **prefix )
  247. {
  248. char *first, *second;
  249. *type = *prefix = 0;
  250. first = strtok( arg, ":" );
  251. second = strtok( 0, ":" );
  252. if ( first == 0 || second == 0 ) return;
  253. *type = strdup( first );
  254. *prefix = strdup( second );
  255. }
  256. /*
  257. ====================================================================
  258. Replace any existence of character old into new.
  259. ====================================================================
  260. */
  261. void strrepl( char **str, char c_old, char c_new )
  262. {
  263. char *pos;
  264. while ( ( pos = strchr( *str, c_old ) ) != 0 )
  265. pos[0] = c_new;
  266. }
  267. /*
  268. ====================================================================
  269. Copy source to dest and at maximum limit chars. Terminate with 0.
  270. ====================================================================
  271. */
  272. void strcpy_lt( char *dest, char *src, int limit )
  273. {
  274. int len = strlen( src );
  275. if ( len > limit ) {
  276. strncpy( dest, src, limit );
  277. dest[limit] = 0;
  278. }
  279. else
  280. strcpy( dest, src );
  281. }
  282. /*
  283. ====================================================================
  284. Parse a version string and return the major version and the current
  285. update.
  286. ====================================================================
  287. */
  288. void parse_version( char *string, int *version, int *update )
  289. {
  290. char *ptr = strchr( string, '.' );
  291. if ( ptr ) ptr[0] = 0;
  292. *version = atoi( string );
  293. if ( ptr ) {
  294. ptr++;
  295. *update = atoi( ptr );
  296. if ( *update < 10 && ptr[0] != '0' )
  297. *update *= 10; /* allow stuff like 1.01 */
  298. ptr--;
  299. ptr[0] = '.';
  300. }
  301. else
  302. *update = 0;
  303. }
  304. /* allocate memory or exit with error if out of it */
  305. void *salloc( int num, int size )
  306. {
  307. void *ptr = calloc( num, size );
  308. if ( ptr == 0 ) {
  309. printf( "out of memory\n" );
  310. exit(1);
  311. }
  312. return ptr;
  313. }
  314. /* print contents of pointer raw */
  315. void print_raw( int len, char *buf )
  316. {
  317. int i;
  318. for ( i = 0; i < len; i++ )
  319. printf( "%02x ", (unsigned char) buf[i] );
  320. printf( "\n" );
  321. }
  322. /* check whether a string does only contain letters, digits or
  323. * underscores */
  324. int is_alphanum( char *str )
  325. {
  326. int i;
  327. for ( i = 0; i< strlen(str); i++ )
  328. if ( !((str[i]>=48&&str[i]<=57)||(str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122)||str[i]=='_') )
  329. return 0;
  330. return 1;
  331. }