File: Utils.h

package info (click to toggle)
kissplice 2.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,752 kB
  • sloc: cpp: 8,783; python: 1,618; perl: 389; sh: 72; makefile: 18
file content (68 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download
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
63
64
65
66
67
68
#ifndef UTILS_H
#define UTILS_H

#include <string>
#include <memory>
#include <vector>

#define MAX 1024
#define NUMBEROFFILES 128
using namespace std;

int edit_distance(const void *s1, size_t l1, const void *s2, size_t l2, size_t nmemb, int (*comp)(const void*, const void*));
int hamming_distance(const void *s1, size_t l1, const void *s2, size_t l2, size_t nmemb, int (*comp)(const void*, const void*));
int comp(const void *a, const void *b); 

char complement(char b);
string reverse_complement(string seq);
char reverse_dir(char dir);

FILE* open_file( char* filename );
string toLowerContext( const string &sequence, const int contextFirst, const int contextLast);

// Wrap std::FILE* in a std::unique_ptr for automatic close on destruction.
struct File {
    struct Deleter {
        void operator()(std::FILE* ptr) { std::fclose(ptr); }
    };
    std::unique_ptr<std::FILE, Deleter> file_ptr;

    // Access raw file pointer
    std::FILE* get_ptr() const { return file_ptr.get(); }
    explicit operator std::FILE*() const { return file_ptr.get(); }

    // Disable default constructor to force use of File::open_*() methods to open a file.
    File() = delete;

    // Open a file at path. Stops program and print error message on failure.
    static File open_path(const char* path, const char* mode);
    static File open_path(const std::string & path, const char* mode) {
        return File::open_path(path.c_str(), mode);
    }

    // Format a path using sprintf then open
    template<typename... Args> static File open_path_sprintf(const char* path_format, const char* mode, Args ...args) {
        char path[1024];
        int written = std::snprintf(path, 1024, path_format, args...);
        if(written < 1024) {
            return File::open_path(path, mode);
        } else {
            auto length = static_cast<std::size_t>(std::snprintf(nullptr, 0, path_format, args...));
            auto buffer = std::vector<char>(length + 1);
            std::snprintf(buffer.data(), buffer.size(), path_format, args...);
            return File::open_path(buffer.data(), mode);
        }
    }

    // Just forward to fprintf
    template<typename... Args> int fprintf(const char* format, Args ...args) const {
        return std::fprintf(this->get_ptr(), format, args...);
    }

    // Force close the file early
    void close() {
        this->file_ptr.reset();
    }
};

#endif