⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.c

📁 LINUX下的收发E-MAIL的程序
💻 C
字号:


#include "parse.h"


char *strstrcase(char *haystack, char *needle)
{
    // find the needle in the haystack neglecting case 
    char *ptr;
    unsigned int len;

    if (needle == (char *) NULL || haystack == (char *) NULL)
	return NULL;

    len = strlen(needle);
    if (len > strlen(haystack))
	return NULL;

    for (ptr = haystack; *(ptr + len - 1) != '\0'; ptr++)
	if (!strncasecmp(ptr, needle, len))
	    return ptr;
    return NULL;
}


void strninsert(char *string, char *insertstr, unsigned int pos,
		size_t strmax)
{
    // inserts one string into another string starting at position pos. 
    char tempstr[strmax], *tstrptr;	// temp-string and it's pointer 
    char *strptr, *istrptr;	// string pointer, insert-string pointer 
    unsigned int i = 0;		// our counter 

    // is there a string to insert? 
    if (insertstr == (char *) NULL)
	return;

    // lets make sure our string isn't NULL 
    if (string == (char *) NULL && pos != 0)
	return;
    else if (string == (char *) NULL && pos == 0) {
	// we're basically creating a new string... 
	strncpy(string, insertstr, strmax);
    }
    // out-of-bounds condition 
    if (pos >= strmax || pos > strlen(string))
	return;

    // We are now left with the condition that we have a string and we have
    // an insert string (non-NULL) and the insertion point is within bounds 

    memset(tempstr, 0, strmax);

    // copy the string to to the point of insertion 
    for (strptr = string, tstrptr = tempstr; i < pos;
	 strptr++, tstrptr++, i++)
	*tstrptr = *strptr;

    // copy the instert-string into place 
    for (istrptr = insertstr; *istrptr != '\0' && i < strmax - 1;
	 istrptr++, tstrptr++, i++)
	*tstrptr = *istrptr;

    // copy the remainder of the original string into the temp string 
    for (; *strptr != '\0' && i < strmax - 1; strptr++, tstrptr++, i++)
	*tstrptr = *strptr;
    *tstrptr = '\0';		// NULL-terminate the temp string 

    memset(string, 0, strmax);
    strncpy(string, tempstr, strmax);
}


void strcut(char *string, unsigned int index, unsigned int numchars)
{
    // cuts a specific number of chars from a string starting at the position index 
    char *ptr, *strptr;
    unsigned int i = 0;

    // we can't cut from a NULL string 
    if (string == (char *) NULL)
	return;

    // out-of-bounds condition 
    if (index >= strlen(string) || index + numchars > strlen(string))
	return;

    // advance to the cutting-point 
    for (strptr = string; *strptr != '\0' && i < index; strptr++, i++);

    ptr = strptr;		// strptr points to the cutting point 
    ptr += numchars;		// ptr points to the end of the cut 

    // copy the portion after the cut 
    for (; *ptr != '\0'; ptr++, strptr++)
	*strptr = *ptr;
    *strptr = '\0';
}


int replace_string(char *line, char *remove, char *replace)
{
    // replaces the first occurence of remove with replace in line.
    // Returns TRUE on success and FALSE on failure (meaning that
    // `remove` could not be found within the string
    int index;

    index = find_string(line, remove);
    if (index == -1)
	return FALSE;
    strcut(line, index, strlen(remove));
    strninsert(line, replace, index, sizeof(line));
    return TRUE;
}


void trim_leadspc(char *string)
{
    // delete the leading white spaces from the string
    char *strt;
    char *ptr1, *ptr2;

    if (string == (char *) NULL)
	return;

    // find the first non-space and point to it with 'strt' 
    for (strt = string; *strt != '\0'; strt++)
	if (*strt != ' ' && *strt != '\t')
	    break;

    // copy the string beginning at 'strt' into 'string' 
    for (ptr1 = string, ptr2 = strt; *ptr2 != '\0'; ptr1++, ptr2++)
	*ptr1 = *ptr2;
    *ptr1 = '\0';
}


void trim_tailspc(char *string)
{
    // delete the trailing white spaces from the string 
    char *end;

    if (string == (char *) NULL)
	return;			// oops, someone sent us a NULL string 

    // find the end of the string...
    for (end = string; *end != '\0'; end++);
// 'end' should now be pointing to the end of the string 

    end--;

    // move backwards until we hit the first non-space char 
    for (; end != string; end--)
	if (*end != ' ' && *end != '\t' && *end != '\n')
	    break;

    // 'end' should now be pointing to the last char in the string

    end++;			// move forward one space
    *end = '\0';		// end the string
}


void trim_whtspc(char *string)
{
    // delete the leading/trailing white space from the string 
    trim_tailspc(string);
    trim_leadspc(string);
    return;
}


unsigned int get_num_fields(char *string, char delim)
{
    // returns the number of fields in a string
    // assumes that the delimeter does not appear
    // at the beginning nor the end of the string.
    unsigned int count = 1;
    char *ptr;

    trim_whtspc(string);

    if (string == (char *) NULL || *string == '\0')
	return 0;

    for (ptr = string; *ptr != '\0'; ptr++)
	if (*ptr == delim)
	    count++;

    return count;
}


char *get_field(char *string, unsigned int field, char delim)
{
    char *buffer = (char *) NULL;
    char *start, *ptr1;
    unsigned int cfield = 1;

    // Validate function inputs.
    if (string == (char *) NULL || *string == '\0')
	return NULL;

    if (field <= 0)
	return NULL;

    // set the starting position to the beginng of the string 
    start = string;

    // Already on the field we want...? 
    if (cfield != field) {
	for (; *start != '\0'; start++)	// keep advancing chars... 
	    if (*start == delim)	// when we hit a delimiter... 
	    {
		cfield++;	// increment the current field 
		if (cfield >= field)
		    break;	// at the right field 
	    }
    }

    if (field != 1)
	start++;		// advace to the char after the delimiting char 

    // copy the beginning of the new string into a buffer so we
    // won't mess up the original string when we capture the end
    // of the string in this field. 
    buffer = g_malloc0(strlen(start) + 1);
    strcpy(buffer, start);

    // keept advancing chars until we get to the next delimiter 
    for (ptr1 = buffer; (*ptr1 != '\0') && (*ptr1 != delim); ptr1++);

    *ptr1 = '\0';		// end the string 

    return buffer;
}

// rfc1522 says that if you want 8bit chars in the headers
// you have to encode it in a special way:
// =?charset?encoding?text?=
// the text goes into ?text? with the 8bit characters escaped
// to =<hexcode> , for example =F6 would be 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -