~aleteoryx/muditaos

ref: 9c9e921ab488e73ec57ece6ff93a07624b252162 muditaos/module-services/service-fileindexer/notesIndexer.cpp -rw-r--r-- 1.3 KiB
9c9e921a — Maciej Gibowicz [EGD-4693] PowerManagement: CPU load measurement (#1156) 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log/log.hpp>
#include "notesIndexer.hpp"
#include <vfs.hpp>
#include <cwctype>
#include <utf8/UTF8.hpp>

namespace service::detail
{

    notesIndexer::notesIndexer(std::string_view path)
    {
        auto file = vfs.fopen(std::string(path).c_str(), "r");
        if (!file) {
            LOG_INFO("Unable to open file [%s] errr: [%s] . Ignore...",
                     std::string(path).c_str(),
                     vfs.lastErrnoToStr().c_str());
            return;
        }
        if (!vfs.eof(file)) {
            auto line = vfs.getline(file, 4096);
            mLinesCount++;
            updateLineStats(line);
        }
        vfs.fclose(file);
    }
    auto notesIndexer::updateLineStats(std::string_view _line) noexcept -> void
    {
        UTF8 line(std::string(_line).c_str());
        bool last_space{};
        for (std::size_t idx = 0; idx < line.length(); ++idx) {
            const auto wchar = line[idx];
            const auto space = std::iswspace(wchar);
            if (space && !last_space)
                mWordCount++;
            if (std::iswprint(wchar) && !space)
                mCharCount++;
            last_space = space;
        }
    }
} // namespace service::detail