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.

135 lines
3.2 KiB

  1. /*
  2. SVMUTIL.CPP
  3. (c) copyright 1998 Microsoft Corp
  4. Shared utility functions
  5. Robert Rounthwaite (RobertRo@microsoft.com)
  6. */
  7. #include <afx.h>
  8. #include <assert.h>
  9. /////////////////////////////////////////////////////////////////////////////
  10. // FWordPresent
  11. //
  12. // Determines if the given "word" is present in the Text. A word in this
  13. // case is any string of characters with a non-alpha character on either
  14. // side (or with the beginning or end of the text on either side).
  15. // Case sensitive.
  16. /////////////////////////////////////////////////////////////////////////////
  17. bool FWordPresent(char *szText, char *szWord)
  18. {
  19. assert(szText != NULL);
  20. if (szWord != NULL)
  21. {
  22. char *szLoc;
  23. UINT cbSz = strlen(szWord);
  24. do
  25. {
  26. szLoc = strstr(szText, szWord);
  27. if (szLoc != NULL)
  28. {
  29. // this code checks to see that the spot we found is a "word" and not a subword
  30. // we want the character before and after to be !isalnum, unless the character on that end of the
  31. // string already is !isalnum (or we're at the beginning of the string, for the char before)
  32. if ((!isalnum(*szLoc) || ((szLoc == szText) || !isalnum(*(szLoc-1)))) && // prev char is not alnum and
  33. ((!isalnum(szLoc[cbSz-1])) || (!isalnum(szLoc[cbSz])))) // next char is not alnum
  34. {
  35. // we've found the word!
  36. return true;
  37. }
  38. szText = szLoc + cbSz;
  39. }
  40. }
  41. while (szLoc != NULL); // note dependency on exit condition in if above
  42. }
  43. return false;
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // Special feature implementations
  47. //
  48. /////////////////////////////////////////////////////////////////////////////
  49. inline BOOL BIsWhiteSpace(const char c)
  50. {
  51. return isspace(c);
  52. }
  53. // This feature is 20% of first 50 words contain no lowercase letters (includes words with no letters at all)
  54. // p20_BODY_INTRO_UPPERCASE_WORDS
  55. bool SpecialFeatureUpperCaseWords(char *pszText)
  56. {
  57. UINT cWords = 0;
  58. UINT cNonLowerWords = 0;
  59. bool bHasLowerLetter = false;
  60. char *pszPos = pszText;
  61. if (pszText == NULL)
  62. {
  63. return false;
  64. }
  65. while (BIsWhiteSpace(*pszPos))
  66. {
  67. pszPos++;
  68. }
  69. while ((*pszPos != '\0') && (cWords < 50))
  70. {
  71. if (BIsWhiteSpace(*pszPos)) // word end
  72. {
  73. cWords++;
  74. if (!bHasLowerLetter)
  75. {
  76. cNonLowerWords++;
  77. }
  78. else
  79. {
  80. bHasLowerLetter = false;
  81. }
  82. }
  83. else
  84. {
  85. bHasLowerLetter |= (islower(*pszPos) != FALSE);
  86. }
  87. pszPos++;
  88. }
  89. return (cWords>0) && ((cNonLowerWords/(double)cWords) >= 0.25);
  90. }
  91. // This feature is: 6% of first 200 non-space and non-numeric characters aren't letters
  92. // p20_BODY_INTRO_NONALPHA
  93. bool SpecialFeatureNonAlpha(char *pszText)
  94. {
  95. UINT cChars = 0;
  96. UINT cNonAlphaChars = 0;
  97. char *pszPos = pszText;
  98. if (pszText == NULL)
  99. {
  100. return false;
  101. }
  102. while (BIsWhiteSpace(*pszPos))
  103. {
  104. pszPos++;
  105. }
  106. while ((*pszPos != '\0') && (cChars < 200))
  107. {
  108. if ((!BIsWhiteSpace(*pszPos)) && (!isdigit(*pszPos))) // character
  109. {
  110. cChars++;
  111. if (!isalpha(*pszPos))
  112. {
  113. cNonAlphaChars++;
  114. }
  115. }
  116. pszPos++;
  117. }
  118. return (cChars>0) && ((cNonAlphaChars/(double)cChars) >= 0.08);
  119. }