1
0

nxpath.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "nxpath.h"
  2. #include <shlwapi.h>
  3. #include "foundation/error.h"
  4. static const wchar_t *FindExtension(nx_uri_t filename)
  5. {
  6. size_t position;
  7. if (!filename || !filename->string || !filename->len)
  8. return 0;
  9. position=filename->len;
  10. while (position--)
  11. {
  12. wchar_t c = filename->string[position];
  13. if (c == '.')
  14. return &filename->string[position+1];
  15. if (c == '/' || c == '\\')
  16. return 0;
  17. }
  18. return 0;
  19. }
  20. static const wchar_t *FindFilename(nx_uri_t filename)
  21. {
  22. size_t position;
  23. if (!filename || !filename->string || !filename->len)
  24. return 0;
  25. position=filename->len;
  26. while (position--)
  27. {
  28. wchar_t c = filename->string[position];
  29. if (c == '/' || c == '\\')
  30. return &filename->string[position+1];
  31. }
  32. return 0;
  33. }
  34. size_t NXPathMatchExtensionList(nx_uri_t filename, nx_string_t *extension_list, size_t num_extensions)
  35. {
  36. const wchar_t *ext = FindExtension(filename);
  37. if (ext && *ext)
  38. {
  39. size_t i;
  40. for (i=0;i<num_extensions;i++)
  41. {
  42. #if WINVER >= 0x0600
  43. if (CompareStringOrdinal(ext, -1, extension_list[i]->string, -1, TRUE) == CSTR_EQUAL)
  44. return i;
  45. #else
  46. if (CompareString(LOCALE_INVARIANT, NORM_IGNORECASE, ext, -1, extension_list[i]->string, -1) == CSTR_EQUAL)
  47. return i;
  48. #endif
  49. }
  50. }
  51. return num_extensions;
  52. }
  53. int NXPathMatchExtension(nx_uri_t filename, nx_string_t extension)
  54. {
  55. const wchar_t *ext = FindExtension(filename);
  56. if (ext && *ext)
  57. {
  58. #if WINVER >= 0x0600
  59. if (CompareStringOrdinal(ext, -1, extension->string, -1, TRUE) == CSTR_EQUAL)
  60. return NErr_Success;
  61. #else
  62. if (CompareString(LOCALE_INVARIANT, NORM_IGNORECASE, ext, -1, extension->string, -1) == CSTR_EQUAL)
  63. return NErr_Success;
  64. #endif
  65. }
  66. return NErr_False;
  67. }
  68. int NXPathProtocol(nx_uri_t filename, const char *protocol)
  69. {
  70. if (PathIsURL(filename->string))
  71. {
  72. wchar_t protocol_from_filename[100];
  73. DWORD protocol_length=100;
  74. if (UrlGetPart(filename->string, protocol_from_filename, &protocol_length, URL_PART_SCHEME, 0) == S_OK)
  75. {
  76. DWORD i;
  77. for (i=0;i<protocol_length;i++)
  78. {
  79. if ((wchar_t)(protocol[i]) != protocol_from_filename[i])
  80. return NErr_False;
  81. }
  82. return NErr_Success;
  83. }
  84. }
  85. return NErr_False;
  86. }
  87. int NXPathIsURL(nx_uri_t filename)
  88. {
  89. if (PathIsURL(filename->string))
  90. return NErr_True;
  91. else
  92. return NErr_False;
  93. }
  94. int NXPathIsRelative(nx_uri_t filename)
  95. {
  96. if (filename->len >= 3 && filename->string[1] == L':' && (filename->string[2] == L'\\' || filename->string[2] == L'/'))
  97. return NErr_False;
  98. if (filename->len >= 1 && (filename->string[2] == L'\\' || filename->string[2] == L'/'))
  99. return NErr_False;
  100. return NErr_True;
  101. }