Fixed issue #1220: ext/CrashServer/CommonLibs/Zlib/Zlib.vcproj immediate dir Win32...
[TortoiseGit.git] / src / Git / GitHash.h
blobe2e7f5bd665d53b4d555e6e5aae10e93ba94cf8a
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
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.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #if defined(_MFC_VER)
22 #include "afx.h"
23 #endif
24 #define GIT_HASH_SIZE 20
26 class CGitHash
28 public:
29 unsigned char m_hash[GIT_HASH_SIZE];
31 CGitHash()
33 memset(m_hash,0, GIT_HASH_SIZE);
35 CGitHash(char *p)
37 memcpy(m_hash,p,GIT_HASH_SIZE);
39 CGitHash & operator = (CString &str)
41 CGitHash hash(str);
42 *this = hash;
43 return *this;
45 CGitHash(CString &str)
47 if(str.GetLength()<GIT_HASH_SIZE)
49 #ifdef ASSERT
50 ASSERT(FALSE);
51 #endif
52 return;
55 for(int i=0;i<GIT_HASH_SIZE;i++)
57 unsigned char a;
58 a=0;
59 for(int j=2*i;j<=2*i+1;j++)
61 a =a<<4;
63 TCHAR ch = str[j];
64 if(ch >= _T('0') && ch <= _T('9'))
65 a |= (ch - _T('0'))&0xF;
66 else if(ch >=_T('A') && ch <= _T('F'))
67 a |= ((ch - _T('A'))&0xF) + 10 ;
68 else if(ch >=_T('a') && ch <= _T('f'))
69 a |= ((ch - _T('a'))&0xF) + 10;
72 m_hash[i]=a;
76 void ConvertFromStrA(char *str)
78 for(int i=0;i<GIT_HASH_SIZE;i++)
80 unsigned char a;
81 a=0;
82 for(int j=2*i;j<=2*i+1;j++)
84 a =a<<4;
86 char ch = str[j];
87 if(ch >= '0' && ch <= '9')
88 a |= (ch - ('0'))&0xF;
89 else if(ch >=('A') && ch <= ('F'))
90 a |= ((ch - ('A'))&0xF) + 10 ;
91 else if(ch >=_T('a') && ch <= ('f'))
92 a |= ((ch - ('a'))&0xF) + 10;
95 m_hash[i]=a;
98 void Empty()
100 memset(m_hash,0, GIT_HASH_SIZE);
102 bool IsEmpty()
104 for(int i=0;i<GIT_HASH_SIZE;i++)
106 if(m_hash[i] != 0)
107 return false;
109 return true;
112 CString ToString()
114 CString str;
115 CString a;
116 for(int i=0;i<GIT_HASH_SIZE;i++)
118 a.Format(_T("%02x"),m_hash[i]);
119 str+=a;
121 return str;
123 operator CString ()
125 return ToString();
128 bool operator == (const CGitHash &hash)
130 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;
134 friend bool operator<(const CGitHash& left, const CGitHash& right)
136 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;
139 friend bool operator>(const CGitHash& left, const CGitHash& right)
141 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;
144 friend bool operator != (const CGitHash& left, const CGitHash& right)
146 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) != 0;
148 #if defined(_MFC_VER)
149 friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)
151 for(int i=0;i<GIT_HASH_SIZE;i++)
152 ar<<hash.m_hash[i];
153 return ar;
155 friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)
157 for(int i=0;i<GIT_HASH_SIZE;i++)
158 ar>>hash.m_hash[i];
159 return ar;
161 #endif