Back to index

nordugrid-arc-nox  1.1.0~rc6
canonical_dir.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <string>
00006 #include "canonical_dir.h"
00007 
00008 int canonical_dir(std::string &name,bool leading_slash) {
00009   std::string::size_type i,ii,n;
00010   ii=0; i=0;
00011   for(;i<name.length();) {
00012     name[ii]=name[i];
00013     if(name[i] == '/') {
00014       n=i+1;
00015       if(n >= name.length()) {
00016         n=i; break;
00017       }
00018       else if(name[n] == '.') {
00019         n++;
00020         if(name[n] == '.') {
00021           n++;
00022           if((n >= name.length()) || (name[n] == '/')) {
00023             i=n;
00024             /* go 1 directory up */
00025             for(;;) {
00026               ii--;
00027               if(ii == std::string::npos) {
00028                  /* bad dir */
00029                 return 1;
00030               };
00031               if(name[i] == '/') break;
00032             };
00033           };
00034         }
00035         else if((n >= name.length()) || (name[n] == '/')) {
00036           i=n;
00037         };  
00038       }
00039       else if(name[n] == '/') {
00040         i=n;
00041       };
00042     };
00043     n=i; i++; ii++;
00044   };
00045   if(leading_slash) {
00046     if((name[0] != '/') || (ii==0)) { name="/"+name.substr(0,ii); }
00047     else { name=name.substr(0,ii); };
00048   }
00049   else {
00050     if((name[0] != '/') || (ii==0)) { name=name.substr(0,ii); }
00051     else { name=name.substr(1,ii-1); };
00052   };
00053   return 0;
00054 }
00055