/src/xylose/compat/unistd.hpp

https://github.com/hpcdev/xylose · C++ Header · 113 lines · 58 code · 28 blank · 27 comment · 1 complexity · ed3b715f5ca1f91304895e72a332818e MD5 · raw file

  1. /*==============================================================================
  2. * Public Domain Contributions 2010 United States Government *
  3. * as represented by the U.S. Air Force Research Laboratory. *
  4. * Portions copyright Copyright (C) 2010 Stellar Science *
  5. * *
  6. * This file is part of xylose *
  7. * *
  8. * This program is free software: you can redistribute it and/or modify it *
  9. * under the terms of the GNU Lesser General Public License as published by *
  10. * the Free Software Foundation, either version 3 of the License, or (at your *
  11. * option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT *
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
  16. * License for more details. *
  17. * *
  18. * You should have received a copy of the GNU Lesser General Public License *
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  20. * *
  21. -----------------------------------------------------------------------------*/
  22. /** \file
  23. * Windows compatibility layer for <unistd.h>.
  24. */
  25. #ifndef xylose_compat_unistd_hpp
  26. #define xylose_compat_unistd_hpp
  27. #ifndef WIN32
  28. # include <unistd.h>
  29. #else
  30. #include <xylose/compat/config.hpp>
  31. #if defined(USE_WINSOCK)
  32. // you will also have to link to ws2_32.lib
  33. # include <winsock2.h> // for gethostname
  34. # pragma comment(lib, "ws2_32.lib")
  35. #endif
  36. #include <windows.h>
  37. #include <direct.h> // for _getcwd, chdir
  38. #include <io.h> // for _unlink
  39. #include <stdio.h> // for _unlink
  40. #include <ctime>
  41. #if defined(_MSC_VER)
  42. #undef min
  43. #undef max
  44. #endif
  45. namespace xylose {
  46. namespace compat {
  47. inline void sleep(int t) {
  48. Sleep(t*1000); // Sleep uses milliseconds
  49. }
  50. #ifndef USE_WINSOCK
  51. inline void gethostname(char*name, const int len) {
  52. strncat(name,"WindozeBox",std::min(10,len-1));
  53. }
  54. #endif
  55. inline unsigned long getpid() {
  56. return static_cast<unsigned long>( GetCurrentProcessId() );
  57. }
  58. static const int _SC_CLK_TCK = 1;
  59. inline long sysconf(int name) {
  60. switch (name) {
  61. case _SC_CLK_TCK:
  62. return CLOCKS_PER_SEC;
  63. break;
  64. default:
  65. return -1;
  66. }
  67. }
  68. #if WIN32_POSIX_REMOVED_COMPLETELY
  69. //MSDN docs say that these won't exist(?), but they seem to...
  70. inline char * getcwd(char * buf, const size_t & size) {
  71. return _getcwd(buf, size);
  72. }
  73. inline int chdir(const char * path) {
  74. return _chdir(path);
  75. }
  76. inline int unlink( const char *path ) {
  77. return _unlink(path);
  78. }
  79. #endif
  80. }/* namespace xylose::compat */
  81. }/* namespace xylose::compat */
  82. /* import xylose::compat into :: */
  83. using namespace xylose::compat;
  84. #endif // WIN32
  85. #endif // xylose_compat_unistd_hpp