File: util.cc

package info (click to toggle)
transtermhp 2.09-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 536 kB
  • sloc: cpp: 4,665; python: 294; makefile: 203; sh: 115
file content (65 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (5)
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
/* This file is part of TransTerm v2.0 BETA and is covered by the GNU GPL
 * License version 2.0. See file LICENSE.txt for more details. */

#include <sstream>
#include "util.h"


// print a status percent
void
print_status(ostream & out, unsigned long cur, unsigned long max)
{
    ostringstream oss;
    oss << (int)(100 * ((float)cur) / max) << "%"; 
    out << oss.str();
    for(unsigned i = 0; i < oss.str().length(); i++) out << "\b";
}

// split a string into fields separated by a single character
void
split(const string & s, char sep, vector<string> & out)
{
    unsigned i = 0;
    out.clear();
    out.push_back("");
    for(unsigned j = 0; j < s.length(); j++)
    {
        if(s[j] == sep)
        {
            i++;
            out.push_back("");
        }
        else
        {
            out[i] += s[j];
        }
    }
}  

// remove whitespace at the front of s
string
trim_front(const string & s)
{
    unsigned i;
    for(i = 0; i<s.length(); i++)
    {
        if(!isspace(s[i])) break;
    }
    return s.substr(i);
}

 
// center a string 
string
center(const string & s, int fieldsize)
{
    string news = "";
    int ex = fieldsize - s.length();
    if(ex <= 0) return s;
    int pad = ex/2;
    for(int i = 0; i < pad; i++) news += ' ';
    news += s;
    for(int i = 0; i < pad; i++) news += ' ';
    if(ex % 2 != 0) news += ' ';
    return news;
}