1
0

FilenameField.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "FilenameField.h"
  2. #include "../nde.h"
  3. //---------------------------------------------------------------------------
  4. FilenameField::FilenameField(const char *Str, int strkind) : StringField(Str, strkind)
  5. {
  6. Type = FIELD_FILENAME;
  7. }
  8. //---------------------------------------------------------------------------
  9. FilenameField::FilenameField()
  10. {
  11. Type = FIELD_FILENAME;
  12. }
  13. //---------------------------------------------------------------------------
  14. int FilenameField::Compare(Field *Entry)
  15. {
  16. if (!Entry) return -1;
  17. if (!CompatibleFields(Entry->GetType(), GetType()))
  18. return 0;
  19. return mystricmp_fn(GetString(), ((FilenameField*)Entry)->GetString());
  20. }
  21. //---------------------------------------------------------------------------
  22. int FilenameField::Starts(Field *Entry)
  23. {
  24. if (!Entry) return -1;
  25. if (!CompatibleFields(Entry->GetType(), GetType()))
  26. return 0;
  27. return (mystristr_fn(GetString(), ((FilenameField*)Entry)->GetString()) == GetString());
  28. }
  29. //---------------------------------------------------------------------------
  30. int FilenameField::Contains(Field *Entry)
  31. {
  32. if (!Entry) return -1;
  33. if (!CompatibleFields(Entry->GetType(), GetType()))
  34. return 0;
  35. return (mystristr_fn(GetString(), ((FilenameField*)Entry)->GetString()) != NULL);
  36. }
  37. Field *FilenameField::Clone(Table *pTable)
  38. {
  39. FilenameField *clone = new FilenameField(String, STRING_IS_NDESTRING);
  40. clone->Pos = FIELD_CLONE;
  41. clone->ID = ID;
  42. clone->MaxSizeOnDisk = GetDataSize();
  43. return clone;
  44. }
  45. // TODO: make file system string comparison functions
  46. //---------------------------------------------------------------------------
  47. bool FilenameField::ApplyFilter(Field *Data, int op)
  48. {
  49. // TODO: maybe do this?
  50. if (op == FILTER_ISEMPTY || op == FILTER_ISNOTEMPTY)
  51. {
  52. bool r = (op == FILTER_ISEMPTY);
  53. if (!String)
  54. return r;
  55. if (String && String[0] == 0)
  56. return r;
  57. return !r;
  58. }
  59. //
  60. bool r;
  61. const char *p=0;
  62. if (Data->GetType() == FIELD_STRING)
  63. p = ((StringField *)Data)->GetString();
  64. else
  65. p = ((FilenameField *)Data)->GetString();
  66. const char *d = GetString();
  67. if (!p)
  68. p = "";
  69. if (!d)
  70. d = "";
  71. switch (op)
  72. {
  73. case FILTER_EQUALS:
  74. r = !nde_stricmp_fn(d, p);
  75. break;
  76. case FILTER_NOTEQUALS:
  77. r = !!nde_stricmp_fn(d, p);
  78. break;
  79. case FILTER_CONTAINS:
  80. r = (NULL != stristr_fn(d, p));
  81. break;
  82. case FILTER_NOTCONTAINS:
  83. r = (NULL == stristr_fn(d, p));
  84. break;
  85. case FILTER_ABOVE:
  86. r = (bool)(nde_stricmp_fn(d, p) > 0);
  87. break;
  88. case FILTER_ABOVEOREQUAL:
  89. r = (bool)(nde_stricmp_fn(d, p) >= 0);
  90. break;
  91. case FILTER_BELOW:
  92. r = (bool)(nde_stricmp_fn(d, p) < 0);
  93. break;
  94. case FILTER_BELOWOREQUAL:
  95. r = (bool)(nde_stricmp_fn(d, p) <= 0);
  96. break;
  97. case FILTER_BEGINS:
  98. r = (bool)(nde_strnicmp_fn(d, p, strlen(p)) == 0);
  99. break;
  100. case FILTER_ENDS:
  101. {
  102. int lenp = (int)strlen(p), lend = (int)strlen(d);
  103. if (lend < lenp) return 0; // too short
  104. r = (bool)(nde_stricmp_fn((d + lend) - lenp, p) == 0);
  105. }
  106. break;
  107. case FILTER_LIKE:
  108. r = (bool)(nde_stricmp_fn(d, p) == 0);
  109. break;
  110. default:
  111. r = true;
  112. break;
  113. }
  114. return r;
  115. }