config.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <fstream>
  6. #include <iostream>
  7. #include <cds_test/stress_test.h>
  8. namespace cds_test {
  9. class config_file
  10. {
  11. std::map< std::string, config> m_cfg;
  12. config m_emptyCfg;
  13. public:
  14. void load( const char * fileName );
  15. config const& operator[]( const std::string& testName ) const
  16. {
  17. auto it = m_cfg.find( testName );
  18. if ( it != m_cfg.end())
  19. return it->second;
  20. return m_emptyCfg;
  21. }
  22. };
  23. void config_file::load( const char * fileName )
  24. {
  25. std::ifstream s;
  26. s.open( fileName );
  27. if ( !s.is_open()) {
  28. std::cerr << "WARNING: Cannot open test cfg file " << fileName
  29. << "\n\tUse default settings"
  30. << std::endl;
  31. return;
  32. }
  33. std::cout << "Using test config file: " << fileName << std::endl;
  34. char buf[4096];
  35. config * pMap = nullptr;
  36. while ( !s.eof()) {
  37. s.getline( buf, sizeof( buf ) / sizeof( buf[0] ));
  38. char * pszStr = buf;
  39. // trim left
  40. while ( *pszStr != 0 && (*pszStr == ' ' || *pszStr == '\t')) ++pszStr;
  41. // trim right
  42. char * pszEnd = strchr( pszStr, 0 );
  43. if ( pszEnd == pszStr ) // empty srtring
  44. continue;
  45. --pszEnd;
  46. while ( pszEnd != pszStr && (*pszEnd == ' ' || *pszEnd == '\t' || *pszEnd == '\n' || *pszEnd == '\r')) --pszEnd;
  47. if ( pszStr == pszEnd ) // empty string
  48. continue;
  49. pszEnd[1] = 0;
  50. if ( *pszStr == '#' ) // comment
  51. continue;
  52. if ( *pszStr == '[' && *pszEnd == ']' ) { // chapter header
  53. *pszEnd = 0;
  54. pMap = &(m_cfg[pszStr + 1]);
  55. continue;
  56. }
  57. if ( !pMap )
  58. continue;
  59. char * pszEq = strchr( pszStr, '=' );
  60. if ( !pszEq )
  61. continue;
  62. if ( pszEq == pszStr )
  63. continue;
  64. pszEnd = pszEq;
  65. while ( pszStr <= --pszEnd && (*pszEnd == ' ' || *pszEnd == '\t' || *pszEnd == '\n' || *pszEnd == '\r'));
  66. if ( pszEnd <= pszStr )
  67. continue;
  68. pszEnd[1] = 0;
  69. pMap->m_Cfg[pszStr] = pszEq + 1;
  70. }
  71. s.close();
  72. }
  73. static config_file s_cfg;
  74. void init_config( int argc, char **argv )
  75. {
  76. #if defined(_DEBUG) || !defined(NDEBUG)
  77. char const * default_cfg_file = "./test-debug.conf";
  78. #else
  79. char const * default_cfg_file = "./test.conf";
  80. #endif
  81. char const * cfg_file = NULL;
  82. for ( int i = 0; i < argc; ++i ) {
  83. char * arg = argv[i];
  84. char * eq = strchr( arg, '=' );
  85. if ( eq ) {
  86. if ( strncmp( arg, "--cfg", eq - arg ) == 0 )
  87. cfg_file = eq + 1;
  88. }
  89. }
  90. if ( !cfg_file ) {
  91. // Get cfg filename from environment variable
  92. cfg_file = getenv( "CDSTEST_CFG" );
  93. }
  94. if ( !cfg_file || *cfg_file == 0 )
  95. cfg_file = default_cfg_file;
  96. ::testing::Test::RecordProperty( "config_file", cfg_file );
  97. s_cfg.load( cfg_file );
  98. }
  99. /*static*/ config const& stress_fixture::get_config( char const * slot )
  100. {
  101. return s_cfg[std::string( slot )];
  102. }
  103. /*static*/ config const& stress_fixture::get_config( std::string const& slot )
  104. {
  105. return s_cfg[ slot ];
  106. }
  107. } // namespace cds_test