Skip to content

Commit 6dc186a

Browse files
committed
added some string functions to util class
1 parent 9897804 commit 6dc186a

File tree

7 files changed

+226
-23
lines changed

7 files changed

+226
-23
lines changed

LFSDesktop/LFSDesktop/src/main.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ int main(int argc, char **argv)
133133
std::string iconpath;
134134
LFSTK_buttonClass *button;
135135
int key=666;
136+
Atom xa;//TODO//
137+
Atom xa_prop[3];
136138

137139
apc=new LFSTK_applicationClass();
138140

@@ -155,23 +157,19 @@ int main(int argc, char **argv)
155157
wc=apc->mainWindow;
156158
wc->passEventToRoot=true;
157159

158-
Atom xa;//TODO//
159-
Atom xa_prop[3];
160-
161-
xa=XInternAtom(apc->display,"_NET_WM_ALLOWED_ACTIONS",False);
162-
xa_prop[0]=XInternAtom(apc->display,"_NET_WM_STATE_STICKY",False);
163-
xa_prop[1]=XInternAtom(apc->display,"_NET_WM_STATE_BELOW",False);
160+
xa=XInternAtom(apc->display,"_NET_WM_ALLOWED_ACTIONS",False);
161+
xa_prop[0]=XInternAtom(apc->display,"_NET_WM_STATE_STICKY",False);
162+
xa_prop[1]=XInternAtom(apc->display,"_NET_WM_STATE_BELOW",False);
164163

165-
if(xa!=None)
166-
XChangeProperty(apc->display,wc->window,xa,XA_ATOM,32,PropModeReplace,(unsigned char *)&xa_prop,2);
164+
if(xa!=None)
165+
XChangeProperty(apc->display,wc->window,xa,XA_ATOM,32,PropModeReplace,(unsigned char *)&xa_prop,2);
167166

168167
xa=XInternAtom(apc->display,"_NET_WM_STATE",False);
169168
xa_prop[0]=XInternAtom(apc->display,"_NET_WM_STATE_BELOW",False);
170169
if(xa!=None)
171170
XChangeProperty(apc->display,wc->window,xa,XA_ATOM,32,PropModeReplace,(unsigned char *)&xa_prop,1);
172171
XLowerWindow(apc->display,wc->window);
173172

174-
175173
//TODO//debug to go
176174
#ifdef _ENABLEDEBUG_
177175
button=new LFSTK_buttonClass(wc,"quit",0,0,GADGETWIDTH,GADGETHITE);//TODO//

LFSDesktop/LFSDesktop/src/prefs.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,19 @@ void saveCacheFile(const char *cachefilepath,desktopItemStruct *cfd)
9090

9191
void setDeskNamesProp(void)
9292
{
93-
std::vector<std::string> tokenstrings;
9493
const char *x1;
9594
std::string x="";
96-
int totallen=0;
9795
std::string names=prefs.LFSTK_getString("desknames");
9896

99-
tokenstrings=LFSTK_UtilityClass::LFSTK_strTok(names,",");
100-
for(unsigned j=0;j<tokenstrings.size();j++)
101-
{
102-
totallen+=tokenstrings.at(j).length()+1;
103-
x+=tokenstrings.at(j)+'\0';
104-
}
105-
97+
x=LFSTK_UtilityClass::LFSTK_strReplaceAllChar(names,",",std::string("")+='\0');
98+
x+='\0';
10699
x1=x.c_str();
107100
XChangeProperty(apc->display,apc->rootWindow,XInternAtom(apc->display,"_NET_DESKTOP_NAMES",false),
108101
XInternAtom(apc->display,"UTF8_STRING",false),
109102
8,
110103
PropModeReplace,
111104
(const unsigned char*)*&x1
112-
,totallen
105+
,names.length()+1
113106
);
114107

115108
XSync(apc->display,false);

LFSToolKit/ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
0.6.1
2+
Added LFSTK_strReplaceAllStr to util class.
3+
Added LFSTK_strReplaceAllChar to util class.
24
Mose code cleaning...
35
Set default whitespace for LFSTK_strStrip.
46
Renoved some leaky glib code.

LFSToolKit/LFSToolKit/lfstk/LFSTKUtilityClass.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ std::string LFSTK_UtilityClass::LFSTK_strStr(std::string haystack,std::string ne
8181
return("");
8282
}
8383

84+
/**
85+
* Trim whitespace from front and back of string.
86+
* \param std::string str hastack.
87+
* \param std::string whitespace Default whitespace="\t \r\n".
88+
* \return std::string.
89+
*/
8490
std::string LFSTK_UtilityClass::LFSTK_strStrip(std::string haystack,std::string whitespace)
8591
{
8692
std::string::size_type foundfront;
@@ -90,3 +96,67 @@ std::string LFSTK_UtilityClass::LFSTK_strStrip(std::string haystack,std::string
9096
foundback=haystack.find_last_not_of(whitespace);
9197
return(haystack.substr(foundfront,foundback-foundfront+1));
9298
}
99+
100+
/**
101+
* Replace/erase all from string.
102+
* \param std::string str hastack.
103+
* \param std::string needle.
104+
* \param std::string replacement needle.
105+
* \param bool erase.
106+
* \note Default is to replace.
107+
* \note replace/erase whole needle.
108+
* \return std::string.
109+
*/
110+
std::string LFSTK_UtilityClass::LFSTK_strReplaceAllStr(std::string haystack,std::string needle,std::string newneedle,bool erase)
111+
{
112+
std::string::size_type found;
113+
std::string localhaystack=haystack;
114+
bool flag=false;
115+
116+
do
117+
{
118+
flag=false;
119+
found=localhaystack.find(needle);
120+
if(found!=std::string::npos)
121+
{
122+
if(erase==true)
123+
localhaystack.erase(found,needle.length());
124+
else
125+
localhaystack.replace(found,needle.length(),newneedle);
126+
flag=true;
127+
}
128+
}while(flag==true);
129+
return(localhaystack);
130+
}
131+
132+
/**
133+
* Replace/erase all from string.
134+
* \param std::string str hastack.
135+
* \param std::string needle.
136+
* \param std::string replacement needle.
137+
* \param bool erase.
138+
* \note Default is to replace.
139+
* \note replace/erase any char in needle.
140+
* \return std::string.
141+
*/
142+
std::string LFSTK_UtilityClass::LFSTK_strReplaceAllChar(std::string haystack,std::string needle,std::string newneedle,bool erase)
143+
{
144+
std::string::size_type found;
145+
std::string localhaystack=haystack;
146+
bool flag=false;
147+
148+
do
149+
{
150+
flag=false;
151+
found=localhaystack.find_first_of(needle);
152+
if(found!=std::string::npos)
153+
{
154+
if(erase==true)
155+
localhaystack.erase(found,1);
156+
else
157+
localhaystack.replace(found,1,newneedle);
158+
flag=true;
159+
}
160+
}while(flag==true);
161+
return(localhaystack);
162+
}

LFSToolKit/LFSToolKit/lfstk/LFSTKUtilityClass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class LFSTK_UtilityClass
3030
static std::vector<std::string> LFSTK_strTok(std::string str,std::string delimiter);
3131
static std::string LFSTK_strStr(std::string haystack,std::string needle,bool caseinsensitive=false);
3232
static std::string LFSTK_strStrip(std::string haystack,std::string whitespace="\t \r\n");
33+
static std::string LFSTK_strReplaceAllStr(std::string haystack,std::string needle,std::string newneedle,bool erase=false);
34+
static std::string LFSTK_strReplaceAllChar(std::string haystack,std::string needle,std::string newneedle,bool erase=false);
3335
};
3436

3537
#endif

LFSToolKit/devtests/replaceall.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#if 0
2+
3+
# (c)keithhedger Tue 9 Jan 13:48:05 GMT 2024 [email protected]
4+
5+
if [[ $USEVALGRIND -eq 1 ]];then
6+
VALGRIND="valgrind --leak-check=full "
7+
fi
8+
9+
APPNAME=$(basename $0 .cpp)
10+
11+
g++ "$0" -O0 -ggdb -I../LFSToolKit -L../LFSToolKit/app/.libs $(pkg-config --cflags --libs x11 xft cairo ) -llfstoolkit -lImlib2 -o $APPNAME||exit 1
12+
LD_LIBRARY_PATH=../LFSToolKit/app/.libs $VALGRIND ./$APPNAME "$@"
13+
14+
15+
retval=$?
16+
echo "Exit code $retval"
17+
rm $APPNAME
18+
exit $retval
19+
#endif
20+
21+
#include "lfstk/LFSTKGlobals.h"
22+
#include <memory>
23+
24+
std::string replaceAllStr(std::string haystack,std::string needle,std::string newneedle,bool erase=true)
25+
{
26+
std::string::size_type found;
27+
std::string localhaystack=haystack;
28+
bool flag=false;
29+
30+
do
31+
{
32+
flag=false;
33+
found=localhaystack.find(needle);
34+
if(found!=std::string::npos)
35+
{
36+
if(erase==true)
37+
localhaystack.erase(found,needle.length());
38+
else
39+
localhaystack.replace(found,needle.length(),newneedle);
40+
flag=true;
41+
}
42+
}while(flag==true);
43+
return(localhaystack);
44+
}
45+
46+
std::string replaceAllChar(std::string haystack,std::string needle,std::string newneedle,bool erase=true)
47+
{
48+
std::string::size_type found;
49+
std::string localhaystack=haystack;
50+
bool flag=false;
51+
52+
do
53+
{
54+
flag=false;
55+
found=localhaystack.find_first_of(needle);
56+
if(found!=std::string::npos)
57+
{
58+
if(erase==true)
59+
localhaystack.erase(found,1);
60+
else
61+
localhaystack.replace(found,1,newneedle);
62+
flag=true;
63+
}
64+
}while(flag==true);
65+
return(localhaystack);
66+
}
67+
68+
int main(int argc, char **argv)
69+
{
70+
std::string x="";
71+
LFSTK_applicationClass *apc=new LFSTK_applicationClass();
72+
std::string haystack="w1,w2,wXXXX3,.w4,XXXX,w.5,wX6";
73+
std::string needle="XXXX";
74+
std::string newneedle="++";
75+
//
76+
////replace ,>0
77+
// x=replaceAll(haystack,needle,std::string("")+='\0',false);
78+
// std::cout<<x<<std::endl;
79+
// fprintf(stderr,"x=%s\n",x.c_str());
80+
// for(int j=0;j<x.length();j++)
81+
// fprintf(stderr,"0x%x\n",x.at(j));
82+
//
83+
//replace str
84+
x=replaceAllStr(haystack,needle,newneedle,false);
85+
std::cout<<"Replace "<<x<<std::endl;
86+
fprintf(stderr,"x=%s\n",x.c_str());
87+
for(int j=0;j<x.length();j++)
88+
fprintf(stderr,"0x%x\n",x.at(j));
89+
90+
//replace char
91+
needle=".,";
92+
x=replaceAllChar(haystack,needle,newneedle,false);
93+
std::cout<<"Replace Char "<<x<<std::endl;
94+
fprintf(stderr,"x=%s\n",x.c_str());
95+
for(int j=0;j<x.length();j++)
96+
fprintf(stderr,"0x%x\n",x.at(j));
97+
98+
//
99+
//erase
100+
x=replaceAllChar(haystack,needle,newneedle,true);
101+
std::cout<<x<<std::endl;
102+
fprintf(stderr,"x=%s\n",x.c_str());
103+
for(int j=0;j<x.length();j++)
104+
fprintf(stderr,"0x%x\n",x.at(j));
105+
106+
//libtest
107+
std::cout<<"Library test"<<std::endl;
108+
x=LFSTK_UtilityClass::LFSTK_strReplaceAllChar(haystack,needle,"=!=");
109+
std::cout<<x<<std::endl;
110+
fprintf(stderr,"x=%s\n",x.c_str());
111+
for(int j=0;j<x.length();j++)
112+
fprintf(stderr,"0x%x\n",x.at(j));
113+
114+
delete apc;
115+
cairo_debug_reset_static_data();
116+
return(0);
117+
}

LFSToolKit/devtests/setdesknames.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
# (c)keithhedger Tue 9 Jan 13:48:05 GMT 2024 [email protected]
44

5-
#USEVALGRIND="valgrind --leak-check=full"
5+
if [[ $USEVALGRIND -eq 1 ]];then
6+
VALGRIND="valgrind --leak-check=full "
7+
fi
68

79
APPNAME=$(basename $0 .cpp)
810

9-
g++ "$0" -O0 -ggdb $(pkg-config --cflags --libs x11 xft cairo lfstk ) -llfstoolkit -lImlib2 -o $APPNAME ||exit 1
10-
$USEVALGRIND ./$APPNAME "$@"
11+
g++ "$0" -O0 -ggdb -I../LFSToolKit -L../LFSToolKit/app/.libs $(pkg-config --cflags --libs x11 xft cairo ) -llfstoolkit -lImlib2 -o $APPNAME||exit 1
12+
LD_LIBRARY_PATH=../LFSToolKit/app/.libs $VALGRIND ./$APPNAME "$@"
1113

1214
retval=$?
1315
echo "Exit code $retval"
@@ -20,6 +22,23 @@ exit $retval
2022

2123
int main(int argc, char **argv)
2224
{
25+
26+
LFSTK_applicationClass *apc=new LFSTK_applicationClass();
27+
std::string names="w1,Desk 2,w3,w4,NoDesk,w6";
28+
std::string x;
29+
const char *x1;
30+
31+
x=LFSTK_UtilityClass::LFSTK_strReplaceAll(names,",",std::string("")+='\0');
32+
x1=x.c_str();
33+
XChangeProperty(apc->display,apc->rootWindow,XInternAtom(apc->display,"_NET_DESKTOP_NAMES",false),
34+
XInternAtom(apc->display,"UTF8_STRING",false),
35+
8,
36+
PropModeReplace,
37+
(const unsigned char*)*&x1
38+
,names.length()
39+
);
40+
#if 0
41+
2342
std::vector<std::string> tokenstrings;
2443
const char *x1;
2544
std::string x="";
@@ -43,8 +62,10 @@ int main(int argc, char **argv)
4362
,totallen
4463
);
4564

65+
#endif
4666
XSync(apc->display,false);
47-
//system("xprop -root _NET_DESKTOP_NAMES");
67+
system("xprop -root _NET_DESKTOP_NAMES");
68+
4869
delete apc;
4970
cairo_debug_reset_static_data();
5071
return(0);

0 commit comments

Comments
 (0)