Example #1
0
File: 06.c Project: yashendaoren/c-
int main()
{
    char s1[100] = "hello ";
    char s2[] = "world";
    
    strlink(s1, s2);
    printf("%s\n", s1);
    
    return 0;
}
Example #2
0
int himd_add_string(struct himd * himd, char *string, int type, struct himderrinfo * status)
{
    int curidx, curtype, i, nextidx;
    int nslots;
    int idx_firstslot;
    gsize length;
    gchar * convertedstring;
    unsigned char * curchunk;
    unsigned char strencoding;

    g_return_val_if_fail(himd != NULL, -1);
    g_return_val_if_fail(string != NULL, -1);


    /* try to use Latin-1 or Shift-JIS. If that fails, use Unicode. */
    if((convertedstring = g_convert(string,-1,"ISO-8859-1","UTF8",
                                    NULL,&length,NULL)) != NULL)
        strencoding = HIMD_ENCODING_LATIN1;
    else if((convertedstring = g_convert(string,-1,"SHIFT_JIS","UTF8",
                                    NULL,&length,NULL)) != NULL)
        strencoding = HIMD_ENCODING_SHIFT_JIS;
    else if((convertedstring = g_convert(string,-1,"UTF-16BE","UTF8",
                                    NULL,&length,NULL)) != NULL)
        strencoding = HIMD_ENCODING_UTF16BE;
    else {
        /* should never happen, as utf-16 can encode anything */
	set_status_printf(status, HIMD_ERROR_UNKNOWN_ENCODING,
			  "can't encode the string '%s' into anything usable",
                          string);
        return -1;
    }

    /* how many number of slots to store string in? */
    nslots = (length+14)/14;	/* +13 for rounding up, +1 for the encoding byte */

    /* check that there are enough free slots. Start at slot 0 which
       is the head of the free list. */
    curidx = 0;
    for(i = 0; i < nslots; i--)
    {
        curtype = strtype(get_strchunk(himd, curidx));
        curidx = strlink(get_strchunk(himd, curidx));
        if(!curidx)
        {
            g_free(convertedstring);
            set_status_printf(status, HIMD_ERROR_OUT_OF_STRINGS,
                "Not enough string space to allocate %d string slots\n", nslots);
            return -1;
        }
        if(curtype != STRING_TYPE_UNUSED)
        {
            g_free(convertedstring);
            set_status_printf(status, HIMD_ERROR_STRING_CHAIN_BROKEN,
                "String slot %d in free list has type %d\n", curidx, curtype);
            return -1;
        }
    }

    idx_firstslot = strlink(get_strchunk(himd, 0));
    curidx = idx_firstslot;
    for(i = 0; i < nslots; i++)
    {
        /* reserve space for the encoding byte in the first slot */
        gsize slotlen = (i != 0) ? 14 : 13;
        gsize stroffset = i*14 - 1;

        /* limit length to what is remaining of the string */
        if(slotlen > length - stroffset)
            slotlen = length - stroffset;

        curchunk = get_strchunk(himd, curidx);
        nextidx  = strlink(curchunk);
        if(i == 0)
        {
            curchunk[0] = strencoding;
            memcpy(curchunk + 1, convertedstring, slotlen);
            set_strtype(curchunk, type);
        }
        else
        {
            memcpy(curchunk, convertedstring + stroffset, slotlen);
            set_strtype(curchunk, STRING_TYPE_CONTINUATION);
        }
        if(i == nslots-1)
            set_strlink(curchunk, 0);
        curidx = nextidx;
    }

    /* adjust free list head pointer */
    set_strlink(get_strchunk(himd, 0), curidx);
    g_free(convertedstring);

    return idx_firstslot;
}
Example #3
0
char* himd_get_string_raw(struct himd * himd, unsigned int idx, int*type, int* length, struct himderrinfo * status)
{
    int curidx;
    int len;
    char * rawstr;
    int actualtype;

    g_return_val_if_fail(himd != NULL, NULL);
    g_return_val_if_fail(idx >= 1, NULL);
    g_return_val_if_fail(idx < 4096, NULL);
    
    actualtype = strtype(get_strchunk(himd,idx));
    /* Not the head of a string */
    if(actualtype < 8)
    {
        set_status_printf(status, HIMD_ERROR_NOT_STRING_HEAD,
                   _("String table entry %d is not a head: Type %d"),
                   idx,actualtype);
        return NULL;
    }
    if(type != NULL)
        *type = actualtype;

    /* Get length of string */
    len = 1;
    for(curidx = strlink(get_strchunk(himd,idx)); curidx != 0; 
          curidx = strlink(get_strchunk(himd,curidx)))
    {
        if(strtype(get_strchunk(himd,curidx)) != STRING_TYPE_CONTINUATION)
        {
            set_status_printf(status, HIMD_ERROR_STRING_CHAIN_BROKEN,
                       _("%dth entry in string chain starting at %d has type %d"),
                       len+1,idx,strtype(get_strchunk(himd,curidx)));
            return NULL;
        }
        len++;
        if(len >= 4096)
        {
            set_status_printf(status, HIMD_ERROR_STRING_CHAIN_BROKEN,
                       _("string chain starting at %d loops"),idx);
            return NULL;
        }
    }

    /* collect fragments */
    rawstr = g_malloc(len*14);
    if(!rawstr)
    {
        set_status_printf(status, HIMD_ERROR_OUT_OF_MEMORY,
                   _("Can't allocate %d bytes for raw string (string idx %d)"),
                   len, idx);
        return NULL;
    }

    len = 0;
    for(curidx = idx; curidx != 0; 
          curidx = strlink(get_strchunk(himd,curidx)))
    {
        memcpy(rawstr+len*14,get_strchunk(himd,curidx),14);
        len++;
    }

    *length = 14*len;
    return rawstr;
}