|
-
Sep 21st, 2002, 10:29 AM
#1
Thread Starter
Hyperactive Member
Help translating from VB
Can someone help me translate this from VB to C++?
VB Code:
' Visual Basic version
Public Function LineTrim(Expression As String) As String
'Remove trailing CRs and LFs
Dim sReturn As String
Dim sCheck As String
sReturn = Expression
sCheck = Right(sReturn, 1)
Do While (sCheck = Chr(10)) Or (sCheck = Chr(13))
sReturn = Left(sReturn, Len(sReturn) - 1)
sCheck = Right(sReturn, 1)
Loop
LineTrim = sReturn
End Function
and now the C++ version, which will be in a DLL for VB
Code:
LPCSTR LineTrim(LPCSTR *Expression)
{
// I dont want to alter the original string,
// so I need to declare this return value correct?
LPCSTR ReturnVal;
// How can I move the pointer to the end of the LPCSTR
// without looping through it until '/0'
// How can I check for the ascii value of the current char,
// and shorten the string if it's CR or LF?
LineTrim = ReturnVal;
}
And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.
-
Sep 21st, 2002, 11:29 AM
#2
Fanatic Member
Code:
LPCSTR LineTrim(LPCSTR line) {
char *tmp = new char[strlen(line)];
strcpy(tmp,line);
strrev(tmp);
while(*tmp==char(10) || *tmp==char(13)){
*tmp++;
}
return strrev(tmp);
}
I think if you wanted to return a string to VB it had to be a BSTR, I'm not sure though
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Sep 21st, 2002, 04:06 PM
#3
Thread Starter
Hyperactive Member
Ok thanks! I'll give it a try.
And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.
-
Sep 21st, 2002, 04:18 PM
#4
Ya ya Baby!!!Me is Back
if I can remember vb(so long time didn't program on vb), but I think that it already have the function Trim() in vb...
-
Sep 21st, 2002, 05:11 PM
#5
Thread Starter
Hyperactive Member
Yeah it does, but it only trims spaces. I needed something that would trim carriage returns and line feeds as well..
While I'm here, is there a C++ function to get the Hexadecimal value of an expression?
Say I wanted the Hex value of "a", or of the number 72. Is there an existing method I can call?
And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.
-
Sep 22nd, 2002, 08:42 AM
#6
Monday Morning Lunatic
You can use itoa, and give a radix of 16 
PS: How you return a string to VB is tricky because you can't deallocate the C++ memory within VB...
BSTR is the way to go, I believe.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 22nd, 2002, 10:52 AM
#7
Fanatic Member
yup its BSTR
Code:
BSTR LineTrim(BSTR line) {
char *tmp = new char[strlen((LPCSTR)line)];
strcpy(tmp,(LPCSTR)line);
strrev(tmp);
while(*tmp==char(10) || *tmp==char(13)){
*tmp++;
}
return (BSTR)strrev(tmp);
}
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Sep 22nd, 2002, 11:43 AM
#8
Ya ya Baby!!!Me is Back
Originally posted by brenaaro
Yeah it does, but it only trims spaces. I needed something that would trim carriage returns and line feeds as well..
While I'm here, is there a C++ function to get the Hexadecimal value of an expression?
Say I wanted the Hex value of "a", or of the number 72. Is there an existing method I can call?
http://www.vbforums.com/showthread.p...hreadid=199715
-
Sep 23rd, 2002, 09:42 AM
#9
Frenzied Member
You are creating a memory leak -
Try (this is C)
Code:
unsigned int in(char, char *);
char *rtrim(char *s){ // remove trailing blanks
register char *buf;
char *remove = " \n"; // change this to alter what gets whacked
if(*s){
for(buf=s;*buf;buf++);
for(buf--;in(*buf,remove)&&(int(buf)>=(int)s; buf-- )
*buf=0x00;
}
return s;
}
unsigned int in(char ch,const char *test){
unsigned int result=1;
register char *buf;
for(buf=test;*buf,result;buf++) result=(ch==*buf);
return result;
}
Last edited by jim mcnamara; Sep 23rd, 2002 at 10:38 AM.
-
Sep 24th, 2002, 07:04 AM
#10
corrected version of naabels code:
Code:
BSTR LineTrim(BSTR line) {
wchar_t *tmp = new wchar_t[wcslen((LPCWSTR)line)];
wcscpy(tmp,(LPCWSTR)line);
wcsrev(tmp);
while(*tmp==wchar_t(10) || *tmp==wchar_t(13)){
tmp++;
}
BSTR ret = SysAllocString(wcsrev(tmp));
delete [] tmp;
return ret;
}
Avoids memory leaks and problem with incorrect usage of char.
Last edited by CornedBee; Sep 28th, 2002 at 07:06 AM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2002, 07:31 AM
#11
Thread Starter
Hyperactive Member
Great work guys, thanks for all the code. I never would have figured it out by myself..well..maybe I would but it would have taken months!
And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.
-
Sep 27th, 2002, 06:09 PM
#12
Fanatic Member
Originally posted by CornedBee
corrected version of naabels code:
Code:
BSTR LineTrim(BSTR line) {
wchar_t *tmp = new wchar_t[wcslen((LPCWSTR)line)];
wcscpy(tmp,(LPCWSTR)line);
wcsrev(tmp);
while(*tmp==wchar_t(10) || *tmp==wchar_t(13)){
*tmp++;
}
BSTR ret = SysAllocString(wcsrev(tmp));
delete [] tmp;
return ret;
}
Avoids memory leaks and problem with incorrect usage of char.
ah coolio..wheres the memory leaks? i wanna get this right
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Sep 28th, 2002, 07:06 AM
#13
BSTR is more than just a cast.
a) It's UNICODE, so you can't simply copy it to a ANSI string (that's why I used wchar_t).
b) It's counted, so you should use SysAllocString, which creates a completly valid BSTR for you.
c) You still don't free the memory allocated by new. And if you don't, no one else will (surely not VB).
d) *tmp++; don't dereference here.
Code:
BSTR LineTrim(BSTR line) {
char *tmp = new char[strlen((LPCSTR)line)];
strcpy(tmp,(LPCSTR)line);
strrev(tmp);
while(*tmp==char(10) || *tmp==char(13)){
*tmp++;
}
return (BSTR)strrev(tmp);
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|