Ejemplo n.º 1
0
void ensure_file_size(rollingfile_t* rf)
{
	string_buffer_t bak1;
	string_buffer_t bak2;
	size_t n;
	if(rf->current_size < rf->max_size)	
	{
		if(0 == rf->out)
		{
			rf->out = fopen(rf->nm, "a+");
			
			if(0 == rf->out)
				printf("打开日志文件 '%s' 失败 - %s", rf->nm, strerror(errno));
		}
		return;
	}

	if(0 != rf->out)
	{
		fclose(rf->out);
		rf->out = 0;
	}

	string_buffer_init(&bak1);
	string_buffer_init(&bak2);

	string_buffer_assign(&bak2, rf->nm);
	string_buffer_append_sprintf(&bak2,".%u", rf->remain_count);
	remove(string_data(&bak2));

	for(n = rf->remain_count-1; n > 0; --n)
	{
		string_buffer_assign(&bak1, rf->nm);
		string_buffer_append_sprintf(&bak1,".%u", n);
		if(0 != rename(string_data(&bak1), string_data(&bak2)))
			debugPrintf(string_data(&bak1), string_data(&bak2));
		string_buffer_swap(&bak1, &bak2);
	}
	if(0 != rename(rf->nm, string_data(&bak2)))
	{
		debugPrintf(rf->nm, string_data(&bak2));
	}
	else
	{
		rf->current_size = 0;
		rf->out = fopen(rf->nm, "a+");
		
		if(0 == rf->out)
			printf("打开日志文件 '%s' 失败 - %s", rf->nm, strerror(errno));
	}


	string_buffer_destroy(&bak1);
	string_buffer_destroy(&bak2);
}
Ejemplo n.º 2
0
char *
str_replace (const char *haystack, const char *needle, const char *replacement)
{
    assert (haystack != NULL);
    assert (needle != NULL);
    assert (replacement != NULL);

    string_buffer_t *sb = string_buffer_create ();
    size_t haystack_len = strlen (haystack);
    size_t needle_len = strlen (needle);

    int pos = 0;
    while (pos < haystack_len) {
        if (needle_len > 0 && str_starts_with (&haystack[pos], needle)) {
            string_buffer_append_string (sb, replacement);
            pos += needle_len;
        }
        else {
            string_buffer_append (sb, haystack[pos]);
            pos++;
        }
    }
    if (needle_len == 0 && haystack_len == 0)
        string_buffer_append_string (sb, replacement);

    char *res = string_buffer_to_string (sb);
    string_buffer_destroy (sb);
    return res;
}
Ejemplo n.º 3
0
zarray_t *
str_split (const char *str, const char *delim)
{
    assert (str != NULL);
    assert (delim != NULL);

    zarray_t *parts = zarray_create (sizeof(char*));
    string_buffer_t *sb = string_buffer_create ();

    size_t delim_len = strlen (delim);
    size_t len = strlen (str);
    size_t pos = 0;

    while (pos < len) {
        if (str_starts_with (&str[pos], delim) && delim_len > 0) {
            pos += delim_len;
            // never add empty strings (repeated tokens)
            if (string_buffer_size (sb) > 0) {
                char *part = string_buffer_to_string (sb);
                zarray_add (parts, &part);
            }
            string_buffer_reset (sb);
        }
        else {
            string_buffer_append (sb, str[pos]);
            pos++;
        }
    }

    if (string_buffer_size(sb) > 0) {
        char *part = string_buffer_to_string (sb);
        zarray_add (parts, &part);
    }

    string_buffer_destroy (sb);
    return parts;
}