Results 1 to 13 of 13

Thread: Help translating from VB

  1. #1

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391

    Help translating from VB

    Can someone help me translate this from VB to C++?
    VB Code:
    1. ' Visual Basic version
    2. Public Function LineTrim(Expression As String) As String
    3. 'Remove trailing CRs and LFs
    4.     Dim sReturn As String
    5.     Dim sCheck As String
    6.    
    7.     sReturn = Expression
    8.    
    9.     sCheck = Right(sReturn, 1)
    10.  
    11.     Do While (sCheck = Chr(10)) Or (sCheck = Chr(13))
    12.         sReturn = Left(sReturn, Len(sReturn) - 1)
    13.         sCheck = Right(sReturn, 1)
    14.     Loop
    15.    
    16.     LineTrim = sReturn
    17.    
    18. 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.

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  3. #3

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    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.

  4. #4
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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...

  5. #5

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  8. #8
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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

  9. #9
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    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.

  12. #12
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width