pyca/cryptography ================= .. image:: https://img.shields.io/pypi/v/cryptography.svg :target: https://pypi.python.org/pypi/cryptography/ :alt: Latest Version .. image:: https://readthedocs.org/projects/cryptography/badge/?version=latest :target: https://cryptography.io :alt: Latest Docs .. image:: https://travis-ci.org/pyca/cryptography.svg?branch=master :target: https://travis-ci.org/pyca/cryptography .. image:: https://codecov.io/github/pyca/cryptography/coverage.svg?branch=master :target: https://codecov.io/github/pyca/cryptography?branch=master ``cryptography`` is a package which provides cryptographic recipes and primitives to Python developers. Our goal is for it to be your "cryptographic standard library". It supports Python 2.6-2.7, Python 3.4+, and PyPy 5.3+. ``cryptography`` includes both high level recipes and low level interfaces to common cryptographic algorithms such as symmetric ciphers, message digests, and key derivation functions. For example, to encrypt something with ``cryptography``'s high level symmetric encryption recipe: .. code-block:: pycon >>> from cryptography.fernet import Fernet >>> # Put this somewhere safe! >>> key = Fernet.generate_key() >>> f = Fernet(key) >>> token = f.encrypt(b"A really secret message. Not for prying eyes.") >>> token '...' >>> f.decrypt(token) 'A really secret message. Not for prying eyes.' You can find more information in the `documentation`_. You can install ``cryptography`` with: .. code-block:: console $ pip install cryptography For full details see `the installation documentation`_. Discussion ~~~~~~~~~~ If you run into bugs, you can file them in our `issue tracker`_. We maintain a `cryptography-dev`_ mailing list for development discussion. You can also join ``#cryptography-dev`` on Freenode to ask questions or get involved. .. _`documentation`: https://cryptography.io/ .. _`the installation documentation`: https://cryptography.io/en/latest/installation/ .. _`issue tracker`: https://github.com/pyca/cryptography/issues .. _`cryptography-dev`: https://mail.python.org/mailman/listinfo/cryptography-dev 4ce2f42f5d3d9fce9'/>
path: root/3rdparty/python-console/ColumnFormatter.cpp
blob: 7e9d6712070cce94f5d70e4ea8f36efbcab8899b (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
130
131
132
133
134
135
136
137
138
139
140
141
/**
Copyright (c) 2014 Alex Tsui

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ColumnFormatter.h"
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstdlib>

bool ColumnFormatter::load(const std::string& fn)
{
    items.clear();
    std::ifstream ifs(fn.c_str());
    if (!ifs.is_open())
        return false;
    std::string str;
    while (!ifs.eof())
    {
        std::getline( ifs, str );
        if (!ifs.eof())
            items.push_back(str);
    }
    return true;
}

int ColumnFormatter::solve(int width)
{
    bool fits = true;
    int i = 1;
    while (fits)
    {
        ++i;
        std::vector<size_t> widths = divideItems(i);
        size_t columnWidth = width / i;
        for (size_t j = 0; j < widths.size(); ++j)
        {
            fits &= (widths[j] < columnWidth);
        }
        if (!fits)
        {
            --i;
        }
    }
    return i;
}

std::vector<size_t> ColumnFormatter::divideItems(int numColumns)
{
    columns.clear();
    for (int i = 0; i < numColumns; ++i)
    columns.push_back(std::list<std::string>());
    for (size_t i = 0; i < items.size(); ++i)
    {
        columns[i % numColumns].push_back(items[i]);
    }

    // count the fattest item in each column
    std::vector<size_t> res(numColumns);
    for (int i = 0; i < numColumns; ++i)
    {
        for (std::list<std::string>::const_iterator it =
            columns[i].begin(); it != columns[i].end(); ++it)
        {
            if (res[i] < it->size())
                res[i] = it->size();
        }
    }

    return res;
}

void ColumnFormatter::format(int width)
{
    m_formattedOutput.clear();
    int cols = solve(width);
    std::vector<int> colWidths(cols, width / cols);
    int rem = width % cols;
    for (int i = 0; i < rem; ++i)
    {
        colWidths[i]++;
    }
    divideItems(cols);
    std::vector< std::list<std::string>::const_iterator > its;
    std::vector< std::list<std::string>::const_iterator > it_ends;
    for (size_t i = 0; i < columns.size(); ++i)
    {
        its.push_back(columns[i].begin());
        it_ends.push_back(columns[i].end());
    }
    bool done = false;
    while (!done)
    {
        std::stringstream row_ss;
        for (size_t i = 0; i < columns.size(); ++i)
        {
            std::stringstream item_ss;
            std::string item;
            if (its[i] != it_ends[i])
            {
                item = *its[i];
                ++its[i];
            }
            item_ss << std::left << std::setw(colWidths[i]) << item;
            row_ss << item_ss.str();
        }
        m_formattedOutput.push_back(row_ss.str());

        done = true;
        for (size_t i = 0; i < columns.size(); ++i)
        {
            done &= (its[i] == it_ends[i]);
        }
    }
}

const std::list<std::string>& ColumnFormatter::formattedOutput() const
{
    return m_formattedOutput;
}