/* vim:set ts=4 sw=4 sts=4 et cindent: */ /* * nanodc - The ncurses DC++ client * Copyright © 2005-2006 Markus Lindqvist * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * Contributor(s): * */ #ifndef _UTILS_H_ #define _UTILS_H_ #include #include #include #include #include #include #include #include #include #include #include namespace utils { static std::string empty_string; class rot13_traits: public std::char_traits { public: static char *copy(char *s1, const char *s2, std::size_t n) { while(n--) { if(std::isalpha(*s2)) { if(toupper(*s2) < ('A' + 13)) *s1 = *s2+13; else *s1 = *s2-13; } else { *s1 = *s2; } *s1++; *s2++; } return s1-n; } }; typedef std::basic_string rot13_string; /** Sleep @param usec Microseconds to sleep */ void sleep(int usec); /** Convert \ -> / */ std::string linux_separator(const std::string &ps); /** Convert / -> \ */ std::string windows_separator(const std::string &ps); template std::string to_string(T s) { std::ostringstream oss; oss << s; return oss.str(); } /** Removes the last element of the container. * Does nothing if the container is empty. */ template Container &remove_last(Container &container) { if(container.length() != 0) container.erase(container.size()-1); return container; } /** Convert a string to T. */ template T to(const std::string &str) { T t; std::istringstream iss(str); iss >> t; return t; } /** */ template bool find_in_string(const std::string &str, Iterator start, Iterator end) { while(start != end) { if(str.find(*start) != std::string::npos) return true; ++start; } return false; } /** Get string representation of current time. @param format Format eg. "[%H:%M:%S]" */ std::string time_to_string(const std::string &format); std::string time_to_string(const std::string &format, time_t time); /** Returns $HOME/.dc++/ */ std::string get_settingdir(); /** Delete. */ template struct delete_functor { void operator()(T *t) { delete t; t = 0; } }; std::string to_utf8(const std::string &str) throw(); std::string from_utf8(const std::string &str) throw(); std::string convert(const std::string &str, const std::string &from, const std::string &to) throw(std::runtime_error); inline long strlen(const std::string &str) throw() { return g_utf8_strlen(str.c_str(), -1); } /** Returns the time since the Epoch in milliseconds. */ inline uint32_t get_millisecs() { timeval tv; gettimeofday(&tv, NULL); return (uint32_t)((tv.tv_sec) * 1000 ) + ( (tv.tv_usec) / 1000); } std::string tolower(const std::string &str) throw(); /** Converts an IP-address to hostname. Eg. 66.35.250.150 -> slashdot.org */ std::string ip_to_host(const std::string &ip); /** Creates a vector from a list of strings. */ std::vector make_vector(int n, ...); int utf16_char_to_utf8(int c, char *outbuf); /** gettid() system call number */ const unsigned int SYS_gettid = 224; /** Returns thread id of the calling thread. * Implemented only if \c __i386__ is defined. */ inline static pid_t gettid() { pid_t tid = 0; #ifdef __i386__ __asm__("int $0x80" : "=a" (tid) : "0" (SYS_gettid)); #endif return tid; } /** Singleton class that holds thread id for the ncurses thread */ class NCursesTID: public Instance { public: void setTID(pid_t tid) { m_tid = tid; } pid_t getTID() { return m_tid; } private: pid_t m_tid; }; /** Checks if current thread id matches */ inline void CHECK_NCURSES_THREAD(const std::string &file, const std::string &func, int line) { if(utils::gettid() != NCursesTID::get()->getTID()) { core::Log::get()->log("%21Warning:%21 This thread should not do ncurses operations"); core::Log::get()->log("In " + file + ":" + utils::to_string(line) + " " + func); if(cow::StackTrace::is_enabled()) { std::ostringstream oss; cow::StackTrace trace; trace.generate_frames(); std::copy(trace.begin(), trace.end(), std::ostream_iterator(oss, "\n")); core::Log::get()->log(oss.str()); } else { core::Log::get()->log("Stack not available"); } } } } // namespace utils #define CHECK_NCURSES_THREAD() utils::CHECK_NCURSES_THREAD(__FILE__, __FUNCTION__, __LINE__) #endif // _UTILS_H_