File: u_basic.nw

package info (click to toggle)
libgeda 20010722-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,208 kB
  • ctags: 1,564
  • sloc: ansic: 15,815; sh: 7,883; makefile: 135; awk: 131; perl: 59
file content (196 lines) | stat: -rw-r--r-- 4,466 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
@c -*- mode: Noweb; noweb-doc-mode: texinfo-mode; noweb-code-mode: c-mode -*-

@node File u_basic.c,,,Top
@chapter File @file{u_basic.c}

@section File header

<<u_basic.c : *>>=
<<u_basic.c : copyright and license>>

/* DO NOT read or edit this file ! Use ../noweb/u_basic.nw instead */

<<u_basic.c : include directives>>
<<u_basic.c : u_basic_strdup()>>
<<u_basic.c : u_basic_strdup_multiple()>>
<<u_basic.c : u_basic_breakup_string()>>

@


<<u_basic.c : copyright and license>>=
/* gEDA - GPL Electronic Design Automation
 * libgeda - gEDA's library
 * Copyright (C) 1998, 1999, 2000 Kazu Hirata / Ales Hvezda
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
 */

@ 


<<u_basic.c : include directives>>=
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include <guile/gh.h>

@


@section Function @code{u_basic_strdup()}

@defun u_basic_strdup p
@end defun

<<u_basic.c : u_basic_strdup()>>=
/* Kazu Hirata <kazu@seul.org> on July 25, 1999 - My own version of
 * strdup(). */
char *
u_basic_strdup(const char* p)
{
  char *q = (char *) malloc(strlen(p) + 1);
  if (q == NULL) {
    return NULL;
  }
  return strcpy(q, p);
}


@ %def u_basic_strdup


@section Function @code{u_basic_strdup_multiple()}

@defun u_basic_strdup_multiple str ...
@end defun

<<u_basic.c : u_basic_strdup_multiple()>>=
/* Kazu Hirata <kazu@seul.org> on July 25, 1999 - Allocates memory and
 * returns a pointer to a string that contains all strings passed as
 * parameters combined. The last one must be NULL so that the function
 * can identify the end of the list. */
char *
u_basic_strdup_multiple(const char *str, ...)
{
  int len;
  va_list vl;
  char *all;
  const char *tmp;

  /* get the total length */
  va_start(vl, str);
  len = 0;
  for(tmp = str; tmp != NULL; tmp = va_arg(vl, char *)) {
    len += strlen(tmp);
  }
  va_end(vl);

  /* '\0' is big enough to occupy its own seat */
  len++;

  /* allocate memory for all of them */
  all = malloc(sizeof(char) * len);
  if(all == NULL) {
    /* oops, the operating system is not nice to me... */
    return NULL;
  }

  /* now combine them all */
  va_start(vl, str);
  len = 0;
  for(tmp = str; tmp != NULL; tmp = va_arg(vl, char *)) {
    strcpy(&all[len], tmp);
    len += strlen(tmp);
  }
  va_end(vl);

  return all;
}


@ %def u_basic_strdup_multiple


@section Function @code{u_basic_breakup_string()}

@defun u_basic_breakup_string string count
@end defun

<<u_basic.c : u_basic_breakup_string()>>=
/* delimiters are , or space */
/* count starts at zero */
char *
u_basic_breakup_string(char *string, int count)
{
  int i=0, j=0;
  int internal_counter=0;
  int done=FALSE;
  char *return_value;

  /* skip over any leading white space */
  while(string[i] == ' ' && !string[i]) {
    i++;
  }

  /* Allocate space for temp string storage (+1 for null character) */ 
  return_value = malloc(sizeof(char)*(strlen(string) + 1));

  while(!done) {

    /* oops, ran out of string before we found what we were */
    /* looking for */
    if (i > strlen(string)) {
      free(return_value);
      return(NULL);
    }

    /* skip over any leading white space */
    while(string[i] == ' ' && string[i] != '\0') {
      i++;
    }

    j = 0;

    /* Old forgiving parsing */
    /*		while(string[i] != ',' && string[i] != ';' && */
    /*		      string[i] != ' ' && string[i] != '\0') {*/

    while(string[i] != ',' && string[i] != '\0') {
      return_value[j] = string[i];
      i++; j++;
    }

    if (internal_counter == count)  { 
      done = TRUE;	
    } else {
      internal_counter++;
      i++; /* skip the offending character */
    }
  }

  return_value[j] = '\0';
  return(return_value);
}


@ %def u_basic_breakup_string