Strings handling in XC

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
octal
XCore Addict
Posts: 228
Joined: Thu Jan 27, 2011 3:30 pm
Location: Argenteuil - France
Contact:

Strings handling in XC

Post 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


User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post 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
User avatar
octal
XCore Addict
Posts: 228
Joined: Thu Jan 27, 2011 3:30 pm
Location: Argenteuil - France
Contact:

Post 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,...).
User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post 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");
	}
}
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post 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.
User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post 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.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post 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.
Post Reply