MAPIConsoleTable.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include "MAPIConsoleTable.h"
  19. #include "ConsoleTable.h"
  20. #include <kopano/mapi_ptr.h>
  21. #include <kopano/stringutil.h>
  22. namespace KC {
  23. static std::string ToString(const SPropValue *lpProp)
  24. {
  25. switch(PROP_TYPE(lpProp->ulPropTag)) {
  26. case PT_STRING8:
  27. return std::string(lpProp->Value.lpszA);
  28. case PT_LONG:
  29. return stringify(lpProp->Value.ul);
  30. case PT_DOUBLE:
  31. return stringify(lpProp->Value.dbl);
  32. case PT_FLOAT:
  33. return stringify(lpProp->Value.flt);
  34. case PT_I8:
  35. return stringify_int64(lpProp->Value.li.QuadPart);
  36. case PT_SYSTIME:
  37. {
  38. time_t t;
  39. char buf[32]; // must be at least 26 bytes
  40. FileTimeToUnixTime(lpProp->Value.ft, &t);
  41. ctime_r(&t, buf);
  42. return trim(buf, " \t\n\r\v\f");
  43. }
  44. case PT_MV_STRING8:
  45. {
  46. std::string s;
  47. for (unsigned int i = 0; i < lpProp->Value.MVszA.cValues; ++i) {
  48. if(!s.empty())
  49. s += ",";
  50. s += lpProp->Value.MVszA.lppszA[i];
  51. }
  52. return s;
  53. }
  54. }
  55. return std::string();
  56. }
  57. HRESULT MAPITablePrint(IMAPITable *lpTable, bool humanreadable /* = true */)
  58. {
  59. SPropTagArrayPtr ptrColumns;
  60. SRowSetPtr ptrRows;
  61. ConsoleTable ct(0, 0);
  62. unsigned int i = 0, j = 0;
  63. HRESULT hr = lpTable->QueryColumns(0, &~ptrColumns);
  64. if (hr != hrSuccess)
  65. return hr;
  66. hr = lpTable->QueryRows(-1, 0, &ptrRows);
  67. if (hr != hrSuccess)
  68. return hr;
  69. ct.Resize(ptrRows.size(), ptrColumns->cValues);
  70. for (i = 0; i < ptrColumns->cValues; ++i)
  71. ct.SetHeader(i, stringify(ptrColumns->aulPropTag[i], true));
  72. for (i = 0; i < ptrRows.size(); ++i)
  73. for (j = 0; j < ptrRows[i].cValues; ++j)
  74. ct.SetColumn(i, j, ToString(&ptrRows[i].lpProps[j]));
  75. humanreadable ? ct.PrintTable() : ct.DumpTable();
  76. return hrSuccess;
  77. }
  78. } /* namespace */