degate  0.1.2
Functions
FileSystem.cc File Reference
#include <FileSystem.h>
#include <Configuration.h>
#include <degate_exceptions.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <limits.h>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>

Go to the source code of this file.

Functions

char * realpath_alloc (const char *path)
char * _get_relative_path (const char *const path, const char *const relative_to)

Function Documentation

char* _get_relative_path ( const char *const  path,
const char *const  relative_to 
)

Definition at line 233 of file FileSystem.cc.

References realpath_alloc().

Referenced by degate::get_relative_path().

                                                                                   {
  char *path_real;
  char *path_real_suffix;
  char *path_real_to;
  char *path_real_to_suffix;
  char *result;
  int prefix_len, i, slash_count;
  char *string_end;
  char path_separator;
#ifdef _WIN32
  path_separator = '\\';
#else
  path_separator = '/';
#endif

  if (NULL == path || NULL == relative_to)
    return NULL;

  path_real = realpath_alloc(path);
  if (!path_real)
    return NULL;
  path_real_to = realpath_alloc(relative_to);
  if (!path_real_to)
    {
      free(path_real);
      return NULL;
    }

  if (0 == strcmp(path_real, path_real_to))
    {
      free(path_real);
      free(path_real_to);

      //the two directories are equal, the relative path is an empty string
      result = (char*)malloc(sizeof(char));
      *result = '\0';
      return result;
    }

  result = NULL;

  //eliminate the common prefix
  for (prefix_len = 0;
       path_real[prefix_len] != '\0' &&
         path_real_to[prefix_len] != '\0' &&
         path_real[prefix_len] == path_real_to[prefix_len];
       ++prefix_len);

  path_real_suffix = path_real + prefix_len;
  while ('\0' != *path_real_suffix &&
#ifdef _WIN32
         ('/' == *path_real_suffix || '\\' == *path_real_suffix)
#else
         ('/' == *path_real_suffix)
#endif
         ) { *path_real_suffix++ = '\0'; }

  path_real_to_suffix = path_real_to + prefix_len;
  while ('\0' != *path_real_to_suffix &&
#ifdef _WIN32
         ('/' == *path_real_to_suffix || '\\' == *path_real_to_suffix)
#else
         ('/' == *path_real_to_suffix)
#endif
         ) { *path_real_to_suffix++ = '\0'; }

  slash_count = 0;
  for (i = 0; '\0' != path_real_to_suffix[i]; ++i)
#ifdef _WIN32
    if ('/' == path_real_to_suffix[i] || '\\' == path_real_to_suffix[i])
#else
      if ('/' == path_real_to_suffix[i])
#endif
        ++slash_count;
  if ('\0' != *path_real_to_suffix) ++slash_count;
  result = (char*)malloc(sizeof(char) * (4 + 3 * slash_count + strlen(path_real_suffix)));

  string_end = result;
  for (i = 0; i < slash_count; ++i)
    {
      if (i > 0)
        *string_end++ = path_separator;
      *string_end++ = '.';
      *string_end++ = '.';
    }
  if (0 == slash_count)
    *string_end++ = '.';
  if ('\0' != *path_real_suffix)
    {
      *string_end++ = path_separator;
      for (i = 0; '\0' != path_real_suffix[i]; ++i)
        *string_end++ = path_real_suffix[i];
    }
  *string_end++ = '\0';

  free(path_real);
  free(path_real_to);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* realpath_alloc ( const char *  path)

Definition at line 207 of file FileSystem.cc.

Referenced by _get_relative_path().

{
  int buff_len;
  char *result;

#ifdef PATH_MAX
  buff_len = PATH_MAX;
#else
  buff_len = pathconf(path, _PC_PATH_MAX);
  if (buff_len <= 0)
    buff_len = 4096;
#endif

  ++buff_len;
  result = (char*)malloc(buff_len * sizeof(char));
  if (!result)
    return NULL;

  if(realpath(path, result) == NULL) {
    free(result);
    return NULL;
  }
  else return result;
}

Here is the caller graph for this function: