OSDN Git Service

Change Log Code structure Unfinished
[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         unsigned char m_hash[GIT_HASH_SIZE];\r
10 public:\r
11         CGitHash()\r
12         {\r
13                 memset(m_hash,0, GIT_HASH_SIZE);\r
14         }\r
15         CGitHash(char *p)\r
16         {\r
17                 memcpy(m_hash,p,GIT_HASH_SIZE);\r
18         }       \r
19         CGitHash(CString &str)\r
20         {\r
21                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
22                 {\r
23                         unsigned char a;\r
24                         a=0;\r
25                         for(int j=2*i;j<2*i+1;j++)\r
26                         {\r
27                                 TCHAR ch = str[j];\r
28                                 if(ch >= _T('0') && ch <= _T('9'))\r
29                                         a |= (ch - _T('0'))&0xF;\r
30                                 else if(ch >=_T('A') && ch <= _T('F'))\r
31                                         a |= (ch - _T('A'))&0xF;\r
32                                 else if(ch >=_T('a') && ch <= _T('f'))\r
33                                         a |= (ch - _T('a'))&0xF;\r
34                                 \r
35                                 a =a<<4;\r
36                         }\r
37                         m_hash[i]=a;\r
38                 }\r
39         }\r
40         void Empty()\r
41         {\r
42                 memset(m_hash,0, GIT_HASH_SIZE);\r
43         }\r
44         bool IsEmpty()\r
45         {\r
46                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
47                 {\r
48                         if(m_hash[i] != 0)\r
49                                 return false;\r
50                 }\r
51                 return true;\r
52         }\r
53         \r
54         CString ToString()\r
55         {\r
56                 CString str;\r
57                 CString a;\r
58                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
59                 {\r
60                         a.Format(_T("%02X"),m_hash[i]);\r
61                         str+=a;\r
62                 }\r
63                 return str;\r
64         }\r
65         operator CString ()\r
66         { \r
67                 return ToString(); \r
68         } \r
69 \r
70         bool operator == (CGitHash &hash)\r
71         {\r
72                 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;\r
73         }\r
74         \r
75         \r
76         friend bool operator<(const CGitHash& left, const CGitHash& right)\r
77         {\r
78                 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;\r
79         }\r
80 \r
81         friend bool operator>(const CGitHash& left, const CGitHash& right)\r
82         {\r
83                 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;\r
84         }\r
85 \r
86 #if defined(_MFC_VER)\r
87         friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)\r
88         {\r
89                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
90                         ar<<hash.m_hash[i];\r
91                 return ar;\r
92         }\r
93         friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)\r
94         {\r
95                 for(int i=0;i<GIT_HASH_SIZE;i++)\r
96                         ar>>hash.m_hash[i];\r
97                 return ar;\r
98         }\r
99 #endif\r
100 };\r
101 \r