GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/fs/virtfs/tools.cpp Lines: 33 56 58.9 %
Date: 2017-11-29 Branches: 23 76 30.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "fs/virtfs/tools.h"
22
23
#include "logger.h"
24
25
#include "fs/paths.h"
26
27
#include "fs/virtfs/fs.h"
28
#include "fs/virtfs/list.h"
29
30
#include "utils/foreach.h"
31
#include "utils/stringutils.h"
32
33
#include <algorithm>
34
#include <sstream>
35
36
#include "debug.h"
37
38
namespace VirtFs
39
{
40
    void searchAndAddArchives(const std::string &restrict path,
41
                              const std::string &restrict ext,
42
                              const Append append)
43
    {
44
        List *const list = VirtFs::enumerateFiles(path);
45
        FOR_EACH (StringVectCIter, i, list->names)
46
        {
47
            const std::string str = *i;
48
            const size_t len = str.size();
49
50
            if (len > ext.length() &&
51
                ext == str.substr(len - ext.length()))
52
            {
53
                const std::string file = path + str;
54
                const std::string realPath = VirtFs::getRealDir(file);
55
                VirtFs::mountZip(pathJoin(realPath, file), append);
56
            }
57
        }
58
        VirtFs::freeList(list);
59
    }
60
61
    void searchAndRemoveArchives(const std::string &restrict path,
62
                                 const std::string &restrict ext)
63
    {
64
        List *const list = VirtFs::enumerateFiles(path);
65
        FOR_EACH (StringVectCIter, i, list->names)
66
        {
67
            const std::string str = *i;
68
            const size_t len = str.size();
69
            if (len > ext.length() &&
70
                ext == str.substr(len - ext.length()))
71
            {
72
                const std::string file = path + str;
73
                const std::string realPath = VirtFs::getRealDir(file);
74
                VirtFs::unmountZip(pathJoin(realPath, file));
75
            }
76
        }
77
        VirtFs::freeList(list);
78
    }
79
80
168
    void getFilesInDir(const std::string &dir,
81
                       StringVect &list,
82
                       const std::string &ext)
83
    {
84
168
        const std::string &path = dir;
85
336
        StringVect tempList;
86
336
        VirtFs::getFilesWithDir(path, tempList);
87
980
        FOR_EACH (StringVectCIter, it, tempList)
88
        {
89
140
            const std::string &str = *it;
90

140
            if (findLast(str, ext))
91
62
                list.push_back(str);
92
        }
93
336
        std::sort(list.begin(), list.end());
94
168
    }
95
96
16
    std::string getPath(const std::string &file)
97
    {
98
        // get the real path to the file
99
48
        const std::string tmp = VirtFs::getRealDir(file);
100
16
        std::string path;
101
102
        // if the file is not in the search path, then its empty
103
16
        if (!tmp.empty())
104
        {
105
30
            path = pathJoin(tmp, file);
106
#if defined __native_client__
107
            std::string dataZip = "/http/data.zip/";
108
            if (path.substr(0, dataZip.length()) == dataZip)
109
                path = path.replace(0, dataZip.length(), "/http/data/");
110
#endif  // defined __native_client__
111
        }
112
        else
113
        {
114
            // if not found in search path return the default path
115

18
            path = pathJoin(getPackageDir(), file);
116
        }
117
118
16
        return path;
119
    }
120
121
6
    std::string loadTextFileString(const std::string &fileName)
122
    {
123
        int contentsLength;
124
12
        const char *fileContents = VirtFs::loadFile(fileName, contentsLength);
125
126
6
        if (fileContents == nullptr)
127
        {
128
            logger->log("Couldn't load text file: %s", fileName.c_str());
129
            return std::string();
130
        }
131
12
        const std::string str = std::string(fileContents, contentsLength);
132
6
        delete [] fileContents;
133
6
        return str;
134
    }
135
136
102
    bool loadTextFile(const std::string &fileName,
137
                      StringVect &lines)
138
    {
139
        int contentsLength;
140
204
        const char *fileContents = VirtFs::loadFile(fileName, contentsLength);
141
142
102
        if (fileContents == nullptr)
143
        {
144
8
            logger->log("Couldn't load text file: %s", fileName.c_str());
145
            return false;
146
        }
147
148
588
        std::istringstream iss(std::string(fileContents, contentsLength));
149
98
        std::string line;
150
151

1540
        while (getline(iss, line))
152
672
            lines.push_back(line);
153
154
98
        delete [] fileContents;
155
98
        return true;
156
    }
157
}  // namespace VirtFs