Talk:Find common directory path: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 39: Line 39:
It is intended to work that way, and it works with "dovetail" also. The first conditional handles cases both with and without terminating slash. Try it. [[User:Per|Per]] 12:07, 14 April 2011 (UTC)
It is intended to work that way, and it works with "dovetail" also. The first conditional handles cases both with and without terminating slash. Try it. [[User:Per|Per]] 12:07, 14 April 2011 (UTC)


:ok, yes, nevermind. And the case I was worried about would have appeared with the provided example but is dealt with by a prior test. However, that does not mean that the algorithm is completely valid. Consider, for example, what happens if you change an instance of "home" to "hone". Here's my proposed alternative: <lang c>static void longestSharedPath(const char *fixed, char *moving) {
:ok, yes, nevermind. However, that does not mean that the algorithm is completely valid. Consider, for example, what happens if you change an instance of "home" to "hone". Here's my proposed alternative: <lang c>static void longestSharedPath(const char *fixed, char *moving) {
char *t;
char *t;
unsigned n = 0;
unsigned n = 0;

Revision as of 14:34, 14 April 2011

Shorter version for the C language:

<lang C>

  1. define PATH_MAX 127
  2. include <string.h>
  3. include <stdio.h>

static void longestSharedPath(const char *fixed, char *moving) { char *t; unsigned n = 0, l = strlen(fixed); while (moving[n] == fixed[n] && n < l) n++; if (strlen(moving) == n && (l == n || (l > n && fixed[n] == '/'))) return; moving[n] = '\0'; t = strrchr(moving, '/'); if (t && t != moving) *t = '\0'; }

int main() { char *dir_list[] = { "/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members", NULL }; int i = 0; char tmp[PATH_MAX]; strcpy(tmp, dir_list[0]); while (dir_list[++i]) { longestSharedPath(dir_list[i], tmp); } printf("%s\n", tmp); return 0; } </lang>

I think that the last two lines of leastCommonPath are wrong: <lang c> t = strrchr(moving, '/');

if (t && t != moving) *t = '\0';</lang>. This should place the null after the slash, and not eliminate the slash otherwise the slash will not be available for future comparisons. If you change "coverage" to "dovetail" in the example data, I think this problem would raise its head. --Rdm 11:46, 14 April 2011 (UTC)

It is intended to work that way, and it works with "dovetail" also. The first conditional handles cases both with and without terminating slash. Try it. Per 12:07, 14 April 2011 (UTC)

ok, yes, nevermind. However, that does not mean that the algorithm is completely valid. Consider, for example, what happens if you change an instance of "home" to "hone". Here's my proposed alternative: <lang c>static void longestSharedPath(const char *fixed, char *moving) {
       char *t;
       unsigned n = 0;
       while (moving[n] == fixed[n] && moving[n]) n++;
       if (!moving[n]) return;
       t = strrchr(moving, '/');
       if (t)
               if (t == moving)
                       moving[1]= '\0';
               else
                       *t = '\0';

}</lang> --Rdm 14:22, 14 April 2011 (UTC)