1.1.1 • Published 7 years ago

cpp-include v1.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

cpp-include

Build Status Coverage Status Dependency Status NPM MIT

Collect path of #include directive on C/C++ source code.

const cppInclude = require('cpp-include');

cppInclude.getIncludeFiles("myapp.cpp");
/* [
  { path: "myapp.h", local: true },
]*/
cppInclude.getIncludeFiles("myapp.h");
/* [
  { path: "iostream", local: false },
  { path: "mylib.h", local: true },
]*/

local represents whether include directive is quoted-form (local: true) or angle-bracket form (local: false)

If want to input source code string rather than path, use getIncludeFilesFromString

var src = "#include <iostream>\n#include \"mylib.h\"";
cppInclude.getIncludeFilesFromString(src);
/* [
  { path: "iostream", local: false },
  { path: "mylib.h", local: true },
]*/

Find the actual full path of include file

Return items of getIncludeFiles and getIncludeFilesFromString respectively has two additional members: find and origin.

  • origin represents the path of the C/C++ source code that was passed to getIncludeFiles.
    • This value can be changed at any time, changing it affects the find method.
    • NOTE: Semantically this value MUST be file path rather than directory path. But find method can work fine as long as specified directory exist. See ensureDirname internal function to learn why.
  • find is the method to accuire the actual location on the machine.
    • When local is true, this method try to find from the following directories:
      • origin
      • 1st argument of this method (string, or Array of string)
      • CPATH (Linux/Mac) or INCLUDE (Windows)
    • Otherwise (local is false)
      • 1st argument of this method (string, or Array of string)
      • CPATH (Linux/Mac) or INCLUDE (Windows)
cppInclude.getIncludeFile("myapp.cpp")
  .filter(f => { return f.path == "iostream" })[0]
  .find();
// C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\iostream

cppInclude.getIncludeFile("myapp.cpp")
  .filer(f => { return f.path == "mylib.h" })[0]
  .find("C:\\Users\\retorillo\\include");
// C:\Users\retorillo\include\mylib.h

License

Distirubted under the MIT license

Copyright (C) 2017 Retorillo