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.

152 lines
4.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1996-1997, Microsoft Corporation.
  4. //
  5. // File: strsort.cxx
  6. //
  7. // Contents: Builds a sort node from a string
  8. //
  9. // History: 96/Jan/3 DwightKr Created
  10. //
  11. //----------------------------------------------------------------------------
  12. #include <pch.cxx>
  13. #pragma hdrstop
  14. //+---------------------------------------------------------------------------
  15. //
  16. // Function: GetStringDbSortNode - public
  17. //
  18. // Synopsis: Builds a CDbSortNode from the string passed
  19. //
  20. // Arguments: [wcsSort] - the string containing the sort specification
  21. // [pList] - the property list describing the sort DBTYPES
  22. //
  23. // Notes: CPListException is used instead of CParserException so the
  24. // error class will be IDQError, not RESError in idq.dll.
  25. //
  26. // History: 96/Jan/03 DwightKr Created.
  27. //
  28. //----------------------------------------------------------------------------
  29. CDbSortNode * GetStringDbSortNode( const WCHAR * wcsSort,
  30. IColumnMapper * pList,
  31. LCID locale)
  32. {
  33. Win4Assert( 0 != wcsSort );
  34. unsigned cSortCol = 0;
  35. CDbSortNode * pDbSortNode = new CDbSortNode();
  36. if ( 0 == pDbSortNode )
  37. {
  38. THROW( CException(STATUS_INSUFFICIENT_RESOURCES) );
  39. }
  40. XPtr<CDbSortNode> xDbSortNode(pDbSortNode);
  41. CQueryScanner scanner( wcsSort, FALSE, locale );
  42. for ( XPtrST<WCHAR> wcsColumnName( scanner.AcqColumn() );
  43. wcsColumnName.GetPointer() != 0;
  44. wcsColumnName.Set( scanner.AcqColumn() )
  45. )
  46. {
  47. scanner.AcceptColumn(); // Remove the column name
  48. //
  49. // Parse for the [a] or [d] parameters.
  50. //
  51. unsigned order = QUERY_SORTASCEND; // Assume ascending sort order
  52. if ( scanner.LookAhead() == W_OPEN_TOKEN )
  53. {
  54. scanner.Accept(); // Remove the '['
  55. WCHAR wchOrder = scanner.GetCommandChar();
  56. if ( wchOrder == L'a' )
  57. {
  58. order = QUERY_SORTASCEND;
  59. }
  60. else if ( wchOrder == L'd' )
  61. {
  62. order = QUERY_SORTDESCEND;
  63. }
  64. else
  65. {
  66. // Report an error
  67. THROW( CPListException(QPARSE_E_INVALID_SORT_ORDER, 0) );
  68. }
  69. WCHAR wchNext = scanner.GetCommandChar();
  70. if (wchNext != 0 && !iswspace(wchNext))
  71. // some alphabetic character followed the '[a' or '[d'.
  72. THROW( CPListException(QPARSE_E_INVALID_SORT_ORDER, 0) );
  73. scanner.AcceptCommand(); // Remove the command character
  74. if ( scanner.LookAhead() != W_CLOSE_TOKEN )
  75. {
  76. // Report an error
  77. THROW( CPListException(QPARSE_E_EXPECTING_BRACE, 0) );
  78. }
  79. scanner.Accept(); // Remove the ']' character
  80. }
  81. //
  82. // Build a CDbSortKey with the parameters obtained.
  83. //
  84. CDbColId *pDbColId = 0;
  85. DBID *pdbid = 0;
  86. if ( FAILED(pList->GetPropInfoFromName( wcsColumnName.GetPointer(), &pdbid, 0, 0 )) )
  87. {
  88. //
  89. // Column name not found.
  90. //
  91. THROW( CPListException(QPARSE_E_NO_SUCH_SORT_PROPERTY, 0) );
  92. }
  93. pDbColId = (CDbColId *)pdbid;
  94. CDbSortKey sortKey( *pDbColId, order, locale);
  95. //
  96. // Add the new sort key to the tree of sort nodes.
  97. //
  98. if ( !pDbSortNode->AddSortColumn(sortKey) )
  99. {
  100. // Report a failure.
  101. THROW( CException(STATUS_INSUFFICIENT_RESOURCES) );
  102. }
  103. delete wcsColumnName.Acquire();
  104. //
  105. // Skip over commas seperating sort columns
  106. //
  107. if ( scanner.LookAhead() == COMMA_TOKEN )
  108. {
  109. scanner.Accept(); // Remove the ','
  110. }
  111. else if (scanner.LookAhead() != EOS_TOKEN)
  112. {
  113. THROW( CPListException(QPARSE_E_EXPECTING_COMMA, 0) );
  114. }
  115. cSortCol++;
  116. }
  117. if (scanner.LookAhead() != EOS_TOKEN)
  118. {
  119. if (cSortCol > 0)
  120. {
  121. THROW( CPListException(QPARSE_E_NO_SUCH_SORT_PROPERTY, 0) );
  122. }
  123. else
  124. {
  125. THROW( CPListException(QPARSE_E_EXPECTING_EOS, 0) );
  126. }
  127. }
  128. return xDbSortNode.Acquire();
  129. }