Exemple #1
0
bool Foam::dynamicCode::createMakeFiles() const
{
    // Create Make/files
    if (compileFiles_.empty())
    {
        return false;
    }

    const fileName dstFile(this->codePath()/"Make/files");

    // Create dir
    mkDir(dstFile.path());

    OFstream os(dstFile);
    //Info<< "Writing to " << dstFile << endl;
    if (!os.good())
    {
        FatalErrorInFunction
                << "Failed writing " << dstFile
                << exit(FatalError);
    }

    writeCommentSHA1(os);

    // Write compile files
    forAll(compileFiles_, fileI)
    {
        os.writeQuoted(compileFiles_[fileI], false) << nl;
    }
// Create a softlink. dst should not exist. Returns true if successful.
bool Foam::ln(const fileName& src, const fileName& dst)
{
    if (POSIX::debug)
    {
        Info<< "Create softlink from : " << src << " to " << dst
            << endl;
    }

    if (exists(dst))
    {
        WarningIn("ln(const fileName&, const fileName&)")
            << "destination " << dst << " already exists. Not linking."
            << endl;
        return false;
    }

    if (!exists(src))
    {
        WarningIn("ln(const fileName&, const fileName&)")
            << "source " << src << " does not exist." << endl;
        return false;
    }

    if (symlink(src.c_str(), dst.c_str()) == 0)
    {
        return true;
    }
    else
    {
        WarningIn("ln(const fileName&, const fileName&)")
            << "symlink from " << src << " to " << dst << " failed." << endl;
        return false;
    }
}
Exemple #3
0
void triSurf::readSurface(const fileName& fName)
{
    if( fName.ext() == "fms" || fName.ext() == "FMS" )
    {
        readFromFMS(fName);
    }
    else if( fName.ext() == "ftr" || fName.ext() == "FTR" )
    {
        readFromFTR(fName);
    }
    else
    {
        triSurface copySurface(fName);

        //- copy the points
        triSurfPoints::points_.setSize(copySurface.points().size());
        forAll(copySurface.points(), pI)
            triSurfPoints::points_[pI] = copySurface.points()[pI];

        //- copy the triangles
        triSurfFacets::triangles_.setSize(copySurface.size());
        forAll(copySurface, tI)
            triSurfFacets::triangles_[tI] = copySurface[tI];

        //- copy patches
        triSurfFacets::patches_ = copySurface.patches();
    }
}
void Foam::ensightSetWriter<Type>::write
(
    const coordSet& points,
    const wordList& valueSetNames,
    const List<const Field<Type>*>& valueSets,
    Ostream& os
) const
{
    const fileName base(os.name().lessExt());
    const fileName meshFile(base + ".mesh");

    // Write .case file
    os  << "FORMAT" << nl
        << "type: ensight gold" << nl
        << nl
        << "GEOMETRY" << nl
        << "model:        1     " << meshFile.name().c_str() << nl
        << nl
        << "VARIABLE"
        << nl;
    forAll(valueSetNames, setI)
    {
        fileName dataFile(base + ".***." + valueSetNames[setI]);

        os.setf(ios_base::left);
        os  << pTraits<Type>::typeName
            << " per node:            1       "
            << setw(15) << valueSetNames[setI]
            << " " << dataFile.name().c_str()
            << nl;
    }
Exemple #5
0
Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
:
    ifPtr_(NULL),
    compression_(IOstream::UNCOMPRESSED)
{
    if (pathname.empty())
    {
        if (IFstream::debug)
        {
            Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
                    "cannot open null file " << endl;
        }
    }

    ifPtr_ = new ifstream(pathname.c_str());

    // If the file is compressed, decompress it before reading.
    if (!ifPtr_->good() && isFile(pathname + ".gz", false))
    {
        if (IFstream::debug)
        {
            Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
                    "decompressing " << pathname + ".gz" << endl;
        }

        delete ifPtr_;

        ifPtr_ = new igzstream((pathname + ".gz").c_str());

        if (ifPtr_->good())
        {
            compression_ = IOstream::COMPRESSED;
        }
    }
}
Foam::autoPtr<Foam::edgeMesh> Foam::edgeMesh::New(const fileName& name)
{
    word ext = name.ext();
    if (ext == "gz")
    {
        ext = name.lessExt().ext();
    }
    return New(name, ext);
}
Foam::autoPtr<Foam::UnsortedMeshedSurface<Face>>
Foam::UnsortedMeshedSurface<Face>::New(const fileName& name)
{
    word ext = name.ext();
    if (ext == "gz")
    {
        ext = name.lessExt().ext();
    }

    return New(name, ext);
}
bool Foam::UnsortedMeshedSurface<Face>::canRead
(
    const fileName& name,
    const bool verbose
)
{
    word ext = name.ext();
    if (ext == "gz")
    {
        ext = name.lessExt().ext();
    }
    return canReadType(ext, verbose);
}
bool Foam::edgeMesh::read(const fileName& name)
{
    word ext = name.ext();
    if (ext == "gz")
    {
        fileName unzipName = name.lessExt();
        return read(unzipName, unzipName.ext());
    }
    else
    {
        return read(name, ext);
    }
}
bool Foam::edgeMesh::canRead
(
    const fileName& name,
    const bool verbose
)
{
    word ext = name.ext();
    if (ext == "gz")
    {
        ext = name.lessExt().ext();
    }
    return canReadType(ext, verbose);
}
bool mkDir(const fileName& pathName, const mode_t mode)
{
    if (pathName.empty())
    {
        return false;
    }


    bool success = ::CreateDirectory(pathName.c_str(), NULL);

    if (success)
    {
        chMod(pathName, mode);
    }
    else
    {
        const DWORD error = ::GetLastError();

        switch (error)
        {
        case ERROR_ALREADY_EXISTS:
        {
            success = true;
            break;
        }
        case ERROR_PATH_NOT_FOUND:
        {
            // Part of the path does not exist so try to create it
            const fileName& parentName = pathName.path();

            if (parentName.size() && mkDir(parentName, mode))
            {
                success = mkDir(pathName, mode);
            }

            break;
        }
        }

        if (!success)
        {
            FatalErrorIn("mkDir(const fileName&, mode_t)")
                    << "Couldn't create directory: " << pathName
                    << " " << MSwindows::getLastError()
                    << exit(FatalError);
        }
    }

    return success;
}
void Foam::edgeMesh::write
(
    const fileName& name,
    const edgeMesh& mesh
)
{
    if (debug)
    {
        Info<< "edgeMesh::write"
            "(const fileName&, const edgeMesh&) : "
            "writing to " << name
            << endl;
    }

    const word ext = name.ext();

    writefileExtensionMemberFunctionTable::iterator mfIter =
        writefileExtensionMemberFunctionTablePtr_->find(ext);

    if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
    {
        FatalErrorIn
        (
            "MeshedSurface::write"
            "(const fileName&, const MeshedSurface&)"
        )   << "Unknown file extension " << ext << nl << nl
            << "Valid types are :" << endl
            << writefileExtensionMemberFunctionTablePtr_->sortedToc()
            << exit(FatalError);
    }
    else
    {
        mfIter()(name, mesh);
    }
}
Exemple #13
0
void writePointSet
(
    const bool binary,
    const vtkMesh& vMesh,
    const pointSet& set,
    const fileName& fileName
)
{
    std::ofstream ostr(fileName.c_str());

    writeFuns::writeHeader
    (
        ostr,
        binary,
        set.name()
    );

    ostr<< "DATASET POLYDATA" << std::endl;

    //------------------------------------------------------------------
    //
    // Write topology
    //
    //------------------------------------------------------------------


    // Write points

    ostr<< "POINTS " << set.size() << " float" << std::endl;

    DynamicList<floatScalar> ptField(3*set.size());

    writeFuns::insert
    (
        UIndirectList<point>(vMesh.mesh().points(), set.toc())(),
        ptField
    );

    writeFuns::write(ostr, binary, ptField);


    //-----------------------------------------------------------------
    //
    // Write data
    //
    //-----------------------------------------------------------------

    // Write faceID

    ostr
        << "POINT_DATA " << set.size() << std::endl
        << "FIELD attributes 1" << std::endl;

    // Cell ids first
    ostr<< "pointID 1 " << set.size() << " int" << std::endl;

    labelList pointIDs(set.toc());

    writeFuns::write(ostr, binary, pointIDs);
}
void getRootCase(fileName& casePath)
{
    casePath.clean();

    if (casePath.empty() || casePath == ".")
    {
        // handle degenerate form and '.'
        casePath = cwd();
    }
    else if (casePath[0] != '/' && casePath.name() == "..")
    {
        // avoid relative cases ending in '..' - makes for very ugly names
        casePath = cwd()/casePath;
        casePath.clean();
    }
}
//- Rename to a corresponding backup file
//  If the backup file already exists, attempt with "01" .. "99" index
bool Foam::mvBak(const fileName& src, const std::string& ext)
{
    if (POSIX::debug)
    {
        Info<< "mvBak : " << src << " to extension " << ext << endl;
    }

    if (exists(src, false))
    {
        const int maxIndex = 99;
        char index[3];

        for (int n = 0; n <= maxIndex; n++)
        {
            fileName dstName(src + "." + ext);
            if (n)
            {
                sprintf(index, "%02d", n);
                dstName += index;
            }

            // avoid overwriting existing files, except for the last
            // possible index where we have no choice
            if (!exists(dstName, false) || n == maxIndex)
            {
                return rename(src.c_str(), dstName.c_str()) == 0;
            }

        }
    }

    // fall-through: nothing to do
    return false;
}
Exemple #16
0
void Foam::MeshedSurfaceProxy<Face>::write
(
    const fileName& name,
    const MeshedSurfaceProxy& surf
)
{
    if (debug)
    {
        Info<< "MeshedSurfaceProxy::write"
            "(const fileName&, const MeshedSurfaceProxy&) : "
            "writing to " << name
            << endl;
    }

    word ext = name.ext();

    typename writefileExtensionMemberFunctionTable::iterator mfIter =
        writefileExtensionMemberFunctionTablePtr_->find(ext);

    if (mfIter == writefileExtensionMemberFunctionTablePtr_->end())
    {
        FatalErrorIn
        (
            "MeshedSurfaceProxy::write(const fileName&)"
        )   << "Unknown file extension " << ext << nl << nl
            << "Valid types are :" << endl
            << writeTypes()
            << exit(FatalError);
    }

    mfIter()(name, surf);
}
Exemple #17
0
bool Foam::fileFormats::STARCDsurfaceFormat<Face>::read
(
    const fileName& filename
)
{
    const bool mustTriangulate = this->isTri();
    this->clear();

    fileName baseName = filename.lessExt();

    // STAR-CD index of points
    List<label> pointId;

    // read points from .vrt file
    readPoints
    (
        IFstream(baseName + ".vrt")(),
        this->storedPoints(),
        pointId
    );

    // Build inverse mapping (STAR-CD pointId -> index)
    Map<label> mapPointId(2*pointId.size());
    forAll(pointId, i)
    {
        mapPointId.insert(pointId[i], i);
    }
// Does the directory exist
bool isDir(const fileName& name)
{
    const DWORD attrs = ::GetFileAttributes(name.c_str());
    bool success = (attrs != INVALID_FILE_ATTRIBUTES) &&
                   (attrs & FILE_ATTRIBUTE_DIRECTORY);

    return success;
}
// Does the name exist in the filing system?
bool exists(const fileName& name, const bool checkGzip)
{
    const DWORD attrs = ::GetFileAttributes(name.c_str());
    const bool success = (attrs != INVALID_FILE_ATTRIBUTES) ||
                         (checkGzip && isGzFile(name));

    return success;
}
Exemple #20
0
void Foam::codedFunctionObject::unloadLibrary
(
    const fileName& libPath,
    const string& globalFuncName,
    const dictionary& contextDict
) const
{
    void* lib = 0;

    if (libPath.empty())
    {
        return;
    }

    dlLibraryTable& libs = const_cast<Time&>(time_).libs();

    lib = libs.findLibrary(libPath);

    if (!lib)
    {
        return;
    }

    // provision for manual execution of code before unloading
    if (dlSymFound(lib, globalFuncName))
    {
        loaderFunctionType function =
            reinterpret_cast<loaderFunctionType>
            (
                dlSym(lib, globalFuncName)
            );

        if (function)
        {
            (*function)(false);    // force unload
        }
        else
        {
            FatalIOErrorIn
            (
                "codedFunctionObject::unloadLibrary()",
                contextDict
            )   << "Failed looking up symbol " << globalFuncName << nl
                << "from " << libPath << exit(FatalIOError);
        }
    }

    if (!libs.close(libPath, false))
    {
        FatalIOErrorIn
        (
            "codedFunctionObject::"
            "updateLibrary()",
            contextDict
        )   << "Failed unloading library " << libPath
            << exit(FatalIOError);
    }
}
// Does the file exist
bool isFile(const fileName& name, const bool checkGzip)
{
    const DWORD attrs = ::GetFileAttributes(name.c_str());
    const bool success = ((attrs != INVALID_FILE_ATTRIBUTES) &&
                          !(attrs & FILE_ATTRIBUTE_DIRECTORY)) ||
                         (checkGzip && isGzFile(name));

    return success;
}
//- Converts old scope syntax to new syntax
word scope(const fileName& entryName)
{
    if (entryName.find(':') != string::npos)
    {
        wordList entryNames(entryName.components(':'));

        word entry(entryNames[0]);
        for (label i = 1; i < entryNames.size(); i++)
        {
            entry += word('.') + entryNames[i];
        }
        return entry;
    }
    else
    {
        return entryName;
    }
}
void printCleaning(fileName& pathName)
{
    Info<< "fileName = " << pathName << nl
        << "  path() = " << pathName.path() << nl
        << "  name() = " << pathName.name() << nl
        << "  joined = " << pathName.path()/pathName.name() << nl << nl;

    pathName.clean();

    Info<< "cleaned  = " << pathName << nl
        << "  path() = " << pathName.path() << nl
        << "  name() = " << pathName.name() << nl
        << "  joined = " << pathName.path()/pathName.name() << nl << nl;

    IOobject::writeDivider(Info);
}
Exemple #24
0
bool isoBubble::write(fileName file) const
{
    fileName fullPath = file.lessExt(); 
    mkDir(fullPath); 

    fullPath = fullPath + "/" + paddWithZeros(file); 

    bubblePtr_->write(fullPath); 

    return true;
}
Exemple #25
0
Foam::OFstreamAllocator::OFstreamAllocator
(
    const fileName& pathname,
    ios_base::openmode mode,
    IOstream::compressionType compression
)
:
    ofPtr_(NULL)
{
    if (pathname.empty())
    {
        if (OFstream::debug)
        {
            Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : "
                   "cannot open null file " << endl;
        }
    }

    if (compression == IOstream::COMPRESSED)
    {
        // get identically named uncompressed version out of the way
        if (isFile(pathname, false))
        {
            rm(pathname);
        }

        ofPtr_ = new ogzstream((pathname + ".gz").c_str());
    }
    else
    {
        // get identically named compressed version out of the way
        if (isFile(pathname + ".gz", false))
        {
            rm(pathname + ".gz");
        }

        ofPtr_ = new ofstream(pathname.c_str());
    }
}
int main(int argc, char *argv[])
{
    argList::noParallel();
    argList::validArgs.clear();
    argList::validArgs.append("input surface file");
    argList args(argc, argv);

    const fileName inFileName(args.args()[1]);
    if( inFileName.ext() == "fms" )
        FatalError << "trying to convert a fms file to itself"
            << exit(FatalError);

    fileName outFileName(inFileName.lessExt()+".fms");

    const triSurf surface(inFileName);

    surface.writeSurface(outFileName);

    Info << "End\n" << endl;

    return 0;
}
Exemple #27
0
Foam::TimePaths::TimePaths
(
    const fileName& rootPath,
    const fileName& caseName,
    const word& systemName,
    const word& constantName
)
:
    processorCase_(caseName.find("processor") != string::npos),
    rootPath_(rootPath),
    case_(caseName),
    system_(systemName),
    constant_(constantName)
{}
// Return the file type: FILE or DIRECTORY
fileName::Type type(const fileName& name)
{
    fileName::Type fileType = fileName::UNDEFINED;
    const DWORD attrs = ::GetFileAttributes(name.c_str());

    if (attrs != INVALID_FILE_ATTRIBUTES)
    {
        fileType = (attrs & FILE_ATTRIBUTE_DIRECTORY) ?
                   fileName::DIRECTORY :
                   fileName::FILE;
    }

    return fileType;
}
Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory)
{
    // Search user files: deprecated.  HJ, 11/Dec/2013

    // Search site files:
    fileName searchDir = getEnv("WM_PROJECT_INST_DIR");
    if (isDir(searchDir))
    {
        // Check for site file in $WM_PROJECT_INST_DIR/site/VERSION
        fileName fullName = searchDir/"site"/FOAMversion/name;
        if (isFile(fullName))
        {
            return fullName;
        }

        // Check for version-independent site file in $WM_PROJECT_INST_DIR/site
        fullName = searchDir/"site"/name;
        if (isFile(fullName))
        {
            return fullName;
        }
    }

    // Search installation files:
    searchDir = getEnv("WM_PROJECT_DIR");
    if (isDir(searchDir))
    {
        // Check for shipped FOAM file in $WM_PROJECT_DIR/etc
        fileName fullName = searchDir/"etc"/name;
        if (isFile(fullName))
        {
            return fullName;
        }
    }

    // Not found
    // abort if the file is mandatory, otherwise return null
    if (mandatory)
    {
        cerr<< "--> FOAM FATAL ERROR in Foam::findEtcFile() :"
               " could not find mandatory file\n    '"
            << name.c_str() << "'\n\n" << std::endl;
        ::exit(1);
    }

    // Return null-constructed fileName rather than fileName::null
    // to avoid cyclic dependencies in the construction of globals
    return fileName();
}
Exemple #30
0
void triSurf::writeSurface(const fileName& fName) const
{
    if( fName.ext() == "fms" || fName.ext() == "FMS" )
    {
        writeToFMS(fName);
    }
    else if( fName.ext() == "ftr" || fName.ext() == "FTR" )
    {
        writeToFTR(fName);
    }
    else
    {
        const pointField& pts = this->points();
        const LongList<labelledTri>& facets = this->facets();
        const geometricSurfacePatchList& patches = this->patches();

        List<labelledTri> newTrias(facets.size());
        forAll(facets, tI)
            newTrias[tI] = facets[tI];

        triSurface newSurf(newTrias, patches, pts);
        newSurf.write(fName);
    }
}