strod.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "prinit.h"
  6. #include "prio.h"
  7. #include "prprf.h"
  8. #include "prdtoa.h"
  9. #include "plgetopt.h"
  10. #include <stdlib.h>
  11. static void Help(void)
  12. {
  13. PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
  14. PR_fprintf(err, "Usage: /.strod [-n n] [-l n] [-h]\n");
  15. PR_fprintf(err, "\t-n n Number to translate (default: 1234567890123456789)\n");
  16. PR_fprintf(err, "\t-l n Times to loop the test (default: 1)\n");
  17. PR_fprintf(err, "\t-h This message and nothing else\n");
  18. } /* Help */
  19. static PRIntn PR_CALLBACK RealMain(PRIntn argc, char **argv)
  20. {
  21. PLOptStatus os;
  22. PRIntn loops = 1;
  23. PRFloat64 answer;
  24. const char *number = "1234567890123456789";
  25. PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
  26. PLOptState *opt = PL_CreateOptState(argc, argv, "hc:l:");
  27. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  28. {
  29. if (PL_OPT_BAD == os) {
  30. continue;
  31. }
  32. switch (opt->option)
  33. {
  34. case 'n': /* number to translate */
  35. number = opt->value;
  36. break;
  37. case 'l': /* number of times to run the tests */
  38. loops = atoi(opt->value);
  39. break;
  40. case 'h': /* user wants some guidance */
  41. Help(); /* so give him an earful */
  42. return 2; /* but not a lot else */
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. PL_DestroyOptState(opt);
  49. PR_fprintf(err, "Settings\n");
  50. PR_fprintf(err, "\tNumber to translate %s\n", number);
  51. PR_fprintf(err, "\tLoops to run test: %d\n", loops);
  52. while (loops--)
  53. {
  54. answer = PR_strtod(number, NULL);
  55. PR_fprintf(err, "Translation = %20.0f\n", answer);
  56. }
  57. return 0;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. PRIntn rv;
  62. PR_STDIO_INIT();
  63. rv = PR_Initialize(RealMain, argc, argv, 0);
  64. return rv;
  65. } /* main */