We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 244 times

Contents

Related Categories

The use of the code analysis library OpenC++: modifications, improvements, error corrections - 4. Function of full file paths disclosure.

AndreyKarpov

4. Function of full file paths disclosure.

In tasks of analysis of original code a large amount of functionality is related to creation of error messages and also to navigation on original files. What is inconvenient is that file names returned by such functions as Program::LineNumber() may be presented in different ways. Here are some examples:

C:\\Program Files\\MSVS 8\\VC\\atlmfc\\include\\afx.h
.\\drawing.cpp
c:\\src\\wxwindows-2.4.2\\samples\\drawing\\wx/defs.h
Boost\\boost-1_33_1\\boost/variant/recursive_variant.hpp
..\\FieldEdit2\\Src\\amsEdit.cpp
..\\..\\..\\src\\base\\ftbase.c

The way may be full or relative. Different delimiters may be used. All this makes the use of such ways inconvenient for processing or for output in information messages. That's why we offer realization of FixFileName() function bringing paths to uniform full way. An auxiliary function GetInputFileDirectory() is used to return the path to the catalogue where the processed file is situated.

const string &GetInputFileDirectory() {
  static string oldInputFileName;
  static string fileDirectory;
  string dir;
  VivaConfiguration &cfg = VivaConfiguration::Instance();
  string inputFileName;
  cfg.GetInputFileName(inputFileName);
  if (oldInputFileName == inputFileName)
    return fileDirectory;
  oldInputFileName = inputFileName;
  filesystem::path inputFileNamePath(inputFileName, filesystem::native);
  fileDirectory = inputFileNamePath.branch_path().string();
  if (fileDirectory.empty()) {
    TCHAR curDir[MAX_PATH];
    if (GetCurrentDirectory(MAX_PATH, curDir) != 0) {
      fileDirectory = curDir;
    } else {
      assert(false);
    }
  }
  algorithm::replace_all(fileDirectory, "/", "\\");
  to_lower(fileDirectory);
  return fileDirectory;
}
typedef map<string, string> StrStrMap;
typedef StrStrMap::iterator StrStrMapIt;
void FixFileName(string &fileName) {
  static StrStrMap FileNamesMap;
  StrStrMapIt it = FileNamesMap.find(fileName);
  if (it != FileNamesMap.end()) {
    fileName = it->second;
    return;
  }
  string oldFileName = fileName;
  algorithm::replace_all(fileName, "/", "\\");
  algorithm::replace_all(fileName, "\\\\", "\\");
  filesystem::path tmpPath(fileName, filesystem::native);
  fileName = tmpPath.string();
  algorithm::replace_all(fileName, "/", "\\");
  to_lower(fileName);
  if (fileName.length() < 2) {
    assert(false);
    FileNamesMap.insert(make_pair(oldFileName, fileName));
    return;
  }
 
  if (fileName[0] == '.' && fileName[1] != '.') {
    const string &dir = GetInputFileDirectory();
    if (!dir.empty())
      fileName.replace(0, 1, dir);
    FileNamesMap.insert(make_pair(oldFileName, fileName));
    return;
  }
  if (isalpha(fileName[0]) && fileName[1] == ':' ) {
    FileNamesMap.insert(make_pair(oldFileName, fileName));
    return;
  }
  const string &dir = GetInputFileDirectory();
  if (dir.empty())
    fileName.insert(0, ".\\");
  else {
    fileName.insert(0, "\\");
    fileName.insert(0, dir);
  }
  FileNamesMap.insert(make_pair(oldFileName, fileName));
}

Comments