ali@459: /* ali@475: * Copyright (C) 2014, 2016 J. Ali Harlow ali@459: * ali@459: * This program is free software; you can redistribute it and/or modify ali@459: * it under the terms of the GNU General Public License as published by ali@459: * the Free Software Foundation; either version 2 of the License, or ali@459: * (at your option) any later version. ali@459: * ali@459: * This program is distributed in the hope that it will be useful, ali@459: * but WITHOUT ANY WARRANTY; without even the implied warranty of ali@459: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ali@459: * GNU General Public License for more details. ali@459: * ali@459: * You should have received a copy of the GNU General Public License along ali@459: * with this program; if not, write to the Free Software Foundation, Inc., ali@459: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ali@459: */ ali@459: ali@459: #include "config.h" ali@459: ali@459: #include ali@459: #include ali@459: #include ali@459: #ifdef MSWIN_API ali@459: #include ali@459: #endif ali@459: #include "razor.h" ali@459: ali@475: #ifdef MSWIN_API ali@475: static int is_ascii_letter(char c) ali@459: { ali@459: return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); ali@459: } ali@459: ali@475: static int is_slash(char c) ali@459: { ali@459: return c == '/' || c == '\\'; ali@459: } ali@459: ali@475: static const char *mswin_path(const char *path) ali@459: { ali@459: if (path == NULL) ali@459: return NULL; ali@459: ali@459: if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' && ali@459: path[3] == '/') ali@459: path++; ali@459: ali@459: return path; ali@459: } ali@475: #endif ali@459: ali@475: static int path_cmp(const char *p1, const char *p2) ali@459: { ali@459: #ifdef MSWIN_API ali@459: while(*p1 && *p2) { ali@459: if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) { ali@459: p1++; ali@459: p2++; ali@459: } else ali@459: break; ali@459: } ali@459: ali@459: return *p1 || *p2; ali@459: #else ali@459: return strcmp(p1, p2); ali@459: #endif ali@459: } ali@459: ali@475: static int test_pfu(const char *uri, const char *path) ali@459: { ali@459: char *s; ali@459: int r; ali@475: struct razor_error *error = NULL; ali@459: ali@475: s = razor_path_from_uri(uri, &error); ali@459: ali@459: #ifdef MSWIN_API ali@459: path = mswin_path(path); ali@459: #endif ali@459: ali@459: if (s && path) ali@459: r = path_cmp(s, path); ali@459: else ali@459: r = (s != path); ali@459: ali@459: if (r) { ali@475: fprintf(stderr, "Fail: razor_path_from_uri(\"%s\")", uri); ali@459: if (s) ali@459: fprintf(stderr, " returns \"%s\", expected", s); ali@459: else ali@475: fprintf(stderr, " fails (%s), expected", ali@475: razor_error_get_msg(error)); ali@459: if (path) ali@459: fprintf(stderr, " \"%s\"\n", path); ali@459: else ali@475: fprintf(stderr, " failure\n"); ali@459: } ali@459: ali@459: free(s); ali@475: if (error) ali@475: razor_error_free(error); ali@459: ali@459: return r; ali@459: } ali@459: ali@459: #ifdef MSWIN_API ali@459: UINT saved_cp; ali@459: ali@475: static void cleanup_on_exit(void) ali@459: { ali@459: SetConsoleOutputCP(saved_cp); ali@459: } ali@459: #endif ali@459: ali@459: int main(int argc, char *argv[]) ali@459: { ali@459: int r = 0; ali@459: ali@459: #ifdef MSWIN_API ali@459: atexit(cleanup_on_exit); ali@459: saved_cp = GetConsoleOutputCP(); ali@459: SetConsoleOutputCP(CP_UTF8); ali@459: #endif ali@459: ali@459: r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab"); ali@459: r |= test_pfu("file:///etc/fstab", "/etc/fstab"); ali@459: r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi", ali@459: "/c:/WINDOWS/clock.avi"); ali@459: r |= test_pfu("file:///c:/WINDOWS/clock.avi", ali@459: "/c:/WINDOWS/clock.avi"); ali@459: r |= test_pfu("file:///path/to/the%20file.txt", ali@459: "/path/to/the file.txt"); ali@459: r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien"); ali@459: r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš"); ali@459: r |= test_pfu("file:///var/log/22%20%e0%b8%aa%e0%b8%b4%e0%b8%87%e0%b8%ab%e0%b8%b2%e0%b8%84%e0%b8%a1%202014", ali@459: "/var/log/22 สิงหาคม 2014"); ali@459: ali@459: exit(r ? 1 : 0); ali@459: }