Ejemplo n.º 1
0
int sn_encoded_input(char *buf, int max_size)
{
	char *rawbuf, *utf8buf;
	int srcRead, dstWrote, nbytes, flags = 0;

	static Tcl_EncodingState utf8_state, ascii_state;

	if(encoding == NULL) {
		/*
		 No translation necessary. Just look for CRLF
		 sequences and remove the CR character.
		 */
		size_t read = fread(buf, sizeof(char), max_size, yyin);
		read -= translate_crlf(buf, read);
		return read;
	}

	if(ascii == NULL) {
		ascii = Tcl_GetEncoding(NULL, "ascii");
		if(ascii == NULL) {
			fprintf(stderr, "Unable to locate `ascii' encoding\n");
			return 0;
		}
	}

	if(start_of_file) {
		flags |= TCL_ENCODING_START;
	}

	if((rawbuf = (char *) ckalloc(max_size)) == NULL) {
		/* Insufficient memory. */
		return 0;
	}

	/* FIXME: This ought to do it. */
	if((utf8buf = (char *) ckalloc(2 * max_size)) == NULL) {
		/* Insufficient memory. */
		return 0;
	}

	/* Read max_size bytes from disk. */
	nbytes = fread(rawbuf, sizeof(unsigned char), sizeof(rawbuf), yyin);
	if(nbytes == 0) {
		/*
		 Continue on with an empty buffer; this allows the Tcl
		 encoding routines to do any necessary finalisation.
		 See the Encoding(n) man page.
		 */
		flags = TCL_ENCODING_END;
	}

	/* Translate encoded file data into UTF-8. */
	Tcl_ExternalToUtf(NULL, encoding, rawbuf, nbytes, flags,
			  &utf8_state, utf8buf, 2 * max_size,
			  &srcRead, &dstWrote, NULL);

	/* Look for CRLF sequences and remove the CR characters */
	dstWrote -= translate_crlf(utf8buf, dstWrote);


	/*
	 FIXME This code assumes that an encoded stream `n' bytes long
	 will always reduce down to an ASCII stream no longer than `n'
	 bytes. This is a reasonable assumption, but probably not
	 foolproof.
	 */

	/* Translate this from UTF-8 to ASCII. */
	Tcl_UtfToExternal(NULL, ascii, utf8buf, dstWrote, flags,
			  &ascii_state, buf, max_size,
			  &srcRead, &dstWrote, NULL);

	if(dstWrote > 0 && start_of_file) {
		start_of_file = 0;
	}

	ckfree(utf8buf);
	ckfree(rawbuf);

	return dstWrote;
}
Ejemplo n.º 2
0
Pixmap
TkpGetNativeAppBitmap(
    Display *display,		/* The display. */
    CONST char *name,		/* The name of the bitmap. */
    int *width,			/* The width & height of the bitmap. */
    int *height)
{
    Pixmap pix;
    CGrafPtr savePort;
    Boolean portChanged;
    Rect destRect;
    Handle resource;
    int type = -1, destWrote;
    Str255 nativeName;
    Tcl_Encoding encoding;

    /*
     * macRoman is the encoding that the resource fork uses.
     */

    encoding = Tcl_GetEncoding(NULL, "macRoman");
    Tcl_UtfToExternal(NULL, encoding, name, strlen(name), 0, NULL,
	    (char *) &nativeName[1], 255, NULL, &destWrote, NULL);
    nativeName[0] = destWrote;
    Tcl_FreeEncoding(encoding);

    resource = GetNamedResource('cicn', nativeName);
    if (resource != NULL) {
	type = TYPE3;
    } else {
	resource = GetNamedResource('ICON', nativeName);
	if (resource != NULL) {
	    type = TYPE2;
	}
    }

    if (resource == NULL) {
	return (Pixmap) NULL;
    }

    pix = Tk_GetPixmap(display, None, 32, 32, 0);
    portChanged = QDSwapPort(TkMacOSXGetDrawablePort(pix), &savePort);

    SetRect(&destRect, 0, 0, 32, 32);
    if (type == TYPE2) {
	RGBColor black = {0, 0, 0};

	RGBForeColor(&black);
	PlotIcon(&destRect, resource);
	ReleaseResource(resource);
    } else if (type == TYPE3) {
	RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
	short id;
	ResType theType;
	Str255 dummy;

	/*
	 * We need to first paint the background white. Also, for some reason
	 * we *must* use GetCIcon instead of GetNamedResource for PlotCIcon to
	 * work - so we use GetResInfo to get the id.
	 */

	RGBForeColor(&white);
	PaintRect(&destRect);
	GetResInfo(resource, &id, &theType, dummy);
	ReleaseResource(resource);
	resource = (Handle) GetCIcon(id);
	PlotCIcon(&destRect, (CIconHandle) resource);
	DisposeCIcon((CIconHandle) resource);
    }

    *width = 32;
    *height = 32;
    if (portChanged) {
	QDSwapPort(savePort, NULL);
    }
    return pix;
}