Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.8 KiB

  1. // FilePath.cpp: implementation of the FilePath class.
  2. //
  3. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  4. // 2000. This computer program includes Confidential, Proprietary
  5. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  6. // use, disclosure, and/or reproduction is prohibited unless authorized
  7. // in writing. All Rights Reserved.
  8. //////////////////////////////////////////////////////////////////////
  9. // Don't allow the min & max macros in WINDEF.H to be defined so the
  10. // min/max methods declared in limits are accessible.
  11. #define NOMINMAX
  12. #include "NoWarning.h"
  13. #include <limits>
  14. #include "iopExc.h"
  15. #include "FilePath.h"
  16. using namespace std;
  17. using namespace iop;
  18. namespace
  19. {
  20. const char szSeparators[] = "/\\:";
  21. }
  22. //////////////////////////////////////////////////////////////////////
  23. // Construction/Destruction
  24. //////////////////////////////////////////////////////////////////////
  25. FilePath::FilePath()
  26. {
  27. }
  28. FilePath::FilePath(const string strFilePath)
  29. {
  30. // Make local copy of the string, since we need to manipulate it
  31. string strPath(strFilePath);
  32. while (strPath.length() > 0)
  33. {
  34. string::size_type i(0);
  35. string::size_type j(0);
  36. i = strPath.find_first_not_of(szSeparators, 0);
  37. if (i == std::string::npos)
  38. break;
  39. j = strPath.find_first_of(szSeparators, i+1);
  40. std::string::size_type cIndex =
  41. std::string::npos == j
  42. ? strPath.length()
  43. : j - i;
  44. FilePathComponent fpc(FilePathComponent(strPath.substr(i, cIndex)));
  45. m_FilePath.push_back(fpc);
  46. if (std::string::npos == j)
  47. break;
  48. strPath = strPath.substr(j+1, strPath.length() - (j+1));
  49. }
  50. }
  51. FilePath::FilePath(FilePath const &fp)
  52. {
  53. m_FilePath = list<FilePathComponent>(fp.m_FilePath);
  54. }
  55. FilePath::~FilePath()
  56. {
  57. }
  58. BYTE
  59. FilePath::NumComponents()
  60. {
  61. return static_cast<BYTE>(m_FilePath.size());
  62. }
  63. bool FilePath::IsEmpty()
  64. {
  65. return (m_FilePath.size() == 0);
  66. }
  67. FilePathComponent FilePath::ChopTail()
  68. {
  69. std::list<FilePathComponent>::iterator iter1 = m_FilePath.begin();
  70. std::list<FilePathComponent>::iterator iter2 = m_FilePath.end();
  71. if (iter1 == iter2)
  72. throw Exception(ccBadFilePath);
  73. iter2--;
  74. FilePathComponent fpc = *iter2;
  75. m_FilePath.erase(iter2);
  76. return fpc;
  77. }
  78. void FilePath::Clear()
  79. {
  80. m_FilePath.clear();
  81. }
  82. FilePathComponent FilePath::Head()
  83. {
  84. std::list<FilePathComponent>::iterator iter = m_FilePath.begin();
  85. if (m_FilePath.end() == iter)
  86. throw Exception(ccBadFilePath);
  87. return *iter;
  88. }
  89. FilePathComponent FilePath::Tail()
  90. {
  91. std::list<FilePathComponent>::iterator iter = m_FilePath.end();
  92. if (m_FilePath.begin() == iter)
  93. throw Exception(ccBadFilePath);
  94. return *(--iter);
  95. }
  96. string FilePath::GetStringPath()
  97. {
  98. string str;
  99. std::list<FilePathComponent>::iterator iter = m_FilePath.begin();
  100. while (iter != m_FilePath.end())
  101. {
  102. str += (*iter).GetStringID();
  103. iter++;
  104. }
  105. return str;
  106. }
  107. FilePath FilePath::Root()
  108. {
  109. return FilePath(string("/3f00"));
  110. }
  111. const FilePath &FilePath::operator +=(const FilePathComponent fp)
  112. {
  113. if (m_FilePath.size() >= std::numeric_limits<BYTE>::max())
  114. throw Exception(ccFilePathTooLong);
  115. m_FilePath.push_back(fp);
  116. return *this;
  117. }
  118. FilePathComponent &FilePath::operator [](unsigned int index)
  119. {
  120. if (index > (m_FilePath.size() - 1))
  121. throw Exception(ccInvalidParameter);
  122. std::list<FilePathComponent>::iterator iter = m_FilePath.begin();
  123. for (unsigned int i = 0; i < index; i++)
  124. iter++;
  125. return *iter;
  126. }
  127. FilePath FilePath::GreatestCommonPrefix(FilePath &rPath)
  128. {
  129. FilePath fp;
  130. std::list<FilePathComponent>::iterator iter1 = m_FilePath.begin();
  131. std::list<FilePathComponent>::iterator iter2 = rPath.m_FilePath.begin();
  132. while ((iter1 != m_FilePath.end()) &&
  133. (iter2 != rPath.m_FilePath.end()) &&
  134. (*iter1 == *iter2))
  135. {
  136. fp += *iter1;
  137. iter1++;
  138. iter2++;
  139. }
  140. return fp;
  141. }
  142. bool iop::operator==(FilePath const &lhs, FilePath const &rhs)
  143. {
  144. return lhs.m_FilePath == rhs.m_FilePath;
  145. }
  146. std::ostream &iop::operator<<(std::ostream &output, FilePath &fp)
  147. {
  148. string str = fp.GetStringPath();
  149. output << str;
  150. return output;
  151. }
  152. FilePathComponent::FilePathComponent(string strFileID)
  153. {
  154. // Check the length and throw if it is greater than 5
  155. if (strFileID.length() > 5)
  156. throw Exception(ccFileIdTooLarge);
  157. char *stopstring;
  158. const char* buf = strFileID.c_str();
  159. if ((buf[0] == '/') || (buf[0] == '\\') || (buf[0] == ':'))
  160. m_usFileID = (short)strtoul(&buf[1],&stopstring,16);
  161. else
  162. m_usFileID = (short)strtoul(buf,&stopstring,16);
  163. }
  164. FilePathComponent::FilePathComponent(unsigned short usFileID) : m_usFileID(usFileID)
  165. {
  166. }
  167. FilePathComponent::~FilePathComponent()
  168. {
  169. }
  170. // Overloaded == operator for FilePathComponent
  171. bool iop::operator==(FilePathComponent const &lhs,
  172. FilePathComponent const &rhs)
  173. {
  174. return lhs.m_usFileID == rhs.m_usFileID;
  175. }
  176. bool iop::operator!=(FilePathComponent const &lhs,
  177. FilePathComponent const &rhs)
  178. {
  179. return lhs.m_usFileID != rhs.m_usFileID;
  180. }
  181. bool iop::operator<(FilePathComponent const &lhs,
  182. FilePathComponent const &rhs)
  183. {
  184. return lhs.m_usFileID < rhs.m_usFileID;
  185. }
  186. bool iop::operator>(FilePathComponent const &lhs,
  187. FilePathComponent const &rhs)
  188. {
  189. return lhs.m_usFileID > rhs.m_usFileID;
  190. }
  191. std::ostream &iop::operator<<(std::ostream &output,
  192. FilePathComponent &fpc)
  193. {
  194. output << fpc.GetStringID();
  195. return output;
  196. }
  197. string FilePathComponent::GetStringID()
  198. {
  199. char buf[33]; //
  200. _itoa(m_usFileID,buf,16);
  201. string tmp(buf);
  202. for (unsigned int i = 4; i > strlen(buf); i--) tmp = string("0") + tmp;
  203. return string("/") + tmp;
  204. }