Example #1
0
int
display_init(struct statics * statics)
{
	int display_lines, *ip, i;
	char **pp;

	if (smart_terminal) {
		addstrp = addstr;
		printwp = (int(*)(const char *, ...))printw;
		standoutp = standout;
		standendp = standend;
	} else {
		addstrp = myfputs;
		printwp = printf;
		standoutp = empty;
		standendp = empty;
	}

	/* call resize to do the dirty work */
	display_lines = display_resize();

	/* only do the rest if we need to */
	/* save pointers and allocate space for names */
	procstate_names = statics->procstate_names;

	cpustate_names = statics->cpustate_names;
	num_cpustates = string_count(cpustate_names);
	
	cpustate_columns = calloc(num_cpustates, sizeof(int));
	if (cpustate_columns == NULL)
		err(1, NULL);

	memory_names = statics->memory_names;

	/* calculate starting columns where needed */
	cpustate_total_length = 0;
	pp = cpustate_names;
	ip = cpustate_columns;
	while (*pp != NULL) {
		if ((i = strlen(*pp++)) > 0) {
			*ip++ = cpustate_total_length;
			cpustate_total_length += i + 8;
		}
	}

	/* return number of lines available */
	return (display_lines);
}
Example #2
0
void string_replace(StringType &dst, const StringType &src, const StringType &old_str, const StringType &new_str)
{

  if (old_str.size() == 0 || old_str.size() > src.size()) {
    /* Just copy -- there's nothing to replace */
    dst = src;
  }
  else if (old_str.size() == new_str.size()) {
    /* Special case when old_str and new_str are same length,
       we copy src to dst and the replace in-place. */
    dst = src;

    if (old_str.size() == 1) {
      /* Special case when old_str and new_str are both 1 character */
      char old_chr = old_str.begin()[0];
      char new_chr = new_str.begin()[0];
      for (auto p = dst.begin(); p != dst.end(); ++p) {
        if (*p == old_chr) {
          *p = new_chr;
        }
      }
    }
    else {
      detail::string_inplace_replacer<StringType> replacer(dst, new_str);
      detail::string_search(src, old_str, replacer);
    }
  }
  else {
    /* Most general case, where old_str and new_str are different
       lengths.  Count matches to determine resulting string length,
       then interleave to make the result. */
    intptr_t count = string_count(src, old_str);
    size_t delta = ((intptr_t)new_str.size() - (intptr_t)old_str.size()) * count;

    dst.resize((intptr_t)src.size() + delta);

    detail::string_copy_replacer<StringType> replacer(dst, src, old_str, new_str);
    detail::string_search(src, old_str, replacer);
    replacer.finish();
  }
}
Example #3
0
/* Compute the relative complexity of a pattern */
static int complexity(const grok_t *grok) {
  int score;
  score += string_count(grok->full_pattern, "|");
  score += strlen(grok->full_pattern) / 2;
  return -score; /* Sort most-complex first */
}