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.

98 lines
3.9 KiB

  1. // Copyright (c) 1999 Microsoft Corporation. All rights reserved.
  2. //
  3. // Declaration of Strings, Slots, and Hash.
  4. //
  5. // Strings holds a collection of strings in one big chunk of memory.
  6. // Slots is just a collection that can be appended to and accessed
  7. // Lookup is used during parsing and then discarded at runtime. It maps strings to consecutive slots and positions in Strings.
  8. #pragma once
  9. class Strings
  10. {
  11. public:
  12. #ifdef _WIN64
  13. typedef __int64 index; // use an index type. this would allow us to do type checking to ensure that the appropriate indices are used with the appropriate collections if we really wanted to. (in order to test this we'd need to make a class in order to create a "real" type, which a mere typedef doesn't do. )
  14. #else
  15. typedef int index; // use an index type. this would allow us to do type checking to ensure that the appropriate indices are used with the appropriate collections if we really wanted to. (in order to test this we'd need to make a class in order to create a "real" type, which a mere typedef doesn't do. )
  16. #endif
  17. Strings();
  18. ~Strings();
  19. HRESULT Add(const char *psz, index &i);
  20. // The address of the stored string returned by operator[]
  21. // is guarenteed to not change over the lifetime of the Strings object.
  22. // (The Lookup class implementation requires this behavior.)
  23. const char *operator[](index i);
  24. private:
  25. char *m_pszBuf;
  26. index m_iCur;
  27. index m_iSize;
  28. index m_iBase;
  29. static const index ms_iInitialSize;
  30. };
  31. template<class T>
  32. class Slots
  33. {
  34. public:
  35. #ifdef _WIN64
  36. typedef __int64 index; // use an index type. this would allow us to do type checking to ensure that the appropriate indices are used with the appropriate collections if we really wanted to. (in order to test this we'd need to make a class in order to create a "real" type, which a mere typedef doesn't do. )
  37. #else
  38. typedef int index; // use an index type. this would allow us to do type checking to ensure that the appropriate indices are used with the appropriate collections if we really wanted to. (in order to test this we'd need to make a class in order to create a "real" type, which a mere typedef doesn't do. )
  39. #endif
  40. index Next() { return m_v.size(); }
  41. HRESULT Add(T t) { index i = m_v.size(); if (!m_v.AccessTo(i)) return E_OUTOFMEMORY; m_v[i] = t; return S_OK; }
  42. T& operator[](index i) { return m_v[i]; }
  43. private:
  44. SmartRef::Vector<T> m_v;
  45. };
  46. // hungarian: lku
  47. class Lookup
  48. {
  49. public:
  50. #ifdef _WIN64
  51. typedef __int64 slotindex; // don't want to have to template this on the kind of slot just for its index since it is just an int anyway
  52. #else
  53. typedef int slotindex; // don't want to have to template this on the kind of slot just for its index since it is just an int anyway
  54. #endif
  55. struct indices
  56. {
  57. Strings::index iString;
  58. slotindex iSlot;
  59. };
  60. Lookup(HRESULT *phr, Strings &strings, int iInitialSize) : m_h(phr, iInitialSize), m_strings(strings) {}
  61. indices &operator[](const char *psz) { StrKey k; k.psz = psz; return m_h[k]; }
  62. // both indices are out parameters, set only if the entry is found
  63. bool Find(const char *psz, slotindex &iSlot, Strings::index &iString) { return S_OK == FindOrAddInternal(false, psz, iSlot, iString); }
  64. // iSlot is in (next slot to add items) and out (slot if found in existing items). iString is out only. returns E_OUTOFMEMORY, S_OK (found), or S_FALSE (added).
  65. HRESULT FindOrAdd(const char *psz, slotindex &iSlot, Strings::index &iString) { return FindOrAddInternal(true, psz, iSlot, iString); }
  66. private:
  67. HRESULT FindOrAddInternal(bool fAdd, const char *psz, slotindex &iSlot, Strings::index &iString);
  68. bool m_fInitialized;
  69. struct StrKey
  70. {
  71. const char *psz;
  72. int Hash() { int i = 0; for (const char *p = psz; *p != '\0'; i += tolower(*p++)) {} return i; };
  73. bool operator ==(const StrKey &o) { return 0 == _stricmp(psz, o.psz); }
  74. };
  75. typedef SmartRef::Hash<StrKey, indices> stringhash;
  76. stringhash m_h;
  77. Strings &m_strings;
  78. };