api_utils.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #include "api/api_utils.h"
  12. #include <time.h>
  13. #include <string>
  14. using namespace std;
  15. namespace MCell {
  16. namespace API {
  17. Orientation convert_mcell_orientation(const orientation_t o) {
  18. switch (o) {
  19. case ORIENTATION_DOWN:
  20. return Orientation::DOWN;
  21. case ORIENTATION_NONE:
  22. return Orientation::NONE;
  23. case ORIENTATION_UP:
  24. return Orientation::UP;
  25. case ORIENTATION_NOT_SET:
  26. return Orientation::NOT_SET;
  27. default:
  28. assert(false);
  29. return Orientation::NOT_SET;
  30. }
  31. }
  32. orientation_t convert_api_orientation(const Orientation o, const bool allow_default, const bool is_vol) {
  33. switch (o) {
  34. case Orientation::DEFAULT:
  35. if (!allow_default) {
  36. throw ValueError("Invalid Orientation value " + to_string((int)o) + " (DEFAULT).");
  37. }
  38. if (is_vol) {
  39. return ORIENTATION_NONE;
  40. }
  41. else {
  42. return ORIENTATION_UP;
  43. }
  44. case Orientation::DOWN:
  45. return ORIENTATION_DOWN;
  46. case Orientation::NONE:
  47. return ORIENTATION_NONE;
  48. case Orientation::UP:
  49. return ORIENTATION_UP;
  50. case Orientation::NOT_SET:
  51. throw ValueError("Invalid Orientation value " + to_string((int)o) + ".");
  52. case Orientation::ANY:
  53. return ORIENTATION_NONE;
  54. default:
  55. throw ValueError("Invalid Orientation value " + to_string((int)o) + ".");
  56. }
  57. }
  58. bool is_simple_species(const std::string& name) {
  59. // complex species always contain a parenthesis in their name
  60. return name.find('(') == std::string::npos;
  61. }
  62. // get current date/time, format is YYYY-MM-DD HH:mm:ss
  63. const std::string get_current_date_time() {
  64. time_t now = time(0);
  65. struct tm tstruct;
  66. char buf[80];
  67. tstruct = *localtime(&now);
  68. strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
  69. return buf;
  70. }
  71. } // namespace API
  72. } // namespace MCell