Example #1
0
static WrapperStr relative_path(const QDir & destdir, WrapperStr relto)
{
    QDir fromdir(relto);
    /*WrapperStr from = WrapperStr(fromdir.absPath());
    WrapperStr to = WrapperStr(destdir.absPath());*/
    QByteArray fromBa = fromdir.absolutePath().toLatin1();
    QByteArray toBa = destdir.absolutePath().toLatin1();
    const char * cfrom = fromBa.constData();
    const char * cto = toBa.constData();
    int lastsep = -1;
    int index = 0;

    for (;;) {
        char f = cfrom[index];
        char t = cto[index];

        if (f == 0) {
            switch (t) {
            case 0:
                // same path
                return "";

            case '/':
                // to = .../aze/qsd/wxc, from = .../aze => qsd/wxc/
                return (cto + index + 1) + WrapperStr("/");

            default:
                // to = .../aze/qsd/wxc, from = .../az => ../aze/qsd/wxc/
                return "../" + WrapperStr(cto + lastsep + 1) + "/";
            }
        }
        else if (t == f) {
            if (t == '/')
                lastsep = index;

            index += 1;
        }
        else if (t == 0) {
            WrapperStr r;
            const char * p = cfrom + index;

            do {
                if (*p == '/')
                    r += "../";
            }
            while (*++p != 0);

            if (f == '/')
                // to = .../aze, from = .../aze/qsd/wxc => ../../
                return r;
            else
                // to = .../az, from = .../aze/qsd/wxc => ../../../az/
                return ("../"  + r + (cto + lastsep + 1)) + "/";
        }
        else {
            // to = .../aze, from = .../iop/klm => ../../aze/
            WrapperStr r = "../";
            const char * p = cfrom + lastsep + 1;

            while (*p != 0)
                if (*p++ == '/')
                    r += "../";

            return (r + (cto + lastsep + 1)) + "/";
        }
    }
}
Example #2
0
static Q3CString relative_path(const QDir & destdir, Q3CString relto)
{
  QDir fromdir(relto);
  Q3CString from = Q3CString(fromdir.absPath().toAscii().constData());
  Q3CString to = Q3CString(destdir.absPath().toAscii().constData());
  const char * cfrom = from;
  const char * cto = to;
  int lastsep = -1;
  int index = 0;
  
  for (;;) {
    char f = cfrom[index];
    char t = cto[index];
    
    if (f == 0) {
      switch (t) {
      case 0:
	// same path
	return "";
      case '/':
	// to = .../aze/qsd/wxc, from = .../aze => qsd/wxc/
	return (cto + index + 1) + Q3CString("/");
      default:
	// to = .../aze/qsd/wxc, from = .../az => ../aze/qsd/wxc/
	return "../" + Q3CString(cto + lastsep + 1) + "/";
      }
    }
    else if (t == f) {
      if (t == '/')
	lastsep = index;
      index += 1;
    }
    else if (t == 0) {
      Q3CString r;
      const char * p = cfrom+index;
      
      do {
	if (*p == '/')
	  r += "../";
      } while (*++p != 0);
      
      if (f == '/')
	// to = .../aze, from = .../aze/qsd/wxc => ../../
	return r;
      else
	// to = .../az, from = .../aze/qsd/wxc => ../../../az/
	return ("../"  + r + (cto + lastsep + 1)) + "/";
    }
    else {
      // to = .../aze, from = .../iop/klm => ../../aze/
      Q3CString r = "../";
      const char * p = cfrom + lastsep + 1;
      
      while (*p != 0)
	if (*p++ == '/')
	  r += "../";
      
      return (r + (cto + lastsep + 1)) + "/";
    }
  }
}