PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/win32/shellext/dirstate.cpp

https://bitbucket.org/tortoisehg/hgtk/
C++ | 99 lines | 64 code | 20 blank | 15 comment | 7 complexity | 61ab83168a07d4ae7e21c12a13ef18c0 MD5 | raw file
Possible License(s): GPL-2.0
  1. // Copyright (C) 2009 Benjamin Pollack
  2. // Copyright (C) 2009 Adrian Buehlmann
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "stdafx.h"
  17. #include "dirstate.h"
  18. #include "TortoiseUtils.h"
  19. std::auto_ptr<Dirstate> Dirstate::read(const std::string& path, bool& unset)
  20. {
  21. unset = false;
  22. FILE *f = fopenReadRenameAllowed(path.c_str());
  23. if (!f)
  24. {
  25. TDEBUG_TRACE("Dirstate::read: can't open " << path);
  26. return std::auto_ptr<Dirstate>(0);
  27. }
  28. std::auto_ptr<Dirstate> pd(new Dirstate());
  29. fread(&pd->parent1, sizeof(char), HASH_LENGTH, f);
  30. fread(&pd->parent2, sizeof(char), HASH_LENGTH, f);
  31. Direntry e;
  32. std::vector<char> relpath(MAX_PATH + 10, 0);
  33. while (e.read(f, relpath))
  34. {
  35. if (e.unset())
  36. unset = true;
  37. if (e.state == 'a')
  38. ++pd->num_added_;
  39. pd->add(&relpath[0], e);
  40. }
  41. fclose(f);
  42. return pd;
  43. }
  44. static char *revhash_string(const char revhash[HASH_LENGTH])
  45. {
  46. unsigned ix;
  47. static char rev_string[HASH_LENGTH * 2 + 1];
  48. static char *hexval = "0123456789abcdef";
  49. for (ix = 0; ix < HASH_LENGTH; ++ix)
  50. {
  51. rev_string[ix * 2] = hexval[(revhash[ix] >> 4) & 0xf];
  52. rev_string[ix * 2 + 1] = hexval[revhash[ix] & 0xf];
  53. }
  54. rev_string[sizeof(rev_string)] = 0;
  55. return rev_string;
  56. }
  57. void testread()
  58. {
  59. bool unset;
  60. std::auto_ptr<Dirstate> pd = Dirstate::read(".hg/dirstate", unset);
  61. if (!pd.get()) {
  62. printf("error: could not read .hg/dirstate\n");
  63. return;
  64. }
  65. time_t t;
  66. char *s;
  67. unsigned ix;
  68. printf("parent1: %s\n", revhash_string(pd->parent1));
  69. printf("parent2: %s\n", revhash_string(pd->parent2));
  70. printf("entries: %d\n\n", pd->size());
  71. pd->root().print();
  72. }
  73. #ifdef APPMAIN
  74. int main(int argc, char *argv[])
  75. {
  76. testread();
  77. return 0;
  78. }
  79. #endif