[go: up one dir, main page]

Menu

[r4]: / string.c  Maximize  Restore  History

Download this file

148 lines (118 with data), 3.1 kB

  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
/*
* string.c
*
*
* this file is part of:
*
* JackDirector 0.1 --- Start/Stop Transport by midi command, play a metronome
*
* jack-director - 2014 (C) by Sergio Atzori <sergioatzori [at] gmail.com>
*/
/*
* 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-1307 USA
*/
/*
* system include files
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
/*
* my headers and definitions
*/
#include "string.h"
/*
* now to the functions
*/
char *GetLine(FILE *const fhandle)
/* This reads a line from a given file */
{
char *tmp,*tmp2;
long i=0;
char c;
tmp=(char *) calloc(1,sizeof(char));
while (((c=fgetc(fhandle)) != 10) && ( ! feof(fhandle) )) {
tmp2=tmp;
tmp=(char *) calloc(i+2,sizeof(char));
strcpy(tmp,tmp2);
free(tmp2);
tmp[i]=c;
i++;
tmp[i]='\0';
}
if (feof(fhandle)) {
*tmp = (char) EOF;
}
return tmp;
}
char *Skip(const char *const input)
/* Skips the next whitespace characters in a string */
{
char *cursor;
cursor=(char *const)input;
if (cursor != NULL) {
while (isspace(*cursor)) {
cursor++;
}
}
return cursor;
}
char *AllocString(const char *const string_constant)
/* This allocates a dynamic copy of a constant string */
{
char *tmp;
tmp=(char*) calloc(strlen(string_constant)+1,sizeof(char));
strcpy(tmp,string_constant);
return(tmp);
}
void Split(char *const input,char **begin,char **end) {
_Split(input, begin, end, '=');
}
void SplitComma(char *const input,char **begin,char **end) {
_Split(input, begin, end, ',');
}
void SplitColon(char *const input,char **begin,char **end) {
_Split(input, begin, end, ':');
}
void _Split(char *const input,char **begin,char **end, char *split)
/* This splits a string into two substrings at the first whitespace(s) */
{
char *cursor;
if (input == NULL) {
*begin=NULL;
*end=NULL;
} else {
/* find splitpoint */
for (cursor=input;(*cursor!='\0') && (*cursor != split);cursor++);
/* copy left part to begin */
if (cursor-input > 0) {
*begin=(char*)calloc(cursor-input+1,sizeof(char));
strncpy(*begin,input,cursor-input);
} else {
*begin=NULL;
}
/* find the first non-whitespace character */
cursor++;
cursor=Skip(cursor);
/* copy right part to end */
if (*cursor != '\0') {
*end=(char*)calloc(strlen(cursor)+1,sizeof(char));
strcpy(*end,cursor);
} else {
*end=NULL;
}
}
}