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