TORCS  1.3.9
The Open Racing Car Simulator
string_utils.cpp
Go to the documentation of this file.
1 // Copyright (C) 2004 Johnny Mariethoz (Johnny.Mariethoz@idiap.ch)
2 // and Samy Bengio (bengio@idiap.ch)
3 // and Ronan Collobert (collober@iro.umontreal.ca)
4 // and Christos Dimitrakakis (dimitrak@idiap.ch)
5 //
6 // This file is based on code from Torch. Release II.
7 /***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <learning/string_utils.h>
17 #include <stdarg.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <portability.h>
21 
22 static char msgbuf[10000];
23 static FILE* msgport = stderr;
24 
25 char *strBaseName(char *filename) {
26  char *p = strrchr(filename, '/');
27  return p ? (p+1) : filename;
28 }
29 
30 char *strRemoveSuffix(char *filename, char c)
31 {
32  char *copy = NULL;
33  int len = strlen(filename);
34  char *p = filename + len - 1;
35  int i=len;
36  while (*p != c && i-- >=0) p--;
37  if (i>0) {
38  //*p = '\0';
39  copy = (char*)malloc(sizeof(char)*i);
40  strncpy(copy,filename,i-1);
41  copy[i-1] = '\0';
42  } else {
43  copy = (char*)malloc(sizeof(char)*(len+1));
44  strcpy(copy,filename);
45  }
46  return copy;
47 }
48 
49 char *strConcat(int n, ...)
50 {
51  char **strs = (char **)malloc(sizeof(char *)*n);
52 
53  int taille = 0;
54  va_list args;
55  va_start(args, n);
56  int i;
57  for(i = 0; i < n; i++)
58  {
59  strs[i] = va_arg(args, char *);
60  taille += strlen(strs[i]);
61  }
62  va_end(args);
63  taille++; // Pour le truc de fin
64 
65  char *the_concat = (char *)malloc(sizeof(char)*taille);
66  the_concat[0] = '\0';
67 
68  for(i = 0; i < n; i++)
69  strcat(the_concat, strs[i]);
70 
71  free(strs);
72 
73  return(the_concat);
74 }
75 
76 
77 
78 void message(const char* msg, ...)
79 {
80  va_list args;
81  va_start(args,msg);
82  vsnprintf(msgbuf, 10000, msg, args);
83  fprintf(msgport, "# %s\n", msgbuf);
84  fflush(stdout);
85  va_end(args);
86 }
87 #define INIT_MSG_LEN 100
88 
89 
90 //====================================================================
91 // read_string() Dynamically read a string
92 //--------------------------------------------------------------------
94 {
95  unsigned int l;
96  fpos_t pos;
97 
98  if (s==NULL) {
99  s = NewStringBuffer (10);
100  }
101 
102  while (1) {
103  l = s->length;
104  if (fgetpos (f, &pos)) {
105  printf ("Error getting position\n");
106  }
107  if ((s->string = fgets (s->c, l, f)) == NULL) {
108  return s;
109  }
110  if (strlen(s->string)<(l-1)) {
111  return s;
112  }
113 
114  if (fsetpos (f, &pos)) {
115  printf ("Error setting position\n");
116  }
117  s->length += l;
118  if ((s->c = (char*) realloc (s->c, s->length)) == NULL) {
119  fprintf (stderr, "Oops, out of RAM\n");
120  FreeStringBuffer (&s);
121  return NULL;
122  }
123  }
124 
125 
126 }
127 
128 
129 
131 {
132  if (l > s->length) {
133  s->length = l;
134  if ((s->c = (char*) realloc (s->c, s->length)) == NULL) {
135  fprintf (stderr, "Oops, out of RAM\n");
136  FreeStringBuffer (&s);
137  return NULL;
138  }
139  }
140  return s;
141 }
142 
144 {
145  StringBuffer* s = AllocM (StringBuffer, 1);
146  if (s==NULL) {
147  return NULL;
148  }
149  s->length = length;
150  if ((s->c = AllocM (char, length))==NULL) {
151  FreeStringBuffer (&s);
152  }
153  return s;
154 }
155 
157 {
158  if ((*s)->c) {
159  FreeM ((*s)->c);
160  }
161  FreeM ((*s));
162  s = NULL;
163 }
164 
165 char* string_copy (char* c)
166 {
167  char* r;
168  if (c==NULL) return NULL;
169  r = AllocM (char, (strlen(c)+1));
170  strcpy (r, c);
171  return r;
172 }
char * strRemoveSuffix(char *filename, char c)
Returns a fresh copy of the name of a file without suffix.
char * string
This is the string.
Definition: string_utils.h:37
#define FreeM(address)
Definition: learn_debug.h:31
char * string_copy(char *c)
copies a string
StringBuffer * NewStringBuffer(int length)
Make a new stringbuffer.
Some simple functions for string operations.
static char msgbuf[10000]
StringBuffer * SetStringBufferLength(StringBuffer *s, unsigned int l)
Scalar length(const Quaternion &q)
Definition: Quaternion.h:193
static Point p[4]
Definition: Convex.cpp:54
unsigned int length
This is the buffer length.
Definition: string_utils.h:38
char * c
This is the buffer.
Definition: string_utils.h:36
char * strConcat(int n,...)
Returns the concatenation n strings.
#define AllocM(type, x)
Definition: learn_debug.h:30
char * strBaseName(char *filename)
Returns the name of a file without leading pathname.
void FreeStringBuffer(StringBuffer **s)
Given a pointer to a stringbuffer pointer, free it and clear it.
static FILE * msgport
StringBuffer * read_string(FILE *f, StringBuffer *s)
Reads a string dynamically.
The StringBuffer structure stores buffers of strings.
Definition: string_utils.h:35
void message(const char *msg,...)
Prints a message.