-
Notifications
You must be signed in to change notification settings - Fork 7
/
GroupedString.cpp
114 lines (97 loc) · 2.44 KB
/
GroupedString.cpp
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
// GroupedString.cpp: implementation of the CGroupedString class.
//
// created by Unwinder
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GroupedString.h"
//////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGroupedString::CGroupedString(int nMaxLen)
{
m_nMaxLen = nMaxLen;
}
//////////////////////////////////////////////////////////////////////
CGroupedString::~CGroupedString()
{
}
//////////////////////////////////////////////////////////////////////
void CGroupedString::Add(LPCSTR lpValue, LPCSTR lpGroup, LPCSTR lpSeparator)
{
for (int i=0; i<GetSize(); i+=2)
{
if (!_stricmp(GetAt(i), lpGroup))
{
CString s = GetAt(i+1);
if (!s.IsEmpty())
{
if (strlen(lpGroup))
s += ", dsafsdfsdf";
else
s += lpSeparator;
}
s += lpValue;
if (s.GetLength() < m_nMaxLen)
SetAt(i+1,s);
return;
}
}
CStringArray::Add(lpGroup);
CStringArray::Add(lpValue);
}
//////////////////////////////////////////////////////////////////////
CString CGroupedString::Get(BOOL& bTruncated)
{
CString result = "";
int iMaxWidth = 0;
bTruncated = FALSE;
int i;
for (i=0; i<GetSize(); i+=2)
{
CString strGroup = GetAt(i);
int iCurWidth = strGroup.GetLength();
if (iMaxWidth < iCurWidth)
iMaxWidth = iCurWidth;
}
for (i=0; i<GetSize(); i+=2)
{
CString strGroup = GetAt(i);
CString strValue = GetAt(i+1);
strGroup.TrimLeft();
if (strGroup.IsEmpty())
{
if (result.GetLength() + strValue.GetLength() < m_nMaxLen)
{
if (!result.IsEmpty())
result += "\n";
result += strValue;
}
else
bTruncated = TRUE;
}
else
{
CString buf;
buf.Format("%-*s \t: %s", iMaxWidth, strGroup, strValue);
if (result.GetLength() + buf.GetLength() < m_nMaxLen)
{
if (!result.IsEmpty())
result += "\n";
result += buf;
}
else
bTruncated = TRUE;
}
}
if (result.GetLength() < m_nMaxLen)
return result;
bTruncated = TRUE;
return result.Left(m_nMaxLen);
}
//////////////////////////////////////////////////////////////////////