Exemple #1
0
int main(void)
{
  const char *s = "ABCD1234EFGHXXXX";
  const char *t = "ABCD1234EFGHXXXX";
  const char *u = "AbCdEfGh";
  const char *v = "aBcDeFgH";
  char *x;
  long i;
  unsigned long pos;

  test_assert(str_chr(s, 'D') == 3);

  i = str_char(s, 'D', &pos);
  test_assert(i == 1);
  test_assert(pos == 3);

  test_assert(str_diff(s, t) == 0);
  test_assert(str_same(s, t));
  test_assert(str_ndiff(s, t, 8) == 0);
  test_assert(str_nsame(s, t, 8) == 1);

  test_assert(str_casei_diff(u, v) == 0);
  test_assert(str_casei_same(u, v) == 1);
  test_assert(str_casei_ndiff(u, v, 8) == 0);
  test_assert(str_casei_nsame(u, v, 8) == 1);;

  test_assert(str_rchr(s, 'X') == 15);

  i = str_rchar(s, 'X', &pos);
  test_assert(i == 1);
  test_assert(pos == 15);

  test_assert(str_dup(s, &x));

  test_assert(str_same(s, x) == 1);
  test_assert(str_nsame(s, x, 8) == 1);

  test_assert(str_starts(s, "ABCD") == 1);
  test_assert(str_starts(s, "XYZA") == 0);
  test_assert(str_starts(s, "1234EFGH1234EFGH1234EFGH") == 0);

  test_assert(str_ends(s, "XXXX") == 1);
  test_assert(str_ends(s, "ABCD") == 0);
  test_assert(str_ends(s, "GH1234EFGH123GH1234EFGH123") == 0);

  test_assert(str_len(s) == 16);
  
  str_toupper(x);
  test_assert(x[0] == 'A');
  str_tolower(x);
  test_assert(x[0] == 'a');

  return 0;
}
double Formulaeditor::char_n(int& nPosition, QString& strCharacter)
{
    do
    {
        nPosition ++;
        if (nPosition <= m_strFunction.length())
            strCharacter = m_strFunction.mid(nPosition - 1, 1);
        else
            strCharacter = str_char("?");
    }
    while (strCharacter == " ");

    return nPosition;
}
Exemple #3
0
/**************************************************************************
 * Generate logos for all motifs in a file
 **************************************************************************/
static void generate_file_logos(OPTIONS_T *options) {
  STR_T *path;
  MREAD_T *mread;
  MOTIF_T *motif;
  // file path buffer
  path = str_create(100);
  str_append(path, options->dir, strlen(options->dir));
  if (str_char(path, -1) != '/') str_append(path, "/", 1);
  // create output directory
  if (create_output_directory(str_internal(path), TRUE, FALSE)) 
    exit(EXIT_FAILURE);
  // open motif file
  mread = mread_create(options->motifs_file, OPEN_MFILE);
  while (mread_has_motif(mread)) {
    motif = mread_next_motif(mread);
    generate_motif_logos(options, path, motif);
    destroy_motif(motif);
  }
  mread_destroy(mread);
  str_destroy(path, FALSE);
}
Exemple #4
0
/*
 * dbg_parse_cmd
 *
 * Analyses the keyboard input of the current shell. It
 * will split the command line into a set of single
 * atoms. Each atom will be stored in it order to the
 * pars[] list of the shell structure. The pointer to the
 * buffer with the first atom will be also included in "cmd".
 * 
 * Return values:
 *	0<	Error
 *	0	Empty input
 *	1 + n	Command and n parameters
 *
 */
int dbg_parse_cmd(void)
{
	dbg_shell_t *l__shell = (*dbg_tls_shellptr);
	utf8_t *l__bufptr = l__shell->cmd_buffer;
	int l__retval = 0;
	size_t l__len = str_len(l__bufptr, DBGSHELL_CMDBUFFER_SIZE) + 1;
		
	while(*l__bufptr != '\0')
	{
		/* Search until we find sthg. else than a space */
		if (*l__bufptr != ' ')
		{
			utf8_t l__term = 0;
			utf8_t *l__endptr = NULL;
			utf8_t *l__outbuf = NULL;
			
			/* Is it a trailing " or the begin of a atomic command? */
			if (*l__bufptr == '\"')
			{
				l__term = '\"';
				l__bufptr ++;
				if (*l__bufptr == '\0') break;
			}
			 else
			{
				l__term = ' ';
			}
			
			utf8_t *l__seekptr = l__bufptr;
			size_t l__atmplen = l__len;
			
			while (1)
			{
				/* Search the end of our command */
				l__endptr = str_char(l__seekptr, l__term, l__atmplen);
			
				/* Not the terminating charracter, but an escaped \"? */
				if ((l__endptr != NULL) && (l__term == '\"'))
				{
					if (*(l__endptr - 1) == '\\') 
					{
						l__atmplen -= ((uintptr_t)l__endptr - (uintptr_t)l__seekptr) + 1;
						
						l__seekptr = l__endptr + 1;
						
						continue;
					}
				}
				
				break;
			}
			
			/* End of the string */
			if (l__endptr == NULL)
			{
				l__outbuf = mem_alloc(sizeof(utf8_t) * (l__len + 1));
				if (l__outbuf == NULL)
				{
					dbg_isprintf("(1) Can't allocate memory for the parameter buffer of 0x%X, because of %i\n", (*tls_my_thread)->thread_sid, *tls_errno);
					return l__retval;
				}
				
				size_t l__newlen = dbg_filter_copy(l__outbuf, l__bufptr, l__len);
				l__outbuf[l__newlen] = '\0';
				
				l__outbuf = mem_realloc(l__outbuf, l__newlen + 1);
				if (l__outbuf == NULL)
				{
					dbg_isprintf("(3) Can't allocate memory for the parameter buffer of 0x%X, because of %i\n", (*tls_my_thread)->thread_sid, *tls_errno);
					return l__retval;
				}
				
			}
			 else /* Somewhere within the string */
			{
				size_t l__tmplen = ((uintptr_t)l__endptr - (uintptr_t)l__bufptr);
				
				l__outbuf = mem_alloc(sizeof(utf8_t) * (l__tmplen + 1));
				
				if (l__outbuf == NULL)
				{
					dbg_isprintf("(2) Can't allocate memory for the parameter buffer of 0x%X, because of %i\n", (*tls_my_thread)->thread_sid, *tls_errno);
					return l__retval;
				}
				
				size_t l__newlen = dbg_filter_copy(l__outbuf, l__bufptr, l__tmplen);			
				
				l__outbuf[l__newlen] = '\0';
				l__len -= l__tmplen;
				l__outbuf = mem_realloc(l__outbuf, l__newlen + 1);
				if (l__outbuf == NULL)
				{
					dbg_isprintf("(4) Can't allocate memory for the parameter buffer of 0x%X, because of %i\n", (*tls_my_thread)->thread_sid, *tls_errno);
					return l__retval;
				}				
			}
			
			/* Put it into the parameter list */
			utf8_t **l__pars = mem_realloc(l__shell->pars, (l__shell->n_pars + 1) * sizeof(utf8_t*));
			if (l__pars == NULL)
			{
				dbg_isprintf("Can't allocate memory for the parameter list of 0x%X, because of %i\n", (*tls_my_thread)->thread_sid, *tls_errno);
				return l__retval;
			}
			
			l__shell->pars = l__pars;
			
			/* Put it into the parameter buffer */
			l__shell->pars[l__shell->n_pars] = l__outbuf;
			l__shell->n_pars ++;
			
			l__retval ++;	
			
			/* Search for the next parameter */			
			if (l__endptr != NULL)
			{
				l__bufptr = l__endptr + 1;
			}
			 else
			{
				break;
			}
		} 
		 else
		{
			/* Next char, continue */
			l__bufptr ++;
			l__len --;
		}
	}

	/* Link the first parameter to "cmd" */
	if (l__shell->pars != NULL) 
	{
		l__shell->cmd = l__shell->pars[0];
	}
	
	/* Convert all variables to strings */
	int l__n = l__shell->n_pars;

	while (l__n --)
	{
		if (l__shell->pars[l__n] != NULL)
		{
			/* Is it a value? */
			if (l__shell->pars[l__n][0] == '$')
			{
				const utf8_t *l__name = &l__shell->pars[l__n][1];
				utf8_t l__buf[32];
				
				/* Get the value */
				if (dbg_get_value(l__name, l__buf, 32))
					continue;
				
				/* Set the parameter to the value of the variable */
				mem_free(l__shell->pars[l__n]);
				l__shell->pars[l__n] = mem_alloc(str_len(l__buf, 32) + 1);
				str_copy(l__shell->pars[l__n], l__buf, 32);
			}
		}
	}

	return l__retval;
}
/*
 * Load background file frequencies into the array.
 */
ARRAY_T* get_file_frequencies(ALPH_T *alph, char *bg_filename, ARRAY_T *freqs) {
  regmatch_t matches[4];
  STR_T *line;
  char chunk[BG_CHUNK_SIZE+1], letter[2], *key;
  int size, terminate, offset, i;
  FILE *fp;
  regex_t bgfreq;
  double freq;
  RBTREE_T *letters;
  RBNODE_T *node;
  
  regcomp_or_die("bg freq", &bgfreq, BGFREQ_RE, REG_EXTENDED);
  letters = rbtree_create(rbtree_strcasecmp, rbtree_strcpy, free, rbtree_dblcpy, free);
  line = str_create(100);
  if (!(fp = fopen(bg_filename, "r"))) {
    die("Unable to open background file \"%s\" for reading.\n", bg_filename);
  }
  
  terminate = feof(fp);
  while (!terminate) {
    size = fread(chunk, sizeof(char), BG_CHUNK_SIZE, fp);
    chunk[size] = '\0';
    terminate = feof(fp);
    offset = 0;
    while (offset < size) {
      // skip mac newline
      if (str_len(line) == 0 && chunk[offset] == '\r') {
        offset++;
        continue;
      }
      // find next new line
      for (i = offset; i < size; ++i) {
        if (chunk[i] == '\n') break;
      }
      // append portion up to the new line or end of chunk
      str_append(line, chunk+offset, i - offset);
      // read more if we didn't find a new line
      if (i == size && !terminate) break;
      // move the offset past the new line
      offset = i + 1;
      // handle windows new line
      if (str_char(line, -1) == '\r') str_truncate(line, -1);
      // remove everything to the right of a comment character
      for (i = 0; i < str_len(line); ++i) {
        if (str_char(line, i) == '#') {
          str_truncate(line, i);
          break;
        }
      }
      // check the line for a single letter followed by a number
      if (regexec_or_die("bg freq", &bgfreq, str_internal(line), 4, matches, 0)) {
        // parse the letter and frequency value
        regex_strncpy(matches+1, str_internal(line), letter, 2);
        freq = regex_dbl(matches+2, str_internal(line));
        // check the frequency is acceptable
        if (freq < 0 || freq > 1) {
          die("The background file lists the illegal probability %g for "
            "the letter %s.\n", freq, letter);
        } else if (freq == 0) {
          die("The background file lists a probability of zero for the "
            "letter %s\n", letter);
        }
        if (freq >= 0 && freq <= 1) rbtree_put(letters, letter, &freq);
      }
      str_clear(line);
    }
  }
  // finished with the file so clean up file parsing stuff
  fclose(fp);
  str_destroy(line, FALSE);
  regfree(&bgfreq);
  // guess the alphabet
  if (*alph == INVALID_ALPH) {
    switch (rbtree_size(letters)) {
      case PROTEIN_ASIZE:
        *alph = PROTEIN_ALPH;
        break;
      case DNA_ASIZE:
        *alph = DNA_ALPH;
        break;
      default:
        die("Number of single character entries in background does not match "
            "an alphabet.\n");
    }
  }
  // make the background
  if (freqs == NULL) freqs = allocate_array(alph_size(*alph, ALL_SIZE));
  assert(get_array_length(freqs) >= alph_size(*alph, ALL_SIZE));
  init_array(-1, freqs);
  for (node = rbtree_first(letters); node != NULL; node = rbtree_next(node)) {
    key = (char*)rbtree_key(node);
    i = alph_index(*alph, key[0]);
    freq = *((double*)rbtree_value(node));
    if (i == -1) {
      die("Background contains letter %s which is not in the %s alphabet.\n", 
          key, alph_name(*alph));
    }
    if (get_array_item(i, freqs) != -1) {
      die("Background contains letter %s which has the same meaning as an "
          "already listed letter.\n", key);
    }
    set_array_item(i, freq, freqs);
  }
  // check that all items were set
  for (i = 0; i < alph_size(*alph, ALPH_SIZE); i++) {
    if (get_array_item(i, freqs) == -1) {
      die("Background is missing letter %c.\n", alph_char(*alph, i));
    }
  }
  // disabled for backwards compatability (AMA test was failing)
  //normalize_subarray(0, ALPH_ASIZE[*alph], 0.0, freqs);
  // calculate the values of the ambiguous letters from the concrete ones
  calc_ambigs(*alph, FALSE, freqs);
  // cleanup
  rbtree_destroy(letters);
  // return result
  return freqs;
}
double Formulaeditor::factor(qint32& nPosition, QString& strCharacter)
{
    qreal f = 0.0;
    qint32 wI = 0, wL = 0, wBeginn = 0, wError = 0;

    if	(strCharacter == str_char(0)) return 0.0;
    // read digit and save as float in f
    if (((strCharacter >= "0") && (strCharacter <= "9")) || (strCharacter == "."))
    {
        wBeginn = nPosition;

        do
        {
            char_n(nPosition, strCharacter);
        }
        while ((((strCharacter >= "0") && (strCharacter <= "9")) || (strCharacter == ".")));

        if (strCharacter == ".")
        {
            do
            {
                char_n(nPosition, strCharacter);
            }
            while (!(((qint8)strCharacter.at(0).digitValue() >= 0) && ((qint8)strCharacter.at(0).digitValue() <=  9))  || (strCharacter.at(0) == '.'));
        }

        QString g_strF = m_strFunction.mid(wBeginn - 1, nPosition - wBeginn);
        f = g_strF.toFloat();
    }
    else
    {
        QString strCharacterUpper = strCharacter.toUpper();
        if (strCharacter == "(")
        {
            char_n(nPosition, strCharacter);
            f = expression(nPosition, strCharacter);
            if (strCharacter == ")")
                char_n(nPosition, strCharacter);
        }
        else if (strCharacterUpper == "X")
        {
            char_n(nPosition, strCharacter);
            f = m_dFktValue;
        }
        else
        {
            bool gefunden = false;
            qint32 AnzStdFunctions = m_strStandardFunction.length() - 1;
            for (wI = 1; wI <= AnzStdFunctions; wI++)
            {
                wL = m_strStandardFunction.at(wI).length();
                QString strFunktionUpper = m_strFunction.mid(nPosition - 1, wL);
                strFunktionUpper = strFunktionUpper.toUpper();
                QString strDummy(m_strStandardFunction.at(wI));
                strDummy = strDummy.toUpper();
                if (strFunktionUpper == strDummy)
                {
                    gefunden = true;
                    nPosition = nPosition + wL - 1;
                    char_n(nPosition, strCharacter);
                    // ! recursion !!!!!!!!!!!!!!!!!!!!!!
                    f = factor(nPosition, strCharacter);
                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    if (strFunktionUpper == "ABS")
                        f = fabs(f);
                    else if (strFunktionUpper == "SQRT")
                        if (f >= 0)
                            f = sqrt(f);
                        else
                            wError = -1;
                    else if (strFunktionUpper == "SINH")
                        f = sinh(f);
                    else if (strFunktionUpper == "COSH")
                        f = cosh(f);
                    else if (strFunktionUpper == "TANH")
                        f = tanh(f);
                    else if (strFunktionUpper == "ARCTAN")
                        f = atan(f);
                    else if (strFunktionUpper == "LN")
                    {
                        if (f >= 0)
                            f = log(f);
                        else
                            wError = -1;
                    }
                    else if (strFunktionUpper == "LOG")
                    {
                        if (f >= 0)
                            f = log10(f);
                        else
                            wError = -1;
                    }
                    else if (strFunktionUpper == "EXP")
                    {
                        //if (f <= 41)
                            f = exp(f);
                        //else
                            //wError = -1;
                    }
                    else if (strFunktionUpper == "SIN")
                        f = sin(f);
                    else if (strFunktionUpper == "COS")
                        f = cos(f);
                    else if (strFunktionUpper == "COT")
                        f = cot(f);
                    else if (strFunktionUpper == "TAN")
                    {
                        if (cos(f) != 0)
                            f = tan(f);
                        else
                            wError = -1;
                    }
                    else if (strFunktionUpper == "ARCSIN")
                    {
                        if (fabs(f) < 1)
                            f = asin(f);
                        else
                            wError = -1;
                    }
                    else if (strFunktionUpper == "ARCCOS")
                    {
                        if (fabs(f) <= 1)
                            f = acos(f);
                        else
                            wError = -1;
                    }
                    else if (strFunktionUpper == "SIGN")
                        f = signl(f);                    
                    else if (strFunktionUpper == "RAD")
                        f = RAD(f);
                    else if (strFunktionUpper == "DEG")
                        f = DEG(f);
                    else if (strFunktionUpper == "ARSINH")
                        f = ArSinh(f);                   
                    else if (strFunktionUpper == "ARCOSH")
                    {
                        if (fabs(f) >= 1)
                            f = ArCosh(f);
                        else
                            wError = -1;
                    }
                    else if (strFunktionUpper == "ARTANH")
                    {
                        if (fabs(f) <= 1)
                            f = ArTanh(f);
                        else
                            wError = -1;
                    }
                    break;
                }
            }
            if (!gefunden)
            {
                char_n(nPosition, strCharacter);
                if (strCharacterUpper == "A")
                    f = m_dFunctionConstant[0];
                else if (strCharacterUpper == "B")
                    f = m_dFunctionConstant[1];
                else if (strCharacterUpper == "C")
                    f = m_dFunctionConstant[2];
                else if (strCharacterUpper == "D")
                    f = m_dFunctionConstant[3];
                else if (strCharacterUpper == "E")
                    f = m_dFunctionConstant[4];
                else if (strCharacterUpper == "F")
                    f = m_dFunctionConstant[5];
                else if (strCharacterUpper == "G")
                    f = m_dFunctionConstant[6];
                else if (strCharacterUpper == "H")
                    f = m_dFunctionConstant[7];
            }
        }
    }

    if (wError == -1)           errorText = QString("General Parser Error blocked!");

    return f;
}
Exemple #7
0
static inline void spxml_next_event(const utf8_t *xml, size_t len, spxml_event_t *evt)
{
	evt->position = xml;
	
	/* Test parameters */
	if (evt == NULL) return;
	if ((xml == NULL) || (len == 0)) {evt->type = SPXMLEVENT_INVALID; return;}	
	
	/* End of file? */
	if (*xml == 0)
	{
		evt->len = 1;
		evt->type = SPXMLEVENT_EOF;
		evt->total_len = evt->len;
		evt->content = evt->position;		
		
		return;
	}
	
	/* No SPXML element? */
	if (*xml != '<')
	{
		/* Search for next element */
		const utf8_t *l__end = str_char(evt->position, '<', len);
		
		if (l__end == NULL)
		{
			evt->type = SPXMLEVENT_INVALID;
		}
		 else
		{
			evt->len = l__end - evt->position;
			evt->total_len = evt->len;
			evt->content = evt->position;
		}
		
		/* Is it a whitespace or EOF content? */
		int l__is_ws = spxml_is_whitespace(evt->position, evt->total_len);

		switch (l__is_ws)
		{
			case 1:
				evt->type = SPXMLEVENT_WHITESPACE;
				break;
			case 2:
				evt->type = SPXMLEVENT_EOF;
				break;
			default:
				evt->type =  SPXMLEVENT_DATA;
		}
			
		return;
	}
	
	evt->len = len;
	
	/* What kind of element do we have? */
	if (spxml_probe_element(evt, SPXMLEVENT_COMMENT)) return;
	if (spxml_probe_element(evt, SPXMLEVENT_PROCESSING)) return;
	if (spxml_probe_element(evt, SPXMLEVENT_END_TAG)) return;
	if (spxml_probe_element(evt, SPXMLEVENT_BEGIN_TAG)) 
	{
		/* Begin tag or empty tag? */
		
		if (evt->content[evt->len - 1] == '/')
		{
			evt->len -= 1;
			evt->type = SPXMLEVENT_EMPTY_TAG;
		}
				
		return;
	}
	
	evt->type = SPXMLEVENT_INVALID;
	
	return;
}
Exemple #8
0
static char* get_filename(
	conf_t *conf,
	knot_db_txn_t *txn,
	const knot_dname_t *zone,
	const char *name)
{
	assert(name);

	const char *end = name + strlen(name);
	char out[1024] = "";

	do {
		// Search for a formatter.
		const char *pos = strchr(name, '%');

		// If no formatter, copy the rest of the name.
		if (pos == NULL) {
			if (strlcat(out, name, sizeof(out)) >= sizeof(out)) {
				CONF_LOG_ZONE(LOG_WARNING, zone, "too long zonefile name");
				return NULL;
			}
			break;
		}

		// Copy constant block.
		char *block = strndup(name, pos - name);
		if (block == NULL ||
		    strlcat(out, block, sizeof(out)) >= sizeof(out)) {
			CONF_LOG_ZONE(LOG_WARNING, zone, "too long zonefile name");
			return NULL;
		}
		free(block);

		// Move name pointer behind the formatter.
		name = pos + 2;

		char buff[512] = "";
		uint8_t idx1, idx2;
		bool failed = false;

		const char type = *(pos + 1);
		switch (type) {
		case '%':
			strlcat(buff, "%", sizeof(buff));
			break;
		case 'c':
			if (get_index(&name, end, &idx1, &idx2) != KNOT_EOK ||
			    str_char(zone, buff, sizeof(buff), idx1, idx2) != KNOT_EOK) {
				failed = true;
			}
			break;
		case 'l':
			if (get_index(&name, end, &idx1, NULL) != KNOT_EOK ||
			    str_label(zone, buff, sizeof(buff), idx1) != KNOT_EOK) {
				failed = true;
			}
			break;
		case 's':
			if (str_zone(zone, buff, sizeof(buff)) != KNOT_EOK) {
				failed = true;
			}
			break;
		case '\0':
			CONF_LOG_ZONE(LOG_WARNING, zone, "ignoring missing "
			              "trailing zonefile formatter");
			continue;
		default:
			CONF_LOG_ZONE(LOG_WARNING, zone, "ignoring zonefile "
			              "formatter '%%%c'", type);
			continue;
		}

		if (failed) {
			CONF_LOG_ZONE(LOG_WARNING, zone, "failed to process "
			              "zonefile formatter '%%%c'", type);
			return NULL;
		}

		if (strlcat(out, buff, sizeof(out)) >= sizeof(out)) {
			CONF_LOG_ZONE(LOG_WARNING, zone, "too long zonefile name");
			return NULL;
		}
	} while (name < end);

	// Use storage prefix if not absolute path.
	if (out[0] == '/') {
		return strdup(out);
	} else {
		conf_val_t val = conf_zone_get_txn(conf, txn, C_STORAGE, zone);
		char *storage = conf_abs_path(&val, NULL);
		if (storage == NULL) {
			return NULL;
		}
		char *abs = sprintf_alloc("%s/%s", storage, out);
		free(storage);
		return abs;
	}
}