OSDN Git Service

Build is okay
[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                                 TCHAR ch = str[j];\r
35                                 if(ch >= _T('0') && ch <= _T('9'))\r
36                                         a |= (ch - _T('0'))&0xF;\r
37                                 else if(ch >=_T('A') && ch <= _T('F'))\r
38                                         a |= (ch - _T('A'))&0xF;\r
39                                 else if(ch >=_T('a') && ch <= _T('f'))\r
40                                         a |= (ch - _T('a'))&0xF;\r
41                                 \r
42                                 a =a<<4;\r
43                         }\r
44                         m_hash[i]=a;\r
45                 }\r
46         }\r
47         void Empty()\r
48         {\r
49                 memset(m_hash,0, GIT_HASH_SIZE);\r
50         }\r
51         bool IsEmpty()\r
52         {\r
53                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
54                 {\r
55                         if(m_hash[i] != 0)\r
56                                 return false;\r
57                 }\r
58                 return true;\r
59         }\r
60         \r
61         CString ToString()\r
62         {\r
63                 CString str;\r
64                 CString a;\r
65                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
66                 {\r
67                         a.Format(_T("%02X"),m_hash[i]);\r
68                         str+=a;\r
69                 }\r
70                 return str;\r
71         }\r
72         operator CString ()\r
73         { \r
74                 return ToString(); \r
75         } \r
76 \r
77         bool operator == (const CGitHash &hash)\r
78         {\r
79                 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;\r
80         }\r
81         \r
82         \r
83         friend bool operator<(const CGitHash& left, const CGitHash& right)\r
84         {\r
85                 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;\r
86         }\r
87 \r
88         friend bool operator>(const CGitHash& left, const CGitHash& right)\r
89         {\r
90                 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;\r
91         }\r
92 \r
93 #if defined(_MFC_VER)\r
94         friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)\r
95         {\r
96                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
97                         ar<<hash.m_hash[i];\r
98                 return ar;\r
99         }\r
100         friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)\r
101         {\r
102                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
103                         ar>>hash.m_hash[i];\r
104                 return ar;\r
105         }\r
106 #endif\r
107 };\r
108 \r