String File_appendFileNameChar(String fileName, char ch) { assert(fileName != NULL); if (String_length(fileName) > 0) { if (String_index(fileName,String_length(fileName)-1) != FILES_PATHNAME_SEPARATOR_CHAR) { String_appendChar(fileName,FILES_PATHNAME_SEPARATOR_CHAR); } } String_appendChar(fileName,ch); return fileName; }
void distance2text (String s, char *species, double dist) { char distbuffer[32]; String_appendCString(s, species); String_appendChar(s, ':'); sprintf(distbuffer, "%g", dist); String_appendCString(s, distbuffer); }
String File_appendFileNameBuffer(String fileName, const char *buffer, ulong bufferLength) { assert(fileName != NULL); if (String_length(fileName) > 0) { if (String_index(fileName,String_length(fileName)-1) != FILES_PATHNAME_SEPARATOR_CHAR) { String_appendChar(fileName,FILES_PATHNAME_SEPARATOR_CHAR); } } String_appendBuffer(fileName,buffer,bufferLength); return fileName; }
LOCAL bool readProcessIO(int fd, String line) { #if defined(PLATFORM_LINUX) int n; #elif defined(PLATFORM_WINDOWS) u_long n; #endif /* PLATFORM_... */ char ch; do { // check if data available #if defined(PLATFORM_LINUX) ioctl(fd,FIONREAD,&n); #elif defined(PLATFORM_WINDOWS) // NYI ??? #warning not implemented #endif /* PLATFORM_... */ // read data until EOL found while (n > 0) { if (read(fd,&ch,1) == 1) { switch (ch) { case '\n': case '\r': case '\b': if (String_length(line) > 0L) return TRUE; break; default: String_appendChar(line,ch); break; } n--; } else { n = 0; } } } while (n > 0); return FALSE; }
void tree2string_node(struct Tnode *node, String s) { if (node != NULL) { if (node->left == NULL && node->right == NULL) { if (node->clust->clustersize == 0 || node->clust->members == NULL) { } else if (node->clust->clustersize == 1) { distance2text(s, node->clust->members[0]->name, node->distance); } else { unsigned int i; for (i=0; i<node->clust->clustersize-1; i++) { String_appendChar(s, '('); distance2text(s, node->clust->members[i]->name, 0.0); } distance2text(s, node->clust->members[i]->name, 0.0); String_appendChar(s, ')'); for (i=0; i<node->clust->clustersize-2; i++) { String_appendChar(s, ':'); distance2text(s, "", 0.0); String_appendChar(s, ')'); } String_appendChar(s, ':'); distance2text(s, "", node->distance); } } else if (node->left != NULL && node->right != NULL) { String_appendChar(s, '('); tree2string_node(node->left, s); String_appendChar(s, ','); tree2string_node(node->right, s); String_appendChar(s, ')'); distance2text(s, "", node->distance); } } }
LOCAL bool readProcessIO(int fd, String line) { int n; char ch; do { // check if data available ioctl(fd,FIONREAD,&n); // read data until EOL found while (n > 0) { if (read(fd,&ch,1) == 1) { switch (ch) { case '\n': case '\r': case '\b': if (String_length(line) > 0L) return TRUE; break; default: String_appendChar(line,ch); break; } n--; } else { n = 0; } } } while (n > 0); return FALSE; }
Errors File_readLine(FileHandle *fileHandle, String line ) { int ch; assert(fileHandle != NULL); assert(fileHandle->file != NULL); assert(line != NULL); String_clear(line); do { ch = getc(fileHandle->file); if (ch >= 0) fileHandle->index += 1; if (ch < 0) { return ERRORX(IO_ERROR,errno,String_cString(fileHandle->name)); } if (((char)ch != '\n') && ((char)ch != '\r')) { String_appendChar(line,ch); } } while (((char)ch != '\r') && ((char)ch != '\n')); if ((char)ch == '\r') { ch = getc(fileHandle->file); if (ch >= 0) fileHandle->index += 1; if (ch != '\n') { fileHandle->index -= 1; ungetc(ch,fileHandle->file); } } return ERROR_NONE; }
String Misc_expandMacros(String string, const char *macroTemplate, const TextMacro macros[], uint macroCount ) { #define APPEND_CHAR(string,index,ch) \ do \ { \ if ((index) < sizeof(string)-1) \ { \ (string)[index] = ch; \ (index)++; \ } \ } \ while (0) #define SKIP_SPACES(string,i) \ do \ { \ while ( ((string)[i] != '\0') \ && isspace((string)[i]) \ ) \ { \ (i)++; \ } \ } \ while (0) bool macroFlag; ulong i; uint j; char name[128]; char format[128]; assert(macroTemplate != NULL); assert((macroCount == 0) || (macros != NULL)); String_clear(string); i = 0; do { // add prefix string macroFlag = FALSE; while ((macroTemplate[i] != '\0') && !macroFlag) { if (macroTemplate[i] == '%') { if ((macroTemplate[i+1] == '%')) { String_appendChar(string,'%'); i+=2; } else { macroFlag = TRUE; i++; } } else { String_appendChar(string,macroTemplate[i]); i++; } } if (macroFlag) { // skip spaces SKIP_SPACES(macroTemplate,i); // get macro name j = 0; if ( (macroTemplate[i] != '\0') && isalpha(macroTemplate[i]) ) { APPEND_CHAR(name,j,'%'); do { APPEND_CHAR(name,j,macroTemplate[i]); i++; } while ( (macroTemplate[i] != '\0') && isalnum(macroTemplate[i]) ); } name[j] = '\0'; // get format data (if any) j = 0; if (macroTemplate[i] == ':') { // skip ':' i++; // skip spaces SKIP_SPACES(macroTemplate,i); // get format string APPEND_CHAR(format,j,'%'); while ( (macroTemplate[i] != '\0') && ( isdigit(macroTemplate[i]) || (macroTemplate[i] == '-') || (macroTemplate[i] == '.') ) ) { APPEND_CHAR(format,j,macroTemplate[i]); i++; } while ( (macroTemplate[i] != '\0') && (strchr("l",macroTemplate[i]) != NULL) ) { APPEND_CHAR(format,j,macroTemplate[i]); i++; } if ( (macroTemplate[i] != '\0') && (strchr("duxfsS",macroTemplate[i]) != NULL) ) { APPEND_CHAR(format,j,macroTemplate[i]); i++; } } format[j] = '\0'; // find macro if (strlen(name) > 0) { // find macro j = 0; while ( (j < macroCount) && (strcmp(name,macros[j].name) != 0) ) { j++; } if (j < macroCount) { // get default format if no format given if (strlen(format) == 0) { switch (macros[j].type) { case TEXT_MACRO_TYPE_INTEGER: strcpy(format,"%d"); break; case TEXT_MACRO_TYPE_INTEGER64: strcpy(format,"%lld"); break; case TEXT_MACRO_TYPE_DOUBLE: strcpy(format,"%lf"); break; case TEXT_MACRO_TYPE_CSTRING: strcpy(format,"%s"); break; case TEXT_MACRO_TYPE_STRING: strcpy(format,"%S"); break; #ifndef NDEBUG default: HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE(); break; /* not reached */ #endif /* NDEBUG */ } } // expand macro switch (macros[j].type) { case TEXT_MACRO_TYPE_INTEGER: String_format(string,format,macros[j].value.i); break; case TEXT_MACRO_TYPE_INTEGER64: String_format(string,format,macros[j].value.l); break; case TEXT_MACRO_TYPE_DOUBLE: String_format(string,format,macros[j].value.d); break; case TEXT_MACRO_TYPE_CSTRING: String_format(string,format,macros[j].value.s); break; case TEXT_MACRO_TYPE_STRING: String_format(string,format,macros[j].value.string); break; #ifndef NDEBUG default: HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE(); break; /* not reached */ #endif /* NDEBUG */ } } else { // keep unknown macro String_appendCString(string,name); } } else { // empty macro: add empty string String_format(string,format,""); } } } while (macroFlag); return string; #undef SKIP_SPACES #undef APPEND_CHAR }
char * tree2string (struct Tree *tree) { String s; s = String_new(); if (tree != NULL) { if (tree->child[0] != NULL) { if (tree->child[1] == NULL) { if (tree->child[0]->left != NULL && tree->child[0]->right != NULL) { String_appendChar(s, '('); tree2string_node(tree->child[0]->left, s); String_appendChar(s, ','); tree2string_node(tree->child[0]->right, s); String_appendCString(s, ");"); } else { if (tree->child[0]->clust->clustersize == 1) { String_appendCString(s, "0;"); } else { unsigned int i; for (i=0; i<tree->child[0]->clust->clustersize-1; i++) { String_appendChar(s, '('); distance2text(s, tree->child[0]->clust->members[i]->name, 0.0); } distance2text(s, tree->child[0]->clust->members[i]->name, 0.0); for (i=0; i<tree->child[0]->clust->clustersize-2; i++) { String_appendCString(s, "):0.0)"); } String_appendCString(s, ");"); } } } } } // unsigned int strlen = String_length(s); char *buffer = NULL; // buffer = malloc(sizeof(char) * (strlen+1)); // strncpy(buffer, String_cString(s), strlen); char *position; const char *cString = String_cString(s); // fprintf(stderr, "[TREE]: ### %s ###\n", cString); position = strchr(cString, ';'); if (position == NULL) { fprintf(stderr, "Error in trees\n"); exit(-1); } int index = cString - position; if (index < 0) { index = -index; } // fprintf(stderr, "\tindex: %d\n", index); buffer = malloc(sizeof(char) * (index+2)); strncpy(buffer, cString, index+1); buffer[index+1] = '\0'; // fprintf(stderr, "\tbuffer: %s\n", buffer); String_delete(s); return buffer; }