]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/convto.h
Add support for sending a standard reply with no command name.
[user/henk/code/inspircd.git] / include / convto.h
index 82f13c46dc8b592fc894f735e425e03757b229cb..1fc6848290bc84407b2c542890d68018ea8aef96 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
- *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
- *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
@@ -74,6 +74,11 @@ inline std::string ConvToStr(char in)
        return std::string(1, in);
 }
 
+inline const std::string& ConvToStr(const std::string& in)
+{
+       return in;
+}
+
 /** Template function to convert any input type to std::string
  */
 template <class T> inline std::string ConvToStr(const T& in)
@@ -84,22 +89,29 @@ template <class T> inline std::string ConvToStr(const T& in)
        return tmp.str();
 }
 
-/** Template function to convert any input type to any other type
- * (usually an integer or numeric type)
+/** Template function to convert a std::string to any numeric type.
  */
-template<typename T> inline long ConvToInt(const T& in)
-{
-       std::stringstream tmp;
-       if (!(tmp << in))
-               return 0;
-       return atol(tmp.str().c_str());
-}
-
-inline uint64_t ConvToUInt64(const std::string& in)
+template<typename TOut> inline TOut ConvToNum(const std::string& in)
 {
-       uint64_t ret;
+       TOut ret;
        std::istringstream tmp(in);
        if (!(tmp >> ret))
                return 0;
        return ret;
 }
+
+template<> inline char ConvToNum<char>(const std::string& in)
+{
+       // We specialise ConvToNum for char to avoid istringstream treating
+       // the input as a character literal.
+       int16_t num = ConvToNum<int16_t>(in);
+       return num >= INT8_MIN && num <= INT8_MAX ? num : 0;
+}
+
+template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in)
+{
+       // We specialise ConvToNum for unsigned char to avoid istringstream
+       // treating the input as a character literal.
+       uint16_t num = ConvToNum<uint16_t>(in);
+       return num <= UINT8_MAX ? num : 0;
+}