Source code of Windows XP (NT5)
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.

159 lines
4.2 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /**************************************************************************/
  4. /***** Common Library Component - Parse Table Handling Routines 1 *********/
  5. /**************************************************************************/
  6. // Return the number of items in the given parse table.
  7. static size_t countScp ( PSCP pscpTable )
  8. {
  9. size_t i ;
  10. for (i = 0; pscpTable[i].sz ; i++) ;
  11. return i;
  12. }
  13. // Compare two parse table structures
  14. static int __cdecl compareScps ( const void * scp1, const void * scp2 )
  15. {
  16. return _stricmp( ((PSCP) scp1)->sz, ((PSCP) scp2)->sz );
  17. }
  18. // Allocate and initialize a sorted version of the given
  19. // parse table
  20. static PSCP reallocateSorted ( PSCP pscpTable )
  21. {
  22. size_t cItems = countScp( pscpTable ) ;
  23. PSCP pscpSorted = (PSCP) calloc( cItems, sizeof (SCP) ) ;
  24. if (pscpSorted == NULL)
  25. return NULL;
  26. // Copy and sort the new table.
  27. memcpy( (void *) pscpSorted,
  28. (void *) pscpTable,
  29. (size_t) cItems * sizeof (SCP) );
  30. qsort( pscpSorted,
  31. cItems,
  32. sizeof (SCP),
  33. compareScps );
  34. return pscpSorted ;
  35. }
  36. static PSPT allocateParseTable ( PSCP pscpTable )
  37. {
  38. PSPT pspt = malloc( sizeof (SPT) ) ;
  39. if (pspt == NULL) {
  40. return NULL;
  41. }
  42. pspt->pscpSorted = reallocateSorted( pscpTable ) ;
  43. if (pspt->pscpSorted == NULL) {
  44. free( pspt );
  45. return NULL ;
  46. }
  47. pspt->pscpBase = pscpTable ;
  48. pspt->cItems = countScp( pscpTable );
  49. pspt->spcDelim = pscpTable[ pspt->cItems ].spc ;
  50. return pspt ;
  51. }
  52. static void destroyParseTable ( PSPT parseTable )
  53. {
  54. free( parseTable->pscpSorted );
  55. free( parseTable );
  56. }
  57. /*
  58. ** Purpose:
  59. ** Initializes a Parsing Table so a string can be searched for and its
  60. ** corresponding code found and returned.
  61. ** Arguments:
  62. ** pscp: a non-NULL array of SCPs with the last one having an sz value
  63. ** of NULL and an SPC for indicating errors.
  64. ** Returns:
  65. ** NULL if an error occurs.
  66. ** Non-NULL if the operation was successful.
  67. **
  68. **************************************************************************/
  69. PSPT APIENTRY PsptInitParsingTable(pscp)
  70. PSCP pscp;
  71. {
  72. AssertDataSeg();
  73. ChkArg(pscp != (PSCP)NULL, 1, (PSPT)NULL);
  74. return allocateParseTable( pscp ) ;
  75. }
  76. /*
  77. ** Purpose:
  78. ** Searches for a string in a String Parsing Table and returns its
  79. ** associated code.
  80. ** Arguments:
  81. ** pspt: non-NULL String Parsing Table initialized by a successful
  82. ** call to PsptInitParsingTable()..
  83. ** sz: non-NULL string to search for.
  84. ** Returns:
  85. ** The corresponding SPC if sz is in pspt, or the SPC associated with
  86. ** the NULL in pspt if sz could not be found.
  87. **
  88. **************************************************************************/
  89. SPC APIENTRY SpcParseString(pspt, sz)
  90. PSPT pspt;
  91. SZ sz;
  92. {
  93. PSCP pscpResult ;
  94. SCP scpTemp ;
  95. AssertDataSeg();
  96. ChkArg(pspt != (PSPT)NULL, 1, spcError);
  97. ChkArg(sz != (SZ)NULL, 2, spcError);
  98. if (pspt == NULL || sz == NULL)
  99. return 0 ;
  100. scpTemp.sz = sz ;
  101. pscpResult = (PSCP) bsearch( (void *) & scpTemp,
  102. (void *) pspt->pscpSorted,
  103. pspt->cItems,
  104. sizeof (SCP),
  105. compareScps );
  106. return pscpResult
  107. ? pscpResult->spc
  108. : pspt->spcDelim ;
  109. }
  110. /*
  111. ** Purpose:
  112. ** Destroys a Parse Table (freeing any memory allocated).
  113. ** Arguments:
  114. ** pspt: non-NULL String Parsing Table to be destroyed that was
  115. ** initialized with a successful call to PsptInitParsingTable().
  116. ** Returns:
  117. ** fFalse if an error occurred.
  118. ** fTrue if successful.
  119. **
  120. **************************************************************************/
  121. BOOL APIENTRY FDestroyParsingTable(pspt)
  122. PSPT pspt;
  123. {
  124. AssertDataSeg();
  125. ChkArg(pspt != (PSPT)NULL, 1, fFalse);
  126. destroyParseTable( pspt );
  127. return(fTrue);
  128. }