📄 reader.c
字号:
/*
* WINE RTF file reader
*
* Portions Copyright 2004 Mike McCormack for CodeWeavers
* Portions Copyright 2006 by Phil Krylov
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Derived from RTF Tools by Paul DuBois (dubois@primate.wisc.edu)
* Homepage: http://www.snake.net/software/RTF/
* Original license follows:
*/
/*
* reader.c - RTF file reader. Release 1.10.
*
* ....
*
* Author: Paul DuBois dubois@primate.wisc.edu
*
* This software may be redistributed without restriction and used for
* any purpose whatsoever.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
#include "windef.h"
#include "winbase.h"
#include "wine/debug.h"
#include "editor.h"
#include "rtf.h"
WINE_DEFAULT_DEBUG_CHANNEL(richedit);
extern HANDLE me_heap;
static int _RTFGetChar(RTF_Info *);
static void _RTFGetToken (RTF_Info *);
static void _RTFGetToken2 (RTF_Info *);
static int GetChar (RTF_Info *);
static void ReadFontTbl (RTF_Info *);
static void ReadColorTbl (RTF_Info *);
static void ReadStyleSheet (RTF_Info *);
static void ReadInfoGroup (RTF_Info *);
static void ReadPictGroup (RTF_Info *);
static void ReadObjGroup (RTF_Info *);
static void Lookup (RTF_Info *, char *);
static int Hash (const char *);
static void CharAttr(RTF_Info *info);
static void CharSet(RTF_Info *info);
static void DocAttr(RTF_Info *info);
static void RTFFlushCPOutputBuffer(RTF_Info *info);
static void RTFPutCodePageChar(RTF_Info *info, int c);
/* ---------------------------------------------------------------------- */
/*
* Memory allocation routines
*/
/*
* Return pointer to block of size bytes, or NULL if there's
* not enough memory available.
*/
#define RTFAlloc(size) richedit_alloc(size)
#define RTFReAlloc(ptr, size) richedit_realloc(ptr, size)
#define RTFFree(ptr) richedit_free(ptr)
/*
* Saves a string on the heap and returns a pointer to it.
*/
static inline char *RTFStrSave(char *s)
{
char *p;
p = RTFAlloc (lstrlenA(s) + 1);
if (p == NULL)
return NULL;
return lstrcpyA (p, s);
}
/* ---------------------------------------------------------------------- */
int _RTFGetChar(RTF_Info *info)
{
int ch;
ME_InStream *stream = info->stream;
if (stream->dwSize <= stream->dwUsed)
{
ME_StreamInFill(stream);
/* if error, it's EOF */
if (stream->editstream->dwError)
return EOF;
/* if no bytes read, it's EOF */
if (stream->dwSize == 0)
return EOF;
}
ch = stream->buffer[stream->dwUsed++];
if (!ch)
return EOF;
return ch;
}
void RTFSetEditStream(RTF_Info *info, ME_InStream *stream)
{
info->stream = stream;
}
static void
RTFDestroyAttrs(RTF_Info *info)
{
RTFColor *cp;
RTFFont *fp;
RTFStyle *sp;
RTFStyleElt *eltList, *ep;
while (info->fontList)
{
fp = info->fontList->rtfNextFont;
RTFFree (info->fontList->rtfFName);
RTFFree (info->fontList);
info->fontList = fp;
}
while (info->colorList)
{
cp = info->colorList->rtfNextColor;
RTFFree (info->colorList);
info->colorList = cp;
}
while (info->styleList)
{
sp = info->styleList->rtfNextStyle;
eltList = info->styleList->rtfSSEList;
while (eltList)
{
ep = eltList->rtfNextSE;
RTFFree (eltList->rtfSEText);
RTFFree (eltList);
eltList = ep;
}
RTFFree (info->styleList->rtfSName);
RTFFree (info->styleList);
info->styleList = sp;
}
}
void
RTFDestroy(RTF_Info *info)
{
if (info->rtfTextBuf)
{
RTFFree(info->rtfTextBuf);
RTFFree(info->pushedTextBuf);
}
RTFDestroyAttrs(info);
RTFFree(info->cpOutputBuffer);
}
/*
* Initialize the reader. This may be called multiple times,
* to read multiple files. The only thing not reset is the input
* stream; that must be done with RTFSetStream().
*/
void RTFInit(RTF_Info *info)
{
int i;
if (info->rtfTextBuf == NULL) /* initialize the text buffers */
{
info->rtfTextBuf = RTFAlloc (rtfBufSiz);
info->pushedTextBuf = RTFAlloc (rtfBufSiz);
if (info->rtfTextBuf == NULL || info->pushedTextBuf == NULL)
ERR ("Cannot allocate text buffers.\n");
info->rtfTextBuf[0] = info->pushedTextBuf[0] = '\0';
}
RTFFree (info->inputName);
RTFFree (info->outputName);
info->inputName = info->outputName = NULL;
for (i = 0; i < rtfMaxClass; i++)
RTFSetClassCallback (info, i, NULL);
for (i = 0; i < rtfMaxDestination; i++)
RTFSetDestinationCallback (info, i, NULL);
/* install built-in destination readers */
RTFSetDestinationCallback (info, rtfFontTbl, ReadFontTbl);
RTFSetDestinationCallback (info, rtfColorTbl, ReadColorTbl);
RTFSetDestinationCallback (info, rtfStyleSheet, ReadStyleSheet);
RTFSetDestinationCallback (info, rtfInfo, ReadInfoGroup);
RTFSetDestinationCallback (info, rtfPict, ReadPictGroup);
RTFSetDestinationCallback (info, rtfObject, ReadObjGroup);
RTFSetReadHook (info, NULL);
/* dump old lists if necessary */
RTFDestroyAttrs(info);
info->ansiCodePage = 1252; /* Latin-1; actually unused */
info->unicodeLength = 1; /* \uc1 is the default */
info->codePage = info->ansiCodePage;
info->defFont = 0;
info->rtfClass = -1;
info->pushedClass = -1;
info->pushedChar = EOF;
info->rtfLineNum = 0;
info->rtfLinePos = 0;
info->prevChar = EOF;
info->bumpLine = 0;
info->dwCPOutputCount = 0;
if (!info->cpOutputBuffer)
{
info->dwMaxCPOutputCount = 0x1000;
info->cpOutputBuffer = RTFAlloc(info->dwMaxCPOutputCount);
}
}
/*
* Set or get the input or output file name. These are never guaranteed
* to be accurate, only insofar as the calling program makes them so.
*/
void RTFSetInputName(RTF_Info *info, char *name)
{
info->inputName = RTFStrSave (name);
if (info->inputName == NULL)
ERR ("RTFSetInputName: out of memory\n");
}
char *RTFGetInputName(RTF_Info *info)
{
return (info->inputName);
}
void RTFSetOutputName(RTF_Info *info, char *name)
{
info->outputName = RTFStrSave (name);
if (info->outputName == NULL)
ERR ("RTFSetOutputName: out of memory\n");
}
char *RTFGetOutputName(RTF_Info *info)
{
return (info->outputName);
}
/* ---------------------------------------------------------------------- */
/*
* Callback table manipulation routines
*/
/*
* Install or return a writer callback for a token class
*/
void RTFSetClassCallback(RTF_Info *info, int class, RTFFuncPtr callback)
{
if (class >= 0 && class < rtfMaxClass)
info->ccb[class] = callback;
}
RTFFuncPtr RTFGetClassCallback(RTF_Info *info, int class)
{
if (class >= 0 && class < rtfMaxClass)
return info->ccb[class];
return NULL;
}
/*
* Install or return a writer callback for a destination type
*/
void RTFSetDestinationCallback(RTF_Info *info, int dest, RTFFuncPtr callback)
{
if (dest >= 0 && dest < rtfMaxDestination)
info->dcb[dest] = callback;
}
RTFFuncPtr RTFGetDestinationCallback(RTF_Info *info, int dest)
{
if (dest >= 0 && dest < rtfMaxDestination)
return info->dcb[dest];
return NULL;
}
/* ---------------------------------------------------------------------- */
/*
* Token reading routines
*/
/*
* Read the input stream, invoking the writer's callbacks
* where appropriate.
*/
void RTFRead(RTF_Info *info)
{
while (RTFGetToken (info) != rtfEOF)
RTFRouteToken (info);
}
/*
* Route a token. If it's a destination for which a reader is
* installed, process the destination internally, otherwise
* pass the token to the writer's class callback.
*/
void RTFRouteToken(RTF_Info *info)
{
RTFFuncPtr p;
if (info->rtfClass < 0 || info->rtfClass >= rtfMaxClass) /* watchdog */
{
ERR( "Unknown class %d: %s (reader malfunction)\n",
info->rtfClass, info->rtfTextBuf);
}
if (RTFCheckCM (info, rtfControl, rtfDestination))
{
/* invoke destination-specific callback if there is one */
p = RTFGetDestinationCallback (info, info->rtfMinor);
if (p != NULL)
{
(*p) (info);
return;
}
}
/* invoke class callback if there is one */
p = RTFGetClassCallback (info, info->rtfClass);
if (p != NULL)
(*p) (info);
}
/*
* Skip to the end of the current group. When this returns,
* writers that maintain a state stack may want to call their
* state unstacker; global vars will still be set to the group's
* closing brace.
*/
void RTFSkipGroup(RTF_Info *info)
{
int level = 1;
while (RTFGetToken (info) != rtfEOF)
{
if (info->rtfClass == rtfGroup)
{
if (info->rtfMajor == rtfBeginGroup)
++level;
else if (info->rtfMajor == rtfEndGroup)
{
if (--level < 1)
break; /* end of initial group */
}
}
}
}
/*
* Read one token. Call the read hook if there is one. The
* token class is the return value. Returns rtfEOF when there
* are no more tokens.
*/
int RTFGetToken(RTF_Info *info)
{
RTFFuncPtr p;
/* don't try to return anything once EOF is reached */
if (info->rtfClass == rtfEOF) {
return rtfEOF;
}
for (;;)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -