示例#1
0
文件: dict.c 项目: HarmtH/vim
/*
 * Go over all entries in "d2" and add them to "d1".
 * When "action" is "error" then a duplicate key is an error.
 * When "action" is "force" then a duplicate key is overwritten.
 * Otherwise duplicate keys are ignored ("action" is "keep").
 */
    void
dict_extend(dict_T *d1, dict_T *d2, char_u *action)
{
    dictitem_T	*di1;
    hashitem_T	*hi2;
    int		todo;
    char_u	*arg_errmsg = (char_u *)N_("extend() argument");

    todo = (int)d2->dv_hashtab.ht_used;
    for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
    {
	if (!HASHITEM_EMPTY(hi2))
	{
	    --todo;
	    di1 = dict_find(d1, hi2->hi_key, -1);
	    if (d1->dv_scope != 0)
	    {
		/* Disallow replacing a builtin function in l: and g:.
		 * Check the key to be valid when adding to any scope. */
		if (d1->dv_scope == VAR_DEF_SCOPE
			&& HI2DI(hi2)->di_tv.v_type == VAR_FUNC
			&& var_check_func_name(hi2->hi_key, di1 == NULL))
		    break;
		if (!valid_varname(hi2->hi_key))
		    break;
	    }
	    if (di1 == NULL)
	    {
		di1 = dictitem_copy(HI2DI(hi2));
		if (di1 != NULL && dict_add(d1, di1) == FAIL)
		    dictitem_free(di1);
	    }
	    else if (*action == 'e')
	    {
		EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
		break;
	    }
	    else if (*action == 'f' && HI2DI(hi2) != di1)
	    {
		if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
		      || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
		    break;
		clear_tv(&di1->di_tv);
		copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
	    }
	}
    }
}
示例#2
0
文件: imgtogba.c 项目: wzzt/imgtogba
int main(int argc, char *argv[])
{
    if (argc < 3) {
        fprintf(stderr,
            "Converts a gif/png/jpg to a c array of a 16-bit bitmap, "
            "format {x, y, data}\n"
            "Need var name as first arg, valid image file as second arg\n"
            "(Compact mode on if third arg is 1)");
        return -1;
    }

    if (!valid_varname(VARNAME)) {
        fprintf(stderr, "Invalid C varname %s ", VARNAME);
        return -2;
    }

    FILE *imgdata;
    gdImagePtr img = NULL;
    char header[4];
    int w, h;
    int x, y;
    int i;
    int type, color, rgb;
    int compact = atoi(argv[3]);

    imgdata = fopen(FILENAME, "rb");

    if (!imgdata)
    { fprintf(stderr, "File %s read error: does not exist?\n", FILENAME); return -4; }

    img = gdImageCreateFromJpeg(imgdata);
    if (!img)
    img = gdImageCreateFromGif(imgdata);
    if (!img)
    img = gdImageCreateFromPng(imgdata);
    
    if (!img) {
        fprintf(stderr, "File %s is invalid %s\n", FILENAME,
                type == 0 ? "JPG" : type == 1 ?
                "GIF" : type == 2 ? "PNG" : "image");
        return -5;
    }

    fclose(imgdata);

    w = img->sx;
    h = img->sy;

    printf("/* 16-bit GBA bitmap array generated by imgtogba */\n\n");
    printf("const unsigned short %s[] = {\n/* xdim  ydim, */\n", VARNAME);
    printf("   %4d, %4d\n", w, h);

    if (compact == 1) {
        for (y = i = 0; y < h; ++y) {
            for (x = 0; x < w; ++x) {
                color = gdImageGetPixel(img, x, y);
                rgb = rgb_conv(
                        gdImageRed(img, color),
                        gdImageGreen(img, color),
                        gdImageBlue(img, color)
                        );
                printf(",%d", rgb);
            }
        }
    } else {
        for (y = i = 0; y < h; ++y) {
            for (x = 0; x < w; ++x) {
                color = gdImageGetPixel(img, x, y);
                rgb = rgb_conv(
                        gdImageRed(img, color),
                        gdImageGreen(img, color),
                        gdImageBlue(img, color)
                        );
                printf("%s0x%04x", (i++ % 10 ? ", " : ",\n"), rgb);
            }
        }
    }

    printf("};");
    gdImageDestroy(img);
    return 0;
}