strtok_r und Crosscompiling


BestNrXYZ

Member
Joined
Nov 17, 2005
Messages
372
Location
Oberhausen
LOCATION
Oberhausen
Nabend,

ich programmiere in der DEVcpp-Umgebung unter Windows. Jetzt bin ich dabei, ein *etwas* umfangreicheres Game zu porten, bei dem es sehr hilfreich wäre, Code-Änderungen erst mal unter Windows zu testen. Dies scheitert aber daran, dass es unter den Windows-Compilern (g++ und Mingw32++) die Funktion strtok_r nicht gibt. :(

Kann mir jemand mit einer Funktionersetzung (aus-)helfen?

Danke und

Greetz
BNrXYZ
 
Google läßt grüßen (http://www.mkssoftware.com/docs/man3/strtok_r.3.asp)
The strtok_r() function is a reentrant version of strtok(). It gets the next token from string s1, where tokens are strings separated by characters from s2. To get the first token from s1, strtok_r() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter. The string of delimiters, s2, can differ from call to call.
 
http://www.koders.com/?s=strtok_r+&_%3Abtn=Search&_%3Ala=C&_%3Ali=BSD
 
:)

Es ist ja nicht so, dass ich nicht gegoogelt hätte... Wofür strtok_s steht, weiß ich also. Nach "Googeln" hatte ich folgende Ersetzung verwendet:
Code:
<i>
</i>#ifdef WINDOWS
#define strtok_r(s,sep,lasts ) (*(lasts) = strtok((s),(sep)))
#endif

Das brachte mir zur Laufzeit aber immer nur Segmentation Faults :(

Und jetzt setze ich mich mal mit den Infos von hier auseinander.

Thx

Greetz
BNrXYZ
 
Ich komme trotzdem nicht weiter

<r>Nabend,<br/>
<br/>
die geposteten Links und weiteres googeln haben mich nicht unbedingt weiter gebracht (mag auch daran liegen, das ptrptr absolutes Neuland für mich sind).<br/>
<br/>
Deshalb hier mal der entsprechende Code (ich bin so frei):<br/>

<CODE><s>
Code:
</s><i>
</i>int Engine::getValueOfFlagTokens(char *realLine)
{
	if (strcmp(realLine, "0") == 0)
		return 0;

	char *store;
	char line[1024];
	bool found;
	int value;
	strcpy(line, realLine);

	int flags = 0;

	char *word = strtok_r(line, "+", &store);

	if (!word)
	{
		printf("ERROR: getValueOfFlagTokens() : NULL Pointer!\n");
		exit(1);
	}

	Data *data;
	
	while (true)
	{
		data = (Data*)defineList.getHead();
		found = false;

		while (data->next != NULL)
		{
			data = (Data*)data->next;

			if (strcmp(data->key, word) == 0)
			{
				value = -1;
				sscanf(data->value, "%d", &value);

				if (value == -1)
				{
					sscanf(data->value, "%*s %*d %*s %d", &value);
					value = 2 << value;
				}

				flags += value;
				found = true;
				break;
			}
		}

		if (!found)
		{
			printf("ERROR: getValueOfFlagTokens() : Illegal Token '%s'\n", word);
			#if IGNORE_FLAGTOKEN_ERRORS
				break;
			#else
				exit(1);
			#endif
		}

	word = strtok_r(NULL, "+", &store);

		if (!word)
			break;
	}

	return flags;
}
<e>
</e></CODE>

Frage: Hat irgend jemand eine sinnvolle / brauchbare Ersetzung von strtok_r für nicht Posix-basierende Systeme? Es geht um die Ersetzung von 2 strtok_r-Aufrufen (Zeile 14 von oben und Zeile 8 von unten).<br/>
<br/>
TIA<br/>
<br/>
Greetz<br/>
BNrXYZ<br/>
<br/>
P.S. Und nein, ich werde diesen Port nicht beim Game-Contest einreichen! Ich porte einfach nur für mich und für euch.</r>
 
You can just use the source of the C Libs. (you'll need more source than just this below, but you can download it, now that you know it is there)

P.
Code:
/* Reentrant string tokenizer.  Generic version.
   Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>

#undef strtok_r
#undef __strtok_r

#ifndef _LIBC
/* Get specification.  */
# include "strtok_r.h"
# define __strtok_r strtok_r
# define __rawmemchr strchr
#endif

/* Parse S into tokens separated by characters in DELIM.
   If S is NULL, the saved pointer in SAVE_PTR is used as
   the next starting point.  For example:
        char s[] = "-abc-=-def";
        char *sp;
        x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
        x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
        x = strtok_r(NULL, "=", &sp);   // x = NULL
                // s = "abc\0-def\0"
*/


char *
__strtok_r (char *s, const char *delim, char **save_ptr)
{
  char *token;

  if (s == NULL)
    s = *save_ptr;

  /* Scan leading delimiters.  */
  s += strspn (s, delim);
  if (*s == '\0')
    {
      *save_ptr = s;
      return NULL;
    }

  /* Find the end of the token.  */
  token = s;
  s = strpbrk (token, delim);
  if (s == NULL)
    /* This token finishes the string.  */
    *save_ptr = __rawmemchr (token, '\0');
  else
    {
      /* Terminate the token and make *SAVE_PTR point past it.  */
      *s = '\0';
      *save_ptr = s + 1;
    }
  return token;
}
#ifdef weak_alias
libc_hidden_def (__strtok_r)
weak_alias (__strtok_r, strtok_r)
#endif
 
Back
Top