ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
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.
 
 
 
 
 
 

77 lines
1.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_IMPL_FORMAT_NUMBER_H
  9. #define CPPCMS_IMPL_FORMAT_NUMBER_H
  10. #include <locale>
  11. #include <sstream>
  12. #include <iomanip>
  13. #include <limits>
  14. #include <string.h>
  15. namespace cppcms {
  16. namespace impl {
  17. template<typename Value,bool is_integer=std::numeric_limits<Value>::is_integer>
  18. struct format_traits;
  19. template<typename Value>
  20. struct format_traits<Value,true> {
  21. static void format(Value v,char *output,size_t n)
  22. {
  23. char buf[std::numeric_limits<Value>::digits10 + 10];
  24. char *begin = buf;
  25. if(v < 0) {
  26. *begin ++ = '-';
  27. }
  28. if(v == 0) {
  29. *begin ++ ='0';
  30. *begin = 0;
  31. }
  32. else {
  33. char *p=begin;
  34. while(v != 0) {
  35. int digit = v % 10;
  36. v/=10;
  37. if(digit < 0)
  38. *p++='0' - digit;
  39. else
  40. *p++='0' + digit;
  41. }
  42. *p-- = 0;
  43. while(begin < p) {
  44. std::swap(*begin,*p);
  45. begin++;
  46. p--;
  47. }
  48. }
  49. strncpy(output,buf,n-1);
  50. output[n-1] = 0;
  51. }
  52. };
  53. template<typename Value>
  54. struct format_traits<Value,false> {
  55. static void format(Value v,char *output,size_t n)
  56. {
  57. std::ostringstream ss;
  58. ss.imbue(std::locale::classic());
  59. ss<<std::setprecision(std::numeric_limits<double>::digits10+1)<<v;
  60. strncpy(output,ss.str().c_str(),n-1);
  61. output[n-1] = 0;
  62. }
  63. };
  64. template<typename Value>
  65. void format_number(Value v,char *begin,size_t n)
  66. {
  67. format_traits<Value>::format(v,begin,n);
  68. }
  69. }
  70. }
  71. #endif