diff options
author | Bruno JOFRET <bruno.jofret@scilab.org> | 2011-05-03 10:01:07 +0200 |
---|---|---|
committer | Vincent COUVERT <vincent.couvert@scilab.org> | 2011-05-06 10:48:57 +0200 |
commit | 1287e683466d4e187be3240860829b9566e3e36c (patch) | |
tree | ceff0ad4ebca53e624d850bc2f1fbbd08d374b33 /scilab/modules | |
parent | 4a1dd79532bcff6a55c474344cbfc486e4570b6b (diff) | |
download | scilab-1287e683466d4e187be3240860829b9566e3e36c.zip scilab-1287e683466d4e187be3240860829b9566e3e36c.tar.gz |
Update getrelativefilename doc + add unitary test.
Change-Id: I160a4f94db17754300f9a25e07968d8b858a2ff5
Diffstat (limited to 'scilab/modules')
4 files changed, 221 insertions, 119 deletions
diff --git a/scilab/modules/fileio/help/en_US/getrelativefilename.xml b/scilab/modules/fileio/help/en_US/getrelativefilename.xml index de8bec6..e759b1a 100644 --- a/scilab/modules/fileio/help/en_US/getrelativefilename.xml +++ b/scilab/modules/fileio/help/en_US/getrelativefilename.xml | |||
@@ -2,11 +2,11 @@ | |||
2 | <!-- | 2 | <!-- |
3 | * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab | 3 | * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab |
4 | * Copyright (C) XXXX-2008 - INRIA | 4 | * Copyright (C) XXXX-2008 - INRIA |
5 | * | 5 | * |
6 | * This file must be used under the terms of the CeCILL. | 6 | * This file must be used under the terms of the CeCILL. |
7 | * This source file is licensed as described in the file COPYING, which | 7 | * This source file is licensed as described in the file COPYING, which |
8 | * you should have received as part of this distribution. The terms | 8 | * you should have received as part of this distribution. The terms |
9 | * are also available at | 9 | * are also available at |
10 | * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt | 10 | * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt |
11 | * | 11 | * |
12 | --> | 12 | --> |
@@ -53,19 +53,13 @@ | |||
53 | </refsection> | 53 | </refsection> |
54 | <refsection> | 54 | <refsection> |
55 | <title>Examples</title> | 55 | <title>Examples</title> |
56 | <programlisting role="example"><![CDATA[ | 56 | <programlisting role="example"><![CDATA[ |
57 | if getos() == 'Windows' then | 57 | getrelativefilename(pwd(), SCI+'/COPYING') |
58 | getrelativefilename('C:\program file\scilab-4.0\bin','C:\program file\scilab-4.0\modules\helptools\help.dtd') | 58 | getrelativefilename(TMPDIR, SCI+'/COPYING') |
59 | getrelativefilename('C:\program file\scilab-4.0\bin\','C:\program file\scilab-4.0\modules\helptools\help.dtd') | 59 | getrelativefilename(SCI+'/bin', SCI+'/COPYING') |
60 | getrelativefilename(SCI+'\bin',SCI+'\modules\helptools\help.dtd') | 60 | getrelativefilename(pwd(), SCI+'/ACKNOLEDGMENTS') |
61 | getrelativefilename(WSCI+'\bin',WSCI+'\modules\helptools\help.dtd') | 61 | getrelativefilename(TMPDIR, SCI+'/ACKNOLEDGMENTS') |
62 | getrelativefilename(pwd(),WSCI+'\bin\Wscilex') | 62 | getrelativefilename(SCI+'/bin', SCI+'/ACKNOLEDGMENTS') |
63 | else | ||
64 | getrelativefilename('/usr/local/scilab-4.0/bin','/usr/local/scilab-4.0/modules/helptools/help.dtd') | ||
65 | getrelativefilename('/usr/local/scilab-4.0/bin/','/usr/local/scilab-4.0/modules/helptools/help.dtd') | ||
66 | getrelativefilename(SCI+'/bin',SCI+'/modules/helptools/help.dtd') | ||
67 | getrelativefilename(pwd(),SCI+'/bin/scilex') | ||
68 | end | ||
69 | ]]></programlisting> | 63 | ]]></programlisting> |
70 | </refsection> | 64 | </refsection> |
71 | <refsection role="see also"> | 65 | <refsection role="see also"> |
diff --git a/scilab/modules/fileio/src/c/getrelativefilename.c b/scilab/modules/fileio/src/c/getrelativefilename.c index acf16d0..707d333 100644 --- a/scilab/modules/fileio/src/c/getrelativefilename.c +++ b/scilab/modules/fileio/src/c/getrelativefilename.c | |||
@@ -1,6 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab | 2 | * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab |
3 | * Copyright (C) 2006 - INRIA - Pierre MARECHAL | 3 | * Copyright (C) 2006 - INRIA - Pierre MARECHAL |
4 | * Copyright (C) 2011 - DIGITEO - Allan CORNET | ||
4 | * | 5 | * |
5 | * This file must be used under the terms of the CeCILL. | 6 | * This file must be used under the terms of the CeCILL. |
6 | * This source file is licensed as described in the file COPYING, which | 7 | * This source file is licensed as described in the file COPYING, which |
@@ -14,7 +15,9 @@ | |||
14 | #include "getrelativefilename.h" | 15 | #include "getrelativefilename.h" |
15 | #include "MALLOC.h" | 16 | #include "MALLOC.h" |
16 | #include "PATH_MAX.h" | 17 | #include "PATH_MAX.h" |
17 | 18 | #include "strsubst.h" | |
19 | /* ================================================================================== */ | ||
20 | static char *normalizeFileSeparator(const char *path); | ||
18 | /* ================================================================================== */ | 21 | /* ================================================================================== */ |
19 | // getrelativefilename | 22 | // getrelativefilename |
20 | // | 23 | // |
@@ -22,114 +25,144 @@ | |||
22 | // For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given, | 25 | // For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given, |
23 | // GetRelativeFilename will return ..\whee\text.txt. | 26 | // GetRelativeFilename will return ..\whee\text.txt. |
24 | /* ================================================================================== */ | 27 | /* ================================================================================== */ |
25 | |||
26 | char* getrelativefilename(char *currentDirectory, char *absoluteFilename) | 28 | char* getrelativefilename(char *currentDirectory, char *absoluteFilename) |
27 | { | 29 | { |
28 | // declarations - put here so this should work in a C compiler | 30 | // declarations - put here so this should work in a C compiler |
29 | int afMarker = 0, rfMarker = 0; | 31 | int afMarker = 0, rfMarker = 0; |
30 | int cdLen = 0, afLen = 0; | 32 | int cdLen = 0, afLen = 0; |
31 | int i = 0; | 33 | int i = 0; |
32 | int levels = 0; | 34 | int levels = 0; |
33 | char *relativeFilename= (char*)MALLOC(PATH_MAX*sizeof(char)); | 35 | char *relativeFilename= (char*)MALLOC(PATH_MAX*sizeof(char)); |
34 | 36 | char *_currentDirectory = normalizeFileSeparator(currentDirectory); | |
35 | cdLen = (int)strlen(currentDirectory); | 37 | char *_absoluteFilename = normalizeFileSeparator(absoluteFilename); |
36 | afLen = (int)strlen(absoluteFilename); | 38 | |
37 | 39 | cdLen = (int)strlen(_currentDirectory); | |
38 | // make sure the names are not too short | 40 | afLen = (int)strlen(_absoluteFilename); |
39 | if( cdLen < ABSOLUTE_NAME_START+1 || afLen < ABSOLUTE_NAME_START+1) | 41 | |
40 | { | 42 | // make sure the names are not too short |
41 | // fix bug 2181 | 43 | if( cdLen < ABSOLUTE_NAME_START+1 || afLen < ABSOLUTE_NAME_START+1) |
42 | strcpy(relativeFilename, absoluteFilename); | 44 | { |
43 | return relativeFilename; | 45 | // fix bug 2181 |
44 | } | 46 | strcpy(relativeFilename, _absoluteFilename); |
45 | 47 | if (_currentDirectory) {FREE(_currentDirectory); _currentDirectory = NULL;} | |
46 | // Handle DOS names that are on different drives: | 48 | if (_absoluteFilename) {FREE(_absoluteFilename); _absoluteFilename = NULL;} |
47 | if(tolower(currentDirectory[0]) != tolower(absoluteFilename[0])) | 49 | return relativeFilename; |
48 | { | 50 | } |
49 | // not on the same drive, so only absolute filename will do | 51 | |
50 | strcpy(relativeFilename, absoluteFilename); | 52 | // Handle DOS names that are on different drives: |
51 | return relativeFilename; | 53 | if(tolower(_currentDirectory[0]) != tolower(_absoluteFilename[0])) |
52 | } | 54 | { |
53 | 55 | // not on the same drive, so only absolute filename will do | |
54 | // they are on the same drive, find out how much of the current directory | 56 | strcpy(relativeFilename, _absoluteFilename); |
55 | // is in the absolute filename | 57 | if (_currentDirectory) {FREE(_currentDirectory); _currentDirectory = NULL;} |
56 | i = ABSOLUTE_NAME_START; | 58 | if (_absoluteFilename) {FREE(_absoluteFilename); _absoluteFilename = NULL;} |
59 | return relativeFilename; | ||
60 | } | ||
61 | |||
62 | // they are on the same drive, find out how much of the current directory | ||
63 | // is in the absolute filename | ||
64 | i = ABSOLUTE_NAME_START; | ||
57 | 65 | ||
58 | #if defined(_MSC_VER) | 66 | #if defined(_MSC_VER) |
59 | while(i < afLen && i < cdLen && tolower(currentDirectory[i])==tolower(absoluteFilename[i]) ) | 67 | while(i < afLen && i < cdLen && tolower(_currentDirectory[i]) == tolower(_absoluteFilename[i]) ) |
60 | { | 68 | { |
61 | i++; | 69 | i++; |
62 | } | 70 | } |
63 | #else | 71 | #else |
64 | while(i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i]) | 72 | while(i < afLen && i < cdLen && _currentDirectory[i] == _absoluteFilename[i]) |
65 | { | 73 | { |
66 | i++; | 74 | i++; |
67 | } | 75 | } |
68 | #endif | 76 | #endif |
69 | 77 | ||
70 | if(i == cdLen && (absoluteFilename[i] == DIR_SEPARATOR[0] || absoluteFilename[i-1] == DIR_SEPARATOR[0])) | 78 | if(i == cdLen && (_absoluteFilename[i] == DIR_SEPARATOR[0] || _absoluteFilename[i-1] == DIR_SEPARATOR[0])) |
71 | { | 79 | { |
72 | // the whole current directory name is in the file name, | 80 | // the whole current directory name is in the file name, |
73 | // so we just trim off the current directory name to get the | 81 | // so we just trim off the current directory name to get the |
74 | // current file name. | 82 | // current file name. |
75 | if(absoluteFilename[i] == DIR_SEPARATOR[0]) | 83 | if(_absoluteFilename[i] == DIR_SEPARATOR[0]) |
76 | { | 84 | { |
77 | // a directory name might have a trailing slash but a relative | 85 | // a directory name might have a trailing slash but a relative |
78 | // file name should not have a leading one... | 86 | // file name should not have a leading one... |
79 | i++; | 87 | i++; |
80 | } | 88 | } |
81 | 89 | ||
82 | strcpy(relativeFilename, &absoluteFilename[i]); | 90 | strcpy(relativeFilename, &_absoluteFilename[i]); |
83 | return relativeFilename; | 91 | if (_currentDirectory) {FREE(_currentDirectory); _currentDirectory = NULL;} |
84 | } | 92 | if (_absoluteFilename) {FREE(_absoluteFilename); _absoluteFilename = NULL;} |
85 | 93 | return relativeFilename; | |
86 | // The file is not in a child directory of the current directory, so we | 94 | } |
87 | // need to step back the appropriate number of parent directories by | 95 | |
88 | // using "..\"s. First find out how many levels deeper we are than the | 96 | // The file is not in a child directory of the current directory, so we |
89 | // common directory | 97 | // need to step back the appropriate number of parent directories by |
90 | afMarker = i; | 98 | // using "..\"s. First find out how many levels deeper we are than the |
91 | levels = 1; | 99 | // common directory |
92 | 100 | afMarker = i; | |
93 | // count the number of directory levels we have to go up to get to the | 101 | levels = 1; |
94 | // common directory | 102 | |
95 | while(i < cdLen) | 103 | // count the number of directory levels we have to go up to get to the |
96 | { | 104 | // common directory |
97 | i++; | 105 | while(i < cdLen) |
98 | if(currentDirectory[i] == DIR_SEPARATOR[0]) | 106 | { |
99 | { | 107 | i++; |
100 | // make sure it's not a trailing slash | 108 | if(_currentDirectory[i] == DIR_SEPARATOR[0]) |
101 | i++; | 109 | { |
102 | if(currentDirectory[i] != '\0') | 110 | // make sure it's not a trailing slash |
103 | { | 111 | i++; |
104 | levels++; | 112 | if(_currentDirectory[i] != '\0') |
105 | } | 113 | { |
106 | } | 114 | levels++; |
107 | } | 115 | } |
108 | 116 | } | |
109 | // move the absolute filename marker back to the start of the directory name | 117 | } |
110 | // that it has stopped in. | 118 | |
111 | while(afMarker > 0 && absoluteFilename[afMarker-1] != DIR_SEPARATOR[0]) | 119 | // move the absolute filename marker back to the start of the directory name |
112 | { | 120 | // that it has stopped in. |
113 | afMarker--; | 121 | while(afMarker > 0 && _absoluteFilename[afMarker-1] != DIR_SEPARATOR[0]) |
114 | } | 122 | { |
115 | 123 | afMarker--; | |
116 | // check that the result will not be too long | 124 | } |
117 | if(levels * 3 + afLen - afMarker > PATH_MAX) | 125 | |
118 | { | 126 | // check that the result will not be too long |
119 | return NULL; | 127 | if(levels * 3 + afLen - afMarker > PATH_MAX) |
120 | } | 128 | { |
121 | 129 | if (_currentDirectory) {FREE(_currentDirectory); _currentDirectory = NULL;} | |
122 | // add the appropriate number of "..\"s. | 130 | if (_absoluteFilename) {FREE(_absoluteFilename); _absoluteFilename = NULL;} |
123 | rfMarker = 0; | 131 | return NULL; |
124 | for(i = 0; i < levels; i++) | 132 | } |
125 | { | 133 | |
126 | relativeFilename[rfMarker++] = '.'; | 134 | // add the appropriate number of "..\"s. |
127 | relativeFilename[rfMarker++] = '.'; | 135 | rfMarker = 0; |
128 | relativeFilename[rfMarker++] = DIR_SEPARATOR[0]; | 136 | for(i = 0; i < levels; i++) |
129 | } | 137 | { |
130 | 138 | relativeFilename[rfMarker++] = '.'; | |
131 | // copy the rest of the filename into the result string | 139 | relativeFilename[rfMarker++] = '.'; |
132 | strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]); | 140 | relativeFilename[rfMarker++] = DIR_SEPARATOR[0]; |
133 | 141 | } | |
134 | return relativeFilename; | 142 | |
143 | // copy the rest of the filename into the result string | ||
144 | strcpy(&relativeFilename[rfMarker], &_absoluteFilename[afMarker]); | ||
145 | |||
146 | if (_currentDirectory) {FREE(_currentDirectory); _currentDirectory = NULL;} | ||
147 | if (_absoluteFilename) {FREE(_absoluteFilename); _absoluteFilename = NULL;} | ||
148 | |||
149 | return relativeFilename; | ||
135 | } | 150 | } |
151 | /* ================================================================================== */ | ||
152 | char *normalizeFileSeparator(const char *path) | ||
153 | { | ||
154 | #define WINDOWS_FILESEPARATOR "\\" | ||
155 | #define OTHERS_FILESEPARATOR "/" | ||
156 | |||
157 | char *normalizedPath = NULL; | ||
158 | if (path) | ||
159 | { | ||
160 | #ifdef _MSC_VER | ||
161 | normalizedPath = strsub((char*)path, OTHERS_FILESEPARATOR, DIR_SEPARATOR); | ||
162 | #else | ||
163 | normalizedPath = strsub((char*)path, WINDOWS_FILESEPARATOR, DIR_SEPARATOR); | ||
164 | #endif | ||
165 | } | ||
166 | return normalizedPath; | ||
167 | } | ||
168 | /* ================================================================================== */ | ||
diff --git a/scilab/modules/fileio/tests/unit_tests/getrelativefilename.dia.ref b/scilab/modules/fileio/tests/unit_tests/getrelativefilename.dia.ref new file mode 100644 index 0000000..94fd207 --- /dev/null +++ b/scilab/modules/fileio/tests/unit_tests/getrelativefilename.dia.ref | |||
@@ -0,0 +1,33 @@ | |||
1 | // ============================================================================= | ||
2 | // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab | ||
3 | // Copyright (C) 2011 - DIGITEO - Bruno JOFRET | ||
4 | // Copyright (C) 2011 - DIGITEO - Allan CORNET | ||
5 | // | ||
6 | // This file is distributed under the same license as the Scilab package. | ||
7 | // ============================================================================= | ||
8 | // <-- JVM NOT MANDATORY --> | ||
9 | ierr = execstr("getrelativefilename();","errcatch"); | ||
10 | if ierr <> 77 then bugmes();quit;end | ||
11 | ierr = execstr("getrelativefilename([], []);","errcatch"); | ||
12 | if ierr <> 999 then bugmes();quit;end | ||
13 | __dir1 = TMPDIR+"/dir1"; | ||
14 | __dir11 = TMPDIR+"/dir1/dir1.1"; | ||
15 | __dir12 = TMPDIR+"/dir1/dir1.2"; | ||
16 | mkdir(__dir1); | ||
17 | mkdir(__dir11); | ||
18 | mkdir(__dir12); | ||
19 | __file1 = __dir1+"/file1.txt"; | ||
20 | __file11 = __dir11+"/file11.txt"; | ||
21 | __file12 = __dir12+"/file12.txt"; | ||
22 | mputl("", __file1); | ||
23 | mputl("", __file11); | ||
24 | mputl("", __file12); | ||
25 | if getrelativefilename(__dir1, __file1) <> "file1.txt" then bugmes();quit;end | ||
26 | if getrelativefilename(__dir1, __file11) <> pathconvert("dir1.1/file11.txt", %f) then bugmes();quit;end | ||
27 | if getrelativefilename(__dir1, __file12) <> pathconvert("dir1.2/file12.txt", %f) then bugmes();quit;end | ||
28 | if getrelativefilename(__dir11, __file1) <> pathconvert("../file1.txt", %f) then bugmes();quit;end | ||
29 | if getrelativefilename(__dir11, __file11) <> "file11.txt" then bugmes();quit;end | ||
30 | if getrelativefilename(__dir11, __file12) <> pathconvert("../dir1.2/file12.txt", %f) then bugmes();quit;end | ||
31 | if getrelativefilename(__dir12, __file1) <> pathconvert("../file1.txt", %f) then bugmes();quit;end; | ||
32 | if getrelativefilename(__dir12, __file11) <> pathconvert("../dir1.1/file11.txt", %f) then bugmes();quit;end; | ||
33 | if getrelativefilename(__dir12, __file12) <> "file12.txt" then bugmes();quit;end; | ||
diff --git a/scilab/modules/fileio/tests/unit_tests/getrelativefilename.tst b/scilab/modules/fileio/tests/unit_tests/getrelativefilename.tst new file mode 100644 index 0000000..6a3c5e3 --- /dev/null +++ b/scilab/modules/fileio/tests/unit_tests/getrelativefilename.tst | |||
@@ -0,0 +1,42 @@ | |||
1 | // ============================================================================= | ||
2 | // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab | ||
3 | // Copyright (C) 2011 - DIGITEO - Bruno JOFRET | ||
4 | // Copyright (C) 2011 - DIGITEO - Allan CORNET | ||
5 | // | ||
6 | // This file is distributed under the same license as the Scilab package. | ||
7 | // ============================================================================= | ||
8 | |||
9 | // <-- JVM NOT MANDATORY --> | ||
10 | ierr = execstr("getrelativefilename();","errcatch"); | ||
11 | if ierr <> 77 then pause,end | ||
12 | |||
13 | ierr = execstr("getrelativefilename([], []);","errcatch"); | ||
14 | if ierr <> 999 then pause,end | ||
15 | |||
16 | __dir1 = TMPDIR+"/dir1"; | ||
17 | __dir11 = TMPDIR+"/dir1/dir1.1"; | ||
18 | __dir12 = TMPDIR+"/dir1/dir1.2"; | ||
19 | |||
20 | mkdir(__dir1); | ||
21 | mkdir(__dir11); | ||
22 | mkdir(__dir12); | ||
23 | |||
24 | __file1 = __dir1+"/file1.txt"; | ||
25 | __file11 = __dir11+"/file11.txt"; | ||
26 | __file12 = __dir12+"/file12.txt"; | ||
27 | |||
28 | mputl("", __file1); | ||
29 | mputl("", __file11); | ||
30 | mputl("", __file12); | ||
31 | |||
32 | if getrelativefilename(__dir1, __file1) <> "file1.txt" then pause, end | ||
33 | if getrelativefilename(__dir1, __file11) <> pathconvert("dir1.1/file11.txt", %f) then pause, end | ||
34 | if getrelativefilename(__dir1, __file12) <> pathconvert("dir1.2/file12.txt", %f) then pause, end | ||
35 | |||
36 | if getrelativefilename(__dir11, __file1) <> pathconvert("../file1.txt", %f) then pause, end | ||
37 | if getrelativefilename(__dir11, __file11) <> "file11.txt" then pause, end | ||
38 | if getrelativefilename(__dir11, __file12) <> pathconvert("../dir1.2/file12.txt", %f) then pause, end | ||
39 | |||
40 | if getrelativefilename(__dir12, __file1) <> pathconvert("../file1.txt", %f) then pause, end; | ||
41 | if getrelativefilename(__dir12, __file11) <> pathconvert("../dir1.1/file11.txt", %f) then pause, end; | ||
42 | if getrelativefilename(__dir12, __file12) <> "file12.txt" then pause, end; | ||