OLEDBTextProviderRS.h
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // OLEDBTextProviderRS.h : Declaration of the COLEDBTextProviderRowset
  2. #ifndef __COLEDBTextProviderRowset_H_
  3. #define __COLEDBTextProviderRowset_H_
  4. #include "resource.h"       // main symbols
  5. // Added by Chuck Wood for text file support
  6. #include <stdio.h>
  7. //Remove inheritance from class
  8. class CTextFile
  9. {
  10. public:
  11. TCHAR strClass[51];
  12. TCHAR strSectionID[12];
  13. TCHAR strYear[7];
  14. TCHAR strTerm[31];
  15. TCHAR strInstructor[51];
  16. TCHAR strAssignment[256];
  17. TCHAR strScore[23];
  18. TCHAR strStudent[36];
  19. BEGIN_PROVIDER_COLUMN_MAP(CTextFile)
  20. PROVIDER_COLUMN_ENTRY_STR("Class", 1, strClass)
  21. PROVIDER_COLUMN_ENTRY_STR("SectionID", 2, strSectionID)
  22. PROVIDER_COLUMN_ENTRY_STR("Year", 3, strYear)
  23. PROVIDER_COLUMN_ENTRY_STR("Term", 4, strTerm)
  24. PROVIDER_COLUMN_ENTRY_STR("Instructor", 5, strInstructor)
  25. PROVIDER_COLUMN_ENTRY_STR("Assignment", 6, strAssignment)
  26. PROVIDER_COLUMN_ENTRY_STR("Score", 7, strScore)
  27. PROVIDER_COLUMN_ENTRY_STR("Student", 8, strStudent)
  28. END_PROVIDER_COLUMN_MAP()
  29. };
  30. // COLEDBTextProviderCommand
  31. class ATL_NO_VTABLE COLEDBTextProviderCommand : 
  32. public CComObjectRootEx<CComSingleThreadModel>,
  33. public IAccessorImpl<COLEDBTextProviderCommand>,
  34. public ICommandTextImpl<COLEDBTextProviderCommand>,
  35. public ICommandPropertiesImpl<COLEDBTextProviderCommand>,
  36. public IObjectWithSiteImpl<COLEDBTextProviderCommand>,
  37. public IConvertTypeImpl<COLEDBTextProviderCommand>,
  38. public IColumnsInfoImpl<COLEDBTextProviderCommand>
  39. {
  40. public:
  41. BEGIN_COM_MAP(COLEDBTextProviderCommand)
  42. COM_INTERFACE_ENTRY(ICommand)
  43. COM_INTERFACE_ENTRY(IObjectWithSite)
  44. COM_INTERFACE_ENTRY(IAccessor)
  45. COM_INTERFACE_ENTRY(ICommandProperties)
  46. COM_INTERFACE_ENTRY2(ICommandText, ICommand)
  47. COM_INTERFACE_ENTRY(IColumnsInfo)
  48. COM_INTERFACE_ENTRY(IConvertType)
  49. END_COM_MAP()
  50. // ICommand
  51. public:
  52. HRESULT FinalConstruct()
  53. {
  54. HRESULT hr = CConvertHelper::FinalConstruct();
  55. if (FAILED (hr))
  56. return hr;
  57. hr = IAccessorImpl<COLEDBTextProviderCommand>::FinalConstruct();
  58. if (FAILED(hr))
  59. return hr;
  60. return CUtlProps<COLEDBTextProviderCommand>::FInit();
  61. }
  62. void FinalRelease()
  63. {
  64. IAccessorImpl<COLEDBTextProviderCommand>::FinalRelease();
  65. }
  66. HRESULT WINAPI Execute(IUnknown * pUnkOuter, REFIID riid, DBPARAMS * pParams, 
  67.   LONG * pcRowsAffected, IUnknown ** ppRowset);
  68. static ATLCOLUMNINFO* GetColumnInfo(COLEDBTextProviderCommand* pv, ULONG* pcInfo)
  69. {
  70. return CTextFile::GetColumnInfo(pv,pcInfo);
  71. }
  72. BEGIN_PROPSET_MAP(COLEDBTextProviderCommand)
  73. BEGIN_PROPERTY_SET(DBPROPSET_ROWSET)
  74. PROPERTY_INFO_ENTRY(IAccessor)
  75. PROPERTY_INFO_ENTRY(IColumnsInfo)
  76. PROPERTY_INFO_ENTRY(IConvertType)
  77. PROPERTY_INFO_ENTRY(IRowset)
  78. PROPERTY_INFO_ENTRY(IRowsetIdentity)
  79. PROPERTY_INFO_ENTRY(IRowsetInfo)
  80. PROPERTY_INFO_ENTRY(IRowsetLocate)
  81. PROPERTY_INFO_ENTRY(BOOKMARKS)
  82. PROPERTY_INFO_ENTRY(BOOKMARKSKIPPED)
  83. PROPERTY_INFO_ENTRY(BOOKMARKTYPE)
  84. PROPERTY_INFO_ENTRY(CANFETCHBACKWARDS)
  85. PROPERTY_INFO_ENTRY(CANHOLDROWS)
  86. PROPERTY_INFO_ENTRY(CANSCROLLBACKWARDS)
  87. PROPERTY_INFO_ENTRY(LITERALBOOKMARKS)
  88. PROPERTY_INFO_ENTRY(ORDEREDBOOKMARKS)
  89. END_PROPERTY_SET(DBPROPSET_ROWSET)
  90. END_PROPSET_MAP()
  91. };
  92. class COLEDBTextProviderRowset : 
  93. public CRowsetImpl< COLEDBTextProviderRowset, 
  94. CTextFile, COLEDBTextProviderCommand>
  95. {
  96. private:
  97. void ColumnCopy(char* row, char* column, int length) {
  98.         column[length] = 0;     //Null Terminate 
  99.         StrTrim(column);        //Trim for users
  100. }
  101. public:
  102. static char* GetFileName() {
  103. //Change this file name 
  104. //if you want to use a different one
  105. return "C:\My Documents\Visual C++\StudentTextFile.txt";
  106. }
  107. static void StrTrim(char *instring) {
  108. static char strBlank[] = " n";    // chars to strip
  109. // remove trailing spaces or newlines
  110. for (char *pchEnd = instring + strlen(instring);
  111. pchEnd != instring && strchr(
  112. strBlank, *(pchEnd - 1)) != NULL;
  113. *--pchEnd = '');
  114. // remove leading spaces or newlines
  115. char *pchStart = &instring[strspn(instring, strBlank)];
  116. memmove(instring, pchStart, 
  117. sizeof(char) * (pchEnd - pchStart + 1));
  118. }
  119. HRESULT Execute(DBPARAMS * pParams, LONG* pcRowsAffected)
  120. {
  121. CTextFile tf;
  122. FILE *fp;       //Text file pointer
  123. char row[500];  //row buffer;
  124. *pcRowsAffected = 0;
  125. fp = fopen(
  126. GetFileName(), 
  127. "r+");     //Open for reading and writing
  128. if (fp == NULL) {
  129. return DB_E_INVALID;
  130. }
  131. while (fgets(row, 500, fp)) { //Go until EOF
  132. ColumnCopy(row, tf.strClass, 50);
  133. ColumnCopy(row+50, tf.strSectionID, 11);
  134. ColumnCopy(row+61, tf.strYear, 6);
  135. ColumnCopy(row+67, tf.strTerm, 30);
  136. ColumnCopy(row+97, tf.strInstructor, 50);
  137. ColumnCopy(row+147, tf.strAssignment, 255);
  138. ColumnCopy(row+402, tf.strScore, 22);
  139. ColumnCopy(row+424, tf.strStudent, 35);
  140. if (!m_rgRowData.Add(tf))
  141. return E_OUTOFMEMORY;
  142. *pcRowsAffected++;
  143. }
  144. fclose(fp);
  145. return S_OK;
  146. }
  147. };
  148. #endif //__COLEDBTextProviderRowset_H_