summaryrefslogtreecommitdiffstats
path: root/scilab/modules/ast/src/cpp/types/library.cpp
blob: 8b95a563baf81f34b2c695a5d325f1b6bf7c968a (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include <sstream>
#include "macrofile.hxx"
#include "library.hxx"
#include "configvariable.hxx"
#include "scilabWrite.hxx"

extern "C"
{
#include <wchar.h>
#include "os_string.h"
}

namespace types
{
Library::Library(const std::string& _path) :
    m_path(_path)
{
#ifndef NDEBUG
    Inspector::addItem(this);
#endif
}

Library::~Library()
{
    //delete all macrofile*
    for (auto macro : m_macros)
    {
        MacroFile* pMacro = macro.second;
        pMacro->DecreaseRef();
        if (pMacro->isDeletable())
        {
            delete pMacro;
        }
    }

    m_macros.clear();
#ifndef NDEBUG
    Inspector::removeItem(this);
#endif
}

bool Library::toString(std::ostringstream& ostr)
{
    char output[1024] = {0};
    os_sprintf(output, _("Functions files location : %s.\n"), m_path.c_str());

    ostr << output << std::endl;

    size_t iLineLen = (size_t)ConfigVariable::getConsoleWidth();

    size_t iCurrentLen = 0;
    for (auto macro : m_macros)
    {
        if (iCurrentLen + macro.first.length() + 2 > iLineLen)
        {
            ostr << std::endl;
            iCurrentLen = 0;
        }
        ostr << macro.first << "  ";
        iCurrentLen += macro.first.length() + 2;
    }

    ostr << std::endl;

    return true;
}

Library* Library::clone()
{
    IncreaseRef();
    return this;
}

bool Library::extract(const std::string & name, InternalType *& out)
{
    out = get(name);
    if (out == NULL)
    {
        char szError[bsiz];
        os_sprintf(szError, _("Unknown field : %ls.\n"), name.c_str());
        throw std::string(szError);
    }

    return true;
}

void Library::add(const std::string& _stName, MacroFile* _macro)
{
    _macro->IncreaseRef();
    m_macros[_stName] = _macro;
}

MacroFile* Library::get(const std::string& _stName)
{
    MacroMap::iterator it = m_macros.find(_stName);
    if (it != m_macros.end())
    {
        return it->second;
    }
    return NULL;
}

int Library::getMacrosName(std::list<std::string>& lst)
{
    for (auto macro : m_macros)
    {
        lst.push_back(macro.first);
    }

    return static_cast<int>(lst.size());
}

std::string Library::getPath()
{
    return m_path;
}
}