Skaruts

sk_utils.h

Apr 29th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #ifndef SK_UTILS_H
  2. #define SK_UTILS_H
  3.  
  4. /************************************************************/
  5. /*  Tools for converting to and from                        */
  6. /*  C/C++ strings into basic types                          */
  7. /*  bool, int and float                                     */
  8. /************************************************************/
  9.  
  10. #include <sstream>     
  11. #include <iomanip>      // for setprecision(n)
  12. #include <stdlib.h>     // for itoa()/itof()
  13.  
  14. using namespace std;    // because I like readable code!
  15.  
  16. namespace sku
  17. {
  18.     // default floating point precision
  19.         extern int8_t DFP;     
  20.     // floating point precision value
  21.         extern int8_t FP;
  22.  
  23.     void setFP(uint16_t p = DFP);
  24.  
  25.  
  26.     /************************************************************/
  27.     /*      To C string                                         */
  28.     /************************************************************/
  29.         const char* tocs(bool, bool alpha = false);     // From bool
  30.         const char* tocs(int32_t);                      // From int
  31.         const char* tocs(float, uint8_t p = FP);        // From float
  32.         const char* tocs(string);                       // From C++ string - for convenience
  33.  
  34.     /************************************************************/
  35.     /*      To C++ string                                       */
  36.     /************************************************************/
  37.         string tos(bool, bool alpha = false);           // From bool
  38.         string tos(int32_t);                            // From int
  39.         string tos(uint32_t);  
  40.         string tos(int64_t);
  41.         string tos(uint64_t);                           // From int
  42.         string tos(float, uint8_t p = FP);              // From float
  43.         string tos(double, uint8_t p = FP);
  44.         string tos(const char*);                        // From C string
  45.  
  46.     /************************************************************/
  47.     /*      To Bool                                             */
  48.     /************************************************************/
  49.         bool stob(const char*);     // C string to bool
  50.         bool stob(string);          // C++ string to bool
  51.  
  52.     /************************************************************/
  53.     /*      To Int                                              */
  54.     /************************************************************/
  55.         int32_t stoi(const char* c);                    // C string to int
  56.         int32_t stoi(string);                           // C++ string to int
  57.  
  58.     /************************************************************/
  59.     /*      To Float                                            */
  60.     /************************************************************/
  61.         float stof(const char*);                        // C string to float (double?)
  62.         float stof(string);                             // C++ string to float (double?)
  63.  
  64.  
  65. } // namespace sku
  66. #endif
Advertisement
Add Comment
Please, Sign In to add comment