📄 mxml-file.c
字号:
_mxml_global_t *global = _mxml_global(); /* Global data */ global->error_cb = cb;}/* * 'mxmlSetWrapMargin()' - Set the the wrap margin when saving XML data. * * Wrapping is disabled when "column" is <= 0. * * @since Mini-XML 2.3@ */voidmxmlSetWrapMargin(int column) /* I - Column for wrapping */{ _mxml_global_t *global = _mxml_global(); /* Global data */ if (column <= 0) global->wrap = 2147483647; else global->wrap = column;}/* * 'mxml_add_char()' - Add a character to a buffer, expanding as needed. */static int /* O - 0 on success, -1 on error */mxml_add_char(int ch, /* I - Character to add */ char **bufptr, /* IO - Current position in buffer */ char **buffer, /* IO - Current buffer */ int *bufsize) /* IO - Current buffer size */{ char *newbuffer; /* New buffer value */ if (*bufptr >= (*buffer + *bufsize - 4)) { /* * Increase the size of the buffer... */ if (*bufsize < 1024) (*bufsize) *= 2; else (*bufsize) += 1024; if ((newbuffer = realloc(*buffer, *bufsize)) == NULL) { free(*buffer); mxml_error("Unable to expand string buffer to %d bytes!", *bufsize); return (-1); } *bufptr = newbuffer + (*bufptr - *buffer); *buffer = newbuffer; } if (ch < 0x80) { /* * Single byte ASCII... */ *(*bufptr)++ = ch; } else if (ch < 0x800) { /* * Two-byte UTF-8... */ *(*bufptr)++ = 0xc0 | (ch >> 6); *(*bufptr)++ = 0x80 | (ch & 0x3f); } else if (ch < 0x10000) { /* * Three-byte UTF-8... */ *(*bufptr)++ = 0xe0 | (ch >> 12); *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f); *(*bufptr)++ = 0x80 | (ch & 0x3f); } else { /* * Four-byte UTF-8... */ *(*bufptr)++ = 0xf0 | (ch >> 18); *(*bufptr)++ = 0x80 | ((ch >> 12) & 0x3f); *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f); *(*bufptr)++ = 0x80 | (ch & 0x3f); } return (0);}/* * 'mxml_fd_getc()' - Read a character from a file descriptor. */static int /* O - Character or EOF */mxml_fd_getc(void *p, /* I - File descriptor buffer */ int *encoding) /* IO - Encoding */{ _mxml_fdbuf_t *buf; /* File descriptor buffer */ int ch, /* Current character */ temp; /* Temporary character */ /* * Grab the next character in the buffer... */ buf = (_mxml_fdbuf_t *)p; if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); ch = *(buf->current)++; switch (*encoding) { case ENCODE_UTF8 : /* * Got a UTF-8 character; convert UTF-8 to Unicode and return... */ if (!(ch & 0x80)) {#if DEBUG > 1 printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);#endif /* DEBUG > 1 */ if (mxml_bad_char(ch)) { mxml_error("Bad control character 0x%02x not allowed by XML standard!", ch); return (EOF); } return (ch); } else if (ch == 0xfe) { /* * UTF-16 big-endian BOM? */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); ch = *(buf->current)++; if (ch != 0xff) return (EOF); *encoding = ENCODE_UTF16BE; return (mxml_fd_getc(p, encoding)); } else if (ch == 0xff) { /* * UTF-16 little-endian BOM? */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); ch = *(buf->current)++; if (ch != 0xfe) return (EOF); *encoding = ENCODE_UTF16LE; return (mxml_fd_getc(p, encoding)); } else if ((ch & 0xe0) == 0xc0) { /* * Two-byte value... */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; if ((temp & 0xc0) != 0x80) return (EOF); ch = ((ch & 0x1f) << 6) | (temp & 0x3f); if (ch < 0x80) return (EOF); } else if ((ch & 0xf0) == 0xe0) { /* * Three-byte value... */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; if ((temp & 0xc0) != 0x80) return (EOF); ch = ((ch & 0x0f) << 6) | (temp & 0x3f); if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; if ((temp & 0xc0) != 0x80) return (EOF); ch = (ch << 6) | (temp & 0x3f); if (ch < 0x800) return (EOF); } else if ((ch & 0xf8) == 0xf0) { /* * Four-byte value... */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; if ((temp & 0xc0) != 0x80) return (EOF); ch = ((ch & 0x07) << 6) | (temp & 0x3f); if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; if ((temp & 0xc0) != 0x80) return (EOF); ch = (ch << 6) | (temp & 0x3f); if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; if ((temp & 0xc0) != 0x80) return (EOF); ch = (ch << 6) | (temp & 0x3f); if (ch < 0x10000) return (EOF); } else return (EOF); break; case ENCODE_UTF16BE : /* * Read UTF-16 big-endian char... */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; ch = (ch << 8) | temp; if (mxml_bad_char(ch)) { mxml_error("Bad control character 0x%02x not allowed by XML standard!", ch); return (EOF); } else if (ch >= 0xd800 && ch <= 0xdbff) { /* * Multi-word UTF-16 char... */ int lch; if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); lch = *(buf->current)++; if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; lch = (lch << 8) | temp; if (lch < 0xdc00 || lch >= 0xdfff) return (EOF); ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000; } break; case ENCODE_UTF16LE : /* * Read UTF-16 little-endian char... */ if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; ch |= (temp << 8); if (mxml_bad_char(ch)) { mxml_error("Bad control character 0x%02x not allowed by XML standard!", ch); return (EOF); } else if (ch >= 0xd800 && ch <= 0xdbff) { /* * Multi-word UTF-16 char... */ int lch; if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); lch = *(buf->current)++; if (buf->current >= buf->end) if (mxml_fd_read(buf) < 0) return (EOF); temp = *(buf->current)++; lch |= (temp << 8); if (lch < 0xdc00 || lch >= 0xdfff) return (EOF); ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000; } break; }#if DEBUG > 1 printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);#endif /* DEBUG > 1 */ return (ch);}/* * 'mxml_fd_putc()' - Write a character to a file descriptor. */static int /* O - 0 on success, -1 on error */mxml_fd_putc(int ch, /* I - Character */ void *p) /* I - File descriptor buffer */{ _mxml_fdbuf_t *buf; /* File descriptor buffer */ /* * Flush the write buffer as needed - note above that "end" still leaves * 4 characters at the end so that we can avoid a lot of extra tests... */ buf = (_mxml_fdbuf_t *)p; if (buf->current >= buf->end) if (mxml_fd_write(buf) < 0) return (-1); if (ch < 0x80) { /* * Write ASCII character directly... */ *(buf->current)++ = ch; } else if (ch < 0x800) { /* * Two-byte UTF-8 character... */ *(buf->current)++ = 0xc0 | (ch >> 6); *(buf->current)++ = 0x80 | (ch & 0x3f); } else if (ch < 0x10000) { /* * Three-byte UTF-8 character... */ *(buf->current)++ = 0xe0 | (ch >> 12); *(buf->current)++ = 0x80 | ((ch >> 6) & 0x3f); *(buf->current)++ = 0x80 | (ch & 0x3f); } else { /* * Four-byte UTF-8 character... */ *(buf->current)++ = 0xf0 | (ch >> 18); *(buf->current)++ = 0x80 | ((ch >> 12) & 0x3f); *(buf->current)++ = 0x80 | ((ch >> 6) & 0x3f); *(buf->current)++ = 0x80 | (ch & 0x3f); } /* * Return successfully... */ return (0);}/* * 'mxml_fd_read()' - Read a buffer of data from a file descriptor. */static int /* O - 0 on success, -1 on error */mxml_fd_read(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */{ int bytes; /* Bytes read... */ /* * Range check input... */ if (!buf) return (-1); /* * Read from the file descriptor... */ while ((bytes = read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)#ifdef EINTR if (errno != EAGAIN && errno != EINTR)#else if (errno != EAGAIN)#endif /* EINTR */ return (-1); if (bytes == 0) return (-1); /* * Update the pointers and return success... */ buf->current = buf->buffer; buf->end = buf->buffer + bytes; return (0);}/* * 'mxml_fd_write()' - Write a buffer of data to a file descriptor. */static int /* O - 0 on success, -1 on error */mxml_fd_write(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */{ int bytes; /* Bytes written */ unsigned char *ptr; /* Pointer into buffer */ /* * Range check... */ if (!buf) return (-1); /* * Return 0 if there is nothing to write... */ if (buf->current == buf->buffer) return (0); /* * Loop until we have written everything... */ for (ptr = buf->buffer; ptr < buf->current; ptr += bytes) if ((bytes = write(buf->fd, ptr, buf->current - ptr)) < 0) return (-1); /* * All done, reset pointers and return success... */ buf->current = buf->buffer; return (0);}/* * 'mxml_file_getc()' - Get a character from a file. */static int /* O - Character or EOF */mxml_file_getc(void *p, /* I - Pointer to file */ int *encoding) /* IO - Encoding */{ int ch, /* Character from file */ temp; /* Temporary character */ FILE *fp; /* Pointer to file */ /* * Read a character from the file and see if it is EOF or ASCII... */ fp = (FILE *)p; ch = getc(fp); if (ch == EOF) return (EOF); switch (*encoding) { case ENCODE_UTF8 : /* * Got a UTF-8 character; convert UTF-8 to Unicode and return... */ if (!(ch & 0x80)) { if (mxml_bad_char(ch)) { mxml_error("Bad control character 0x%02x not allowed by XML standard!", ch); return (EOF); }#if DEBUG > 1 printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);#endif /* DEBUG > 1 */ return (ch); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -