File: BlasrFileUtils.cpp

package info (click to toggle)
pbseqlib 5.3.5%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,020 kB
  • sloc: cpp: 77,250; python: 331; sh: 103; makefile: 41
file content (62 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <alignment/utils/FileUtils.hpp>

bool FileExists(std::string &fileName)
{
    FILE *fp = fopen(fileName.c_str(), "r");
    if (fp) {
        // exists
        fclose(fp);
        return true;
    } else {
        return false;
    }
}

void CriticalOpenRead(std::string &fileName, std::ifstream &file, std::ios::openmode mode)
{
    file.open(fileName.c_str(), mode | std::ios::in);
    if (!file.good()) {
        std::cerr << "Could not open file:" << fileName << std::endl;
        std::exit(EXIT_FAILURE);
    }
}

int OpenRead(std::string &fileName, std::ifstream &file, std::ios::openmode mode)
{
    file.open(fileName.c_str(), mode | std::ios::in);
    return file.good();
}

void CriticalOpenWrite(std::string &fileName, std::ofstream &file, std::ios::openmode mode)
{
    file.open(fileName.c_str(), mode | std::ios::out);
    if (!file.good()) {
        std::cerr << "Could not open file: " << fileName << std::endl;
        std::exit(EXIT_FAILURE);
    }
}

int OpenWrite(std::string &fileName, std::ofstream &file, std::ios::openmode mode)
{
    file.open(fileName.c_str(), mode | std::ios::out);
    return file.good();
}

int CountLinesInFile(std::string fileName)
{
    char *filePtr;
    long fileSize;
    int fileDes;
    fileDes = open(fileName.c_str(), O_RDONLY);
    fileSize = lseek(fileDes, 0, SEEK_END);
    lseek(fileDes, 0, SEEK_SET);
    filePtr = (char *)mmap(0, fileSize, PROT_READ, MAP_PRIVATE, fileDes, 0);
    long pos;
    int numLines = 0;
    for (pos = 0; pos < fileSize; pos++, filePtr++) {
        if (*filePtr == '\n') {
            numLines++;
        }
    }
    return numLines;
}