TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: George White
from: Bill Birrell
date: 1998-06-30 14:25:00
subject: LEFT$

Hey George,

    The untested code does work, but could be a bit tricky to use because of
the Pascal type strings. The compiler started flapping about basic types,
but that boiled down to the type of ret_str as unsigned char, as opposed to
char. Hence the finicky, fardelling casts. Huh!

------------------------------------------------------------
/* LEFT$[] code */
/* string str, first char being the string length (unsigned) */
/* length len, unsigned char (max 255 char string) */
#include 
#include 
#include 

char * left_func (unsigned char * str,unsigned char len)
{
unsigned char * ret_str;

if (str[0] <= len)
    {
    if ((ret_str = (unsigned char *) malloc((size_t)str[0] + 1)) == NULL)
        return (char *) ret_str;  /* ERROR TEST, change to suit */
    memcpy (ret_str,str,(size_t)str[0] + 1);
    }
else
    {
    if ((ret_str = (unsigned char *) malloc((size_t)len + 1)) == NULL)
        return (char *) ret_str;  /* ERROR TEST, change to suit */
    ret_str[0] = len;
    memcpy (&ret_str[1],&str[1],(size_t)len);
    }
return (char *) ret_str;
}
#ifdef DEBUG

int main(void)
{
    char miggy[] = {0,'A','m','i','g','a','B','a','s','i','c'};
    char result[6], *tmp;
    int len;

    len = sizeof(miggy)-1;
    miggy[0] = (unsigned char)len;
    tmp = left_func((unsigned char *)miggy,5);
    strncpy(result,tmp+1,(size_t)tmp[0]);
    result[tmp[0]]='\0';
    free(tmp);
    tmp = malloc(len+1);
    strncpy(tmp,&miggy[1],miggy[0]);
    tmp[len]='\0';
    printf("%s\n",result);
    printf("LEFT$(\"%s\", 5) returns the leftmost %d
characters of \"%s\".\n",
        tmp, 5, tmp);
    free(tmp);
    return 0;
}
#endif


------------------------------------------------------------

Best wishes,
Bill.

---
* Origin: Meerkats Anonymous (2:2504/200)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728
SEEN-BY: 639/252 670/213 218
@PATH: 2504/200 213 2503/501 252/356 140/1 270/101 396/1 633/260 635/506 728
@PATH: 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.