OSDN Git Service

merge original branch.
[tortoisegit/TortoiseGitJp.git] / src / Git / GitHash.h
1 #pragma once\r
2 #if defined(_MFC_VER)\r
3 #include "afx.h"\r
4 #endif\r
5 #define GIT_HASH_SIZE 20\r
6 \r
7 class CGitHash\r
8 {\r
9 public:\r
10         unsigned char m_hash[GIT_HASH_SIZE];\r
11 \r
12         CGitHash()\r
13         {\r
14                 memset(m_hash,0, GIT_HASH_SIZE);\r
15         }\r
16         CGitHash(char *p)\r
17         {\r
18                 memcpy(m_hash,p,GIT_HASH_SIZE);\r
19         }\r
20         CGitHash & operator = (CString &str)\r
21         {\r
22                 CGitHash hash(str);\r
23                 *this = hash;\r
24                 return *this;\r
25         }\r
26         CGitHash(CString &str)\r
27         {\r
28                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
29                 {\r
30                         unsigned char a;\r
31                         a=0;\r
32                         for(int j=2*i;j<=2*i+1;j++)\r
33                         {\r
34                                 a =a<<4;\r
35 \r
36                                 TCHAR ch = str[j];\r
37                                 if(ch >= _T('0') && ch <= _T('9'))\r
38                                         a |= (ch - _T('0'))&0xF;\r
39                                 else if(ch >=_T('A') && ch <= _T('F'))\r
40                                         a |= ((ch - _T('A'))&0xF) + 10 ;\r
41                                 else if(ch >=_T('a') && ch <= _T('f'))\r
42                                         a |= ((ch - _T('a'))&0xF) + 10;         \r
43                                 \r
44                         }\r
45                         m_hash[i]=a;\r
46                 }\r
47         }\r
48         void Empty()\r
49         {\r
50                 memset(m_hash,0, GIT_HASH_SIZE);\r
51         }\r
52         bool IsEmpty()\r
53         {\r
54                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
55                 {\r
56                         if(m_hash[i] != 0)\r
57                                 return false;\r
58                 }\r
59                 return true;\r
60         }\r
61         \r
62         CString ToString()\r
63         {\r
64                 CString str;\r
65                 CString a;\r
66                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
67                 {\r
68                         a.Format(_T("%02x"),m_hash[i]);\r
69                         str+=a;\r
70                 }\r
71                 return str;\r
72         }\r
73         operator CString ()\r
74         { \r
75                 return ToString(); \r
76         } \r
77 \r
78         bool operator == (const CGitHash &hash)\r
79         {\r
80                 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;\r
81         }\r
82         \r
83         \r
84         friend bool operator<(const CGitHash& left, const CGitHash& right)\r
85         {\r
86                 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;\r
87         }\r
88 \r
89         friend bool operator>(const CGitHash& left, const CGitHash& right)\r
90         {\r
91                 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;\r
92         }\r
93 \r
94         friend bool operator != (const CGitHash& left, const CGitHash& right)\r
95         {\r
96                 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) != 0;\r
97         }\r
98 #if defined(_MFC_VER)\r
99         friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)\r
100         {\r
101                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
102                         ar<<hash.m_hash[i];\r
103                 return ar;\r
104         }\r
105         friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)\r
106         {\r
107                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
108                         ar>>hash.m_hash[i];\r
109                 return ar;\r
110         }\r
111 #endif\r
112 };\r
113 \r