diff --git a/Makefile b/Makefile index 1adde93..b499454 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,8 @@ SRCS = cli.c \ espeakup.c \ queue.c \ signal.c \ - softsynth.c + softsynth.c \ + stringhandling.c OBJS = ${SRCS:.c=.o} diff --git a/softsynth.c b/softsynth.c index d70c381..0b9f76f 100644 --- a/softsynth.c +++ b/softsynth.c @@ -26,105 +26,20 @@ #include #include "espeakup.h" +#include "stringhandling.h" /* max buffer size */ -/* A big fat buffer. */ static const size_t maxBufferSize = 16 * 1024 + 1; /* synth flush character */ static const int synthFlushChar = 0x18; - static int softFD = 0; /* Text accumulator: */ char *textAccumulator; int textAccumulator_l; -/* String routines, borrowed from edbrowse: */ -char *EMPTYSTRING = ""; - -void *allocMem(size_t n) -{ - void *s; - if (!n) - return EMPTYSTRING; - if (!(s = malloc(n))) { - fprintf(stderr, "Out of memory!\n"); - exit(1); - } - return s; -} /* allocMem */ - -void *reallocMem(void *p, size_t n) -{ - void *s; - if (!n) { - fprintf(stderr, "Trying to reallocate memory with size of 0.\n"); - exit(1); - } - if (!p) { - fprintf(stderr, "realloc called with a NULL pointer!\n"); - exit(1); - } - if (p == EMPTYSTRING) - return allocMem(n); - if (!(s = realloc(p, n))) { - fprintf(stderr, "Failed to allocate memory.\n"); - exit(1); - } - return s; -} /* reallocMem */ - -char *initString(int *l) -{ - *l = 0; - return EMPTYSTRING; -} - -void stringAndString(char **s, int *l, const char *t) -{ - char *p = *s; - int oldlen, newlen, x; - oldlen = *l; - newlen = oldlen + strlen(t); - *l = newlen; - ++newlen; /* room for the 0 */ - x = oldlen ^ newlen; - if (x > oldlen) { /* must realloc */ - newlen |= (newlen >> 1); - newlen |= (newlen >> 2); - newlen |= (newlen >> 4); - newlen |= (newlen >> 8); - newlen |= (newlen >> 16); - p = reallocMem(p, newlen); - *s = p; - } - strcpy(p + oldlen, t); -} /* stringAndString */ - -void stringAndBytes(char **s, int *l, const char *t, int cnt) -{ - char *p = *s; - int oldlen, newlen, x; - oldlen = *l; - newlen = oldlen + cnt; - *l = newlen; - ++newlen; - x = oldlen ^ newlen; - if (x > oldlen) { /* must realloc */ - newlen |= (newlen >> 1); - newlen |= (newlen >> 2); - newlen |= (newlen >> 4); - newlen |= (newlen >> 8); - newlen |= (newlen >> 16); - p = reallocMem(p, newlen); - *s = p; - } - memcpy(p + oldlen, t, cnt); - p[oldlen + cnt] = 0; -} /* stringAndBytes */ - static void queue_add_cmd(enum command_t cmd, enum adjust_t adj, int value) { struct espeak_entry_t *entry; diff --git a/stringhandling.c b/stringhandling.c new file mode 100644 index 0000000..8c18a39 --- /dev/null +++ b/stringhandling.c @@ -0,0 +1,112 @@ +/* + * espeakup - interface which allows speakup to use espeak + * + * Copyright (C) 2011 William Hubbs + * + * 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 3 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, see . + */ + +/* + * These string routines were borrowed from edbrowse, which was + * originally written by Karl Dahlke, and is being currently maintained + * by Christopher Brannon. + * I would like to thank both of them for their work on this software. + */ + +#include +#include +#include + +char *EMPTYSTRING = ""; + +void *allocMem(size_t n) +{ + void *s; + if (!n) + return EMPTYSTRING; + if (!(s = malloc(n))) { + fprintf(stderr, "Out of memory!\n"); + exit(1); + } + return s; +} + +void *reallocMem(void *p, size_t n) +{ + void *s; + if (!n) { + fprintf(stderr, "Trying to reallocate memory with size of 0.\n"); + exit(1); + } + if (!p) { + fprintf(stderr, "realloc called with a NULL pointer!\n"); + exit(1); + } + if (p == EMPTYSTRING) + return allocMem(n); + if (!(s = realloc(p, n))) { + fprintf(stderr, "Failed to allocate memory.\n"); + exit(1); + } + return s; +} + +char *initString(int *l) +{ + *l = 0; + return EMPTYSTRING; +} + +void stringAndString(char **s, int *l, const char *t) +{ + char *p = *s; + int oldlen, newlen, x; + oldlen = *l; + newlen = oldlen + strlen(t); + *l = newlen; + ++newlen; /* room for the 0 */ + x = oldlen ^ newlen; + if (x > oldlen) { /* must realloc */ + newlen |= (newlen >> 1); + newlen |= (newlen >> 2); + newlen |= (newlen >> 4); + newlen |= (newlen >> 8); + newlen |= (newlen >> 16); + p = reallocMem(p, newlen); + *s = p; + } + strcpy(p + oldlen, t); +} + +void stringAndBytes(char **s, int *l, const char *t, int cnt) +{ + char *p = *s; + int oldlen, newlen, x; + oldlen = *l; + newlen = oldlen + cnt; + *l = newlen; + ++newlen; + x = oldlen ^ newlen; + if (x > oldlen) { /* must realloc */ + newlen |= (newlen >> 1); + newlen |= (newlen >> 2); + newlen |= (newlen >> 4); + newlen |= (newlen >> 8); + newlen |= (newlen >> 16); + p = reallocMem(p, newlen); + *s = p; + } + memcpy(p + oldlen, t, cnt); + p[oldlen + cnt] = 0; +} diff --git a/stringhandling.h b/stringhandling.h new file mode 100644 index 0000000..1353391 --- /dev/null +++ b/stringhandling.h @@ -0,0 +1,31 @@ +/* + * espeakup - interface which allows speakup to use espeak + * + * Copyright (C) 2011 William Hubbs + * + * 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 3 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, see . + */ + +#ifndef __STRINGHANDLING_H +#define __STRINGHANDLING_H + +extern char *EMPTYSTRING; + +void *allocMem(size_t n); +void *reallocMem(void *p, size_t n); +char *initString(int *l); +void stringAndString(char **s, int *l, const char *t); +void stringAndBytes(char **s, int *l, const char *t, int cnt); + +#endif