You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

random.cc 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. random - GPS data generator
  3. Copyright (C) 2007 Olaf Klein, o.b.klein@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "garmin_fs.h"
  18. #include "jeeps/gpsmath.h"
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #define MYNAME "random"
  22. static char* opt_points, *opt_seed;
  23. static arglist_t random_args[] = {
  24. {
  25. "points", &opt_points, "Generate # points", NULL,
  26. ARGTYPE_INT, "1", NULL
  27. },
  28. {
  29. "seed", &opt_seed, "Starting seed of the internal number generator", NULL,
  30. ARGTYPE_INT, "1", NULL
  31. },
  32. ARG_TERMINATOR
  33. };
  34. static double
  35. rand_dbl(const double max)
  36. {
  37. return max * rand() / (((double)RAND_MAX) + 1);
  38. }
  39. static int
  40. rand_int(const int max)
  41. {
  42. return (int)((double)max * rand() / (((double)RAND_MAX) + 1));
  43. }
  44. /* rand_str always returns a valid string with len >= 0 */
  45. static char*
  46. rand_str(const int maxlen, const char* fmt)
  47. {
  48. char* res;
  49. int i, len;
  50. len = rand_int(maxlen) + 1;
  51. res = (char*) xmalloc(len + 1);
  52. res[len] = '\0';
  53. for (i = 0; i < len; i++) {
  54. int c = rand_int(26 + 26 + 10);
  55. if (c < 26) {
  56. c += 'a';
  57. } else if (c < 52) {
  58. c = (c - 26) + 'A';
  59. } else {
  60. c = (c - 52) + '0';
  61. }
  62. res[i] = c;
  63. }
  64. if (fmt) {
  65. char* tmp;
  66. xasprintf(&tmp, fmt, res);
  67. xfree(res);
  68. return tmp;
  69. } else {
  70. return res;
  71. }
  72. }
  73. static QString
  74. rand_qstr(const int maxlen, const char* fmt)
  75. {
  76. char * str = rand_str(maxlen, fmt);
  77. QString qstr = QString(str);
  78. xfree(str);
  79. return qstr;
  80. }
  81. static void
  82. random_rd_init(const QString& fname)
  83. {
  84. }
  85. static void
  86. random_rd_deinit(void)
  87. {
  88. }
  89. static void
  90. random_read(void)
  91. {
  92. #define RND(a) (rand_int(a) > 0)
  93. int i, points;
  94. route_head* head;
  95. Waypoint* prev = NULL;
  96. time_t time = gpsbabel_time;
  97. if (opt_seed) {
  98. srand(atoi(opt_seed));
  99. } else {
  100. srand(gpsbabel_now);
  101. }
  102. points = (opt_points) ? atoi(opt_points) : rand_int(128) + 1;
  103. if (doing_trks || doing_rtes) {
  104. head = route_head_alloc();
  105. if (doing_trks) {
  106. head->rte_name = rand_qstr(8, "Trk_%s");
  107. track_add_head(head);
  108. } else {
  109. head->rte_name = rand_qstr(8, "Rte_%s");
  110. route_add_head(head);
  111. }
  112. head->rte_desc = rand_qstr(16, NULL);
  113. if RND(3) {
  114. head->rte_url = rand_qstr(8, "http://rteurl.example.com/%s");
  115. }
  116. } else {
  117. head = NULL;
  118. }
  119. for (i = 0; i < points; i++) {
  120. Waypoint* wpt;
  121. garmin_fs_t* gmsd;
  122. wpt = new Waypoint;
  123. gmsd = garmin_fs_alloc(-1);
  124. fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
  125. do {
  126. wpt->shortname = rand_qstr(8, "Wpt_%s");
  127. } while (wpt->shortname == NULL);
  128. wpt->latitude = rand_dbl(180) - 90;
  129. wpt->longitude = rand_dbl(360) - 180;
  130. /* !!! "if RND(3) ..." produces some leaks in generated data !!! */
  131. if RND(3) {
  132. wpt->altitude = rand_int(1000) / 10;
  133. }
  134. if RND(3) {
  135. WAYPT_SET(wpt, temperature, rand_int(320) / 10.0);
  136. }
  137. if RND(3) {
  138. WAYPT_SET(wpt, proximity, rand_int(10000) / 10.0);
  139. }
  140. if RND(3) {
  141. WAYPT_SET(wpt, depth, rand_int(10000) / 10.0);
  142. }
  143. if RND(3) {
  144. wpt->AddUrlLink(rand_qstr(8, "http://link1.example.com/%s"));
  145. if RND(3) {
  146. wpt->AddUrlLink(rand_qstr(8, "http://link2.example.com/%s"));
  147. }
  148. }
  149. if RND(3) {
  150. wpt->icon_descr = rand_qstr(3, "Icon_%s");
  151. }
  152. wpt->SetCreationTime(time);
  153. if RND(3) {
  154. wpt->creation_time = wpt->creation_time.addMSecs(rand_int(1000) * 1000);
  155. }
  156. time += rand_int(10) + 1;
  157. if (doing_trks) {
  158. if (i > 0) {
  159. wpt->latitude = prev->latitude + (rand_dbl(1) / 1000);
  160. wpt->longitude = prev->longitude + (rand_dbl(1) / 1000);
  161. WAYPT_SET(wpt, course, waypt_course(prev, wpt));
  162. WAYPT_SET(wpt, speed, waypt_speed(prev, wpt));
  163. }
  164. wpt->sat = rand_int(12 + 1);
  165. wpt->hdop = (rand_int(500)) / 10.0;
  166. wpt->vdop = (rand_int(500)) / 10.0;
  167. wpt->pdop = (rand_int(500)) / 10.0;
  168. wpt->fix = (fix_type)(rand_int(6) - 1);
  169. if RND(3) {
  170. wpt->cadence = rand_int(255);
  171. }
  172. if RND(3) {
  173. wpt->heartrate = rand_int(255);
  174. }
  175. } else {
  176. if (doing_rtes && (i > 0)) {
  177. wpt->latitude = prev->latitude + (rand_dbl(1) / 100);
  178. wpt->longitude = prev->longitude + (rand_dbl(1) / 100);
  179. }
  180. if RND(3) {
  181. wpt->description = rand_qstr(16, "Des_%s");
  182. }
  183. if RND(3) {
  184. wpt->notes = rand_qstr(16, "Nts_%s");
  185. }
  186. if RND(3) {
  187. GMSD_SET(addr, rand_str(8, "Adr_%s"));
  188. }
  189. if RND(3) {
  190. GMSD_SET(city, rand_str(8, "Cty_%s"));
  191. }
  192. if RND(3) {
  193. GMSD_SET(facility, rand_str(8, "Fac_%s"));
  194. }
  195. if RND(3) {
  196. GMSD_SET(country, rand_str(8, "Ctr_%s"));
  197. }
  198. if RND(3) {
  199. GMSD_SET(state, rand_str(8, "Sta_%s"));
  200. }
  201. if RND(3) {
  202. GMSD_SET(phone_nr, rand_str(8, "Pnr_%s"));
  203. }
  204. if RND(3) {
  205. GMSD_SET(postal_code, rand_str(8, "Pcd_%s"));
  206. }
  207. }
  208. if (doing_trks) {
  209. track_add_wpt(head, wpt);
  210. } else if (doing_rtes) {
  211. route_add_wpt(head, wpt);
  212. } else {
  213. waypt_add(wpt);
  214. }
  215. prev = wpt;
  216. }
  217. }
  218. ff_vecs_t random_vecs = {
  219. ff_type_internal,
  220. {
  221. ff_cap_read /* waypoints */,
  222. ff_cap_read /* tracks */,
  223. ff_cap_read /* routes */
  224. },
  225. random_rd_init,
  226. NULL, /* wr_init */
  227. random_rd_deinit,
  228. NULL, /* wr_deinit */
  229. random_read,
  230. NULL, /* write */
  231. NULL, /* exit */
  232. random_args,
  233. CET_CHARSET_ASCII, 1 /* fixed */
  234. };