Drop unused variables
[TortoiseGit.git] / src / Git / GitHash.h
blob93f51811811dffb869639de562374081cb2db84e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - 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 #define GIT_HASH_SIZE 20
23 class CGitHash
25 public:
26 unsigned char m_hash[GIT_HASH_SIZE];
28 CGitHash()
30 memset(m_hash,0, GIT_HASH_SIZE);
32 CGitHash(const char *p)
34 memcpy(m_hash,p,GIT_HASH_SIZE);
36 CGitHash & operator = (const CString &str)
38 CGitHash hash(str);
39 *this = hash;
40 return *this;
42 CGitHash & operator = (const unsigned char *p)
44 memcpy(m_hash, p, GIT_HASH_SIZE);
45 return *this;
47 CGitHash(const CString &str)
49 if (!IsValidSHA1(str))
51 #ifdef ASSERT
52 ASSERT(FALSE);
53 #endif
54 memset(m_hash, 0, GIT_HASH_SIZE);
55 return;
58 for (int i = 0; i < GIT_HASH_SIZE; ++i)
60 unsigned char a;
61 a=0;
62 for (int j = 2 * i; j <= 2 * i + 1; ++j)
64 a =a<<4;
66 TCHAR ch = str[j];
67 if(ch >= _T('0') && ch <= _T('9'))
68 a |= (ch - _T('0'))&0xF;
69 else if(ch >=_T('A') && ch <= _T('F'))
70 a |= ((ch - _T('A'))&0xF) + 10 ;
71 else if(ch >=_T('a') && ch <= _T('f'))
72 a |= ((ch - _T('a'))&0xF) + 10;
75 m_hash[i]=a;
79 void ConvertFromStrA(const char *str)
81 for (int i = 0; i < GIT_HASH_SIZE; ++i)
83 unsigned char a;
84 a=0;
85 for (int j = 2 * i; j <= 2 * i + 1; ++j)
87 a =a<<4;
89 char ch = str[j];
90 if(ch >= '0' && ch <= '9')
91 a |= (ch - ('0'))&0xF;
92 else if(ch >=('A') && ch <= ('F'))
93 a |= ((ch - ('A'))&0xF) + 10 ;
94 else if(ch >=_T('a') && ch <= ('f'))
95 a |= ((ch - ('a'))&0xF) + 10;
98 m_hash[i]=a;
101 void Empty()
103 memset(m_hash,0, GIT_HASH_SIZE);
105 bool IsEmpty() const
107 for (int i = 0; i < GIT_HASH_SIZE; ++i)
109 if(m_hash[i] != 0)
110 return false;
112 return true;
115 CString ToString() const
117 CString str;
118 CString a;
119 for (int i = 0; i < GIT_HASH_SIZE; ++i)
121 a.Format(_T("%02x"),m_hash[i]);
122 str+=a;
124 return str;
126 operator CString () const
128 return ToString();
131 bool operator == (const CGitHash &hash) const
133 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;
136 static friend bool operator<(const CGitHash& left, const CGitHash& right)
138 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;
141 static friend bool operator>(const CGitHash& left, const CGitHash& right)
143 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;
146 static friend bool operator != (const CGitHash& left, const CGitHash& right)
148 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) != 0;
151 static bool IsValidSHA1(const CString &possibleSHA1)
153 if (possibleSHA1.GetLength() != 2 * GIT_HASH_SIZE)
154 return false;
155 for (int i = 0; i < possibleSHA1.GetLength(); ++i)
157 if (!((possibleSHA1[i] >= '0' && possibleSHA1[i] <= '9') || (possibleSHA1[i] >= 'a' && possibleSHA1[i] <= 'f') || (possibleSHA1[i] >= 'A' && possibleSHA1[i] <= 'F')))
158 return false;
160 return true;