Page 1 of 1

Strings handling in XC

Posted: Mon Feb 28, 2011 5:36 pm
by octal
Hello,
XC does not have pointer type, this explain the change that has been done on strings representation (to avoid using C pointers).
But how can we manipulate XC strings? (concatenation, copying, filling with char, ... ) are there any functions dedicated to that (like in C) ?

Regards

Re: Strings handling in XC

Posted: Mon Feb 28, 2011 6:06 pm
by Andy
Is calling a C function from XC that uses the relevant library call out of the question?

A similar question was asked here: https://www.xcore.com/forum/viewtopic.php?f=10&t=331

Re: Strings handling in XC

Posted: Mon Feb 28, 2011 9:30 pm
by octal
That means having to write a wrapper arround each C needed function. :?
maybe a good collaborative project to launch.

I'm writing a kind of scripts intepreter to be executed on an XMOS device, and for that I need a lot of string manipulation functions (mainly concat/comparison/chars extraction,...).

Re: Strings handling in XC

Posted: Mon Mar 07, 2011 12:01 am
by TSC
A string processing library would be a great idea. I'm doing a lot of string processing in my project too, and writing all these C wrappers and functions is kind of tedious.

I've got a few here. Some I stole from forums, some I wrote. But be aware that none of them have been tested particularly well. Please share your functions if you have any.

Code: Select all

#include <print.h>
#include <xccompat.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "utilities.h"

void utostrDIV(unsigned value1, unsigned value2, char str[])
{
   float value = ((float)value1)/((float)value2);
   sprintf(str, "%f", value);
}

void u_sprintf(unsigned value, char str[])
{
   float val = (float)value;
   sprintf(str, "%f", val);
}

void i_sprintf(int value, char str[])
{
   float val = (float)value;
   sprintf(str, "%f", val);
}

// UNTESTED
void c_sprintf(int value, char str[])
{
   sprintf(str, "%c", value);
}

// Returns length of a string
unsigned my_strlen(char str[])
{
   return (unsigned)strlen(str);
}

// Returns an integer representation of a string
int my_atoi(char str[])
{
   return atoi(str);
}

// Returns an integer representation of a char
// UNTESTED
int my_charAtoi(char c){
	return atoi(&c);
}

// Returns the integer location of the first occurrence of a char in a string
int my_strchr(char str[], char c){
	int tempInt = (int)c;
	//strchr(&str[0], tempInt);
	char *pos = strchr(str, c);
	if (pos == NULL)	// char not found
		return -1;
	else
		return (int)(pos - str);	// Subtract base location from pos location
}

// UNTESTED
void my_strcpy(char dest[], char src[]){
	//strcpy(str, strtok(dest, src));
	strcpy(dest, src);
}

// Compares two strings. Returns 0 if they match.
// UNTESTED
int my_strcmp(char str1[], char str2[]){
	return strcmp(str1, str2);
}

// If tok == 0, return the first token
// else, return subsequent token with each subsequent call.
// If there are no more tokens, the input string is set to "NULL".
void my_strtok(char str[], const char delim, int tok){
	char * tokString;
	if (tok == 0)
		tokString = strtok(str, &delim);
	else{	// Get the subsequent token on each call
		strcpy(str, strtok(NULL, &delim));	// pass a null pointer
	}
	if (str = NULL){	// Have to do this because XC doesn't understand a null pointer
		strcpy(str, "NULL");
	}
}

Re: Strings handling in XC

Posted: Mon Mar 07, 2011 4:04 pm
by richard
For the C functions in declared in <string.h> (e.g. strcat(), strlen()) there are XC versions in the
<safestring.h> header (e.g. safestrcat(), safestrlen()).

You will also be able to call some C library functions from XC directly without a wrapper so long as the prototype of the function doesn't use unsupported data types (e.g. pointers, floating point). In particular both sprintf() and atoi() are callable from XC.

Re: Strings handling in XC

Posted: Mon Mar 07, 2011 11:38 pm
by TSC
Thanks Richard, I wasn't aware of safestring.h. That will be very handy for certain things.

Since you are an XMOS employee, there is a related issue I'd like to raise about XC.

One feature I find lacking in the language is the ability to specify an array slice. If this functionality were implemented into the XC specification, people probably wouldn't need to rely nearly as heavily on C functions.
The following is an example of something I'd like to be able to do:

Code: Select all

safememcpy(dst, src[15 downto 8], length);
Am I correct in thinking that there is currently no way to do such an operation without resorting to C and playing with memory addresses? I'm a complete newbie in the XMOS world, so forgive me for any gaps in my understanding.

Re: Strings handling in XC

Posted: Tue Mar 08, 2011 6:05 pm
by richard
TSC wrote:One feature I find lacking in the language is the ability to specify an array slice. If this functionality were implemented into the XC specification, people probably wouldn't need to rely nearly as heavily on C functions.
The following is an example of something I'd like to be able to do:

Code: Select all

safememcpy(dst, src[15 downto 8], length);
Am I correct in thinking that there is currently no way to do such an operation without resorting to C and playing with memory addresses? I'm a complete newbie in the XMOS world, so forgive me for any gaps in my understanding.
Yes, currently there is no way to pass part of an array in XC. I agree this would be a useful feature and your suggestion has been noted.