Exemple #1
0
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);
}
Exemple #2
0
String File_appendFileNameCString(String fileName, const char *name)
{
  assert(fileName != NULL);
  assert(name != 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_appendCString(fileName,name);
  
  return fileName;
}
Exemple #3
0
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
}
Exemple #4
0
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;
}