/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* This file is part of libxls -- A multiplatform, C/C++ library
* for parsing Excel(TM) files.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2004 Komarov Valery
* Copyright 2006 Christophe Leitienne
* Copyright 2008-2012 David Hoerl
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <memory.h>
#include <math.h>
#include <sys/types.h>
#include <string.h>
#include <wchar.h>
#include <assert.h>
#include "libxls/xls.h"
#define min(a,b) ((a) < (b) ? (a) : (b))
int xls_debug=0; // now global, so users can turn it on
static double NumFromRk(BYTE* rk);
extern void xls_addSST(xlsWorkBook* pWB,SST* sst,DWORD size);
extern void xls_appendSST(xlsWorkBook* pWB,BYTE* buf,DWORD size);
extern void xls_addFormat(xlsWorkBook* pWB,FORMAT* format);
extern BYTE* xls_addSheet(xlsWorkBook* pWB,BOUNDSHEET* bs);
extern void xls_addRow(xlsWorkSheet* pWS,ROW* row);
extern void xls_makeTable(xlsWorkSheet* pWS);
extern struct st_cell_data *xls_addCell(xlsWorkSheet* pWS,BOF* bof,BYTE* buf);
extern BYTE *xls_addFont(xlsWorkBook* pWB,FONT* font);
extern void xls_addXF8(xlsWorkBook* pWB,XF8* xf);
extern void xls_addXF5(xlsWorkBook* pWB,XF5* xf);
extern void xls_addColinfo(xlsWorkSheet* pWS,COLINFO* colinfo);
extern void xls_mergedCells(xlsWorkSheet* pWS,BOF* bof,BYTE* buf);
extern void xls_parseWorkBook(xlsWorkBook* pWB);
extern void xls_preparseWorkSheet(xlsWorkSheet* pWS);
extern void xls_formatColumn(xlsWorkSheet* pWS);
extern void xls_parseWorkSheet(xlsWorkSheet* pWS);
extern void xls_dumpSummary(char *buf,int isSummary,xlsSummaryInfo *pSI);
#pragma pack(push, 1)
typedef struct {
uint32_t format[4];
uint32_t offset;
} sectionList;
typedef struct {
uint16_t sig;
uint16_t _empty;
uint32_t os;
uint32_t format[4];
uint32_t count;
sectionList secList[0];
} header;
typedef struct {
uint32_t propertyID;
uint32_t sectionOffset;
} propertyList;
typedef struct {
uint32_t length;
uint32_t numProperties;
propertyList properties[0];
} sectionHeader;
typedef struct {
uint32_t propertyID;
uint32_t data[0];
} property;
#pragma pack(pop)
int xls(int debug)
{
xls_debug = debug;
return 1;
}
void xls_addSST(xlsWorkBook* pWB,SST* sst,DWORD size)
{
verbose("xls_addSST");
pWB->sst.continued=0;
pWB->sst.lastln=0;
pWB->sst.lastid=0;
pWB->sst.lastrt=0;
pWB->sst.lastsz=0;
pWB->sst.count = sst->num;
pWB->sst.string =(struct str_sst_string *)calloc(pWB->sst.count, sizeof(struct str_sst_string));
xls_appendSST(pWB,&sst->strings,size-8);
}
void xls_appendSST(xlsWorkBook* pWB,BYTE* buf,DWORD size)
{
DWORD ln; // String character count
DWORD ofs; // Current offset in SST buffer
DWORD rt; // Count of rich text formatting runs
DWORD sz; // Size of asian phonetic settings block
BYTE flag; // String flags
BYTE* ret;
if (xls_debug) {
printf("xls_appendSST %u\n", size);
}
sz = rt = ln = 0; // kch
ofs=0;
while(ofs<size)
{
int ln_toread;
// Restore state when we're in a continue record
// or read string length
if (pWB->sst.continued)
{
ln=pWB->sst.lastln;
rt=pWB->sst.lastrt;
sz=pWB->sst.lastsz;
}
else
{
ln=*(WORD_UA *)(buf+ofs);
rt = 0;
sz = 0;
ofs+=2;
}
if (xls_debug) {
printf("ln=%u\n", ln);
}
// Read flags
if ( (!pWB->sst.continued) || ( (pWB->sst.continued) && (ln != 0) ) )
{
flag=*(BYTE *)(buf+ofs);
ofs++;
// Count of rich text formatting runs
if (flag & 0x8)
{
rt=*(WORD_UA *)(buf+ofs);
ofs+=2;
}
// Size of asian phonetic settings block
if (flag & 0x4)
{
sz=*(DWORD_UA *)(buf+ofs);
ofs+=4;
if (xls_debug) {
printf("sz=%u\n", sz);
}
}
}
else
{
flag = 0;
}
// Read characters (compressed or not)
ln_toread = 0;
if (ln > 0)
{
if (flag & 0x1)
{
ln_toread = min((size-ofs)/2, ln);
size_t new_len = 0;
ret=unicode_decode(buf+ofs,ln_toread*2,&new_len,pWB->charset);
if (ret == NULL)
{
ret = (BYTE *)strdup("*failed to decode utf16*");
new_len = strlen((char *)ret);
}
ret = (BYTE *)realloc(ret,new_len+1);
*(BYTE*)(ret+new_len)=0;
ln -= ln_toread;
ofs+=ln_toread*2;
if (xls_debug) {
printf("String16SST: %s(%zd)\n",ret,new_len);
}
}
else
{
ln_toread = min((size-ofs), ln);
ret = utf8_decode((buf+ofs), ln_toread, pWB->charset);
ln -= ln_toread;
ofs +=ln_toread;
if (xls_debug) {
printf("String8SST: %s(%u) \n",ret,ln);
}
}
}
else
{
ret = (BYTE *)strdup("");
}
if ( (ln_toread > 0)
||(!pWB->sst.continued) )
{
// Concat string if it's a continue, or add string in table
if (!pWB->sst.continued)
{
pWB->sst.lastid++;
pWB->sst.string[pWB->sst.lastid-1].str=ret;
}
else
{
BYTE *tmp;
tmp=pWB->sst.string[pWB->sst.lastid-1].str;
tmp=(BYTE *)realloc(tmp,strlen((char *)tmp)+strlen((char *)ret)+1);
pWB->sst.string[pWB->sst.lastid-1].str=tmp;
memcpy(tmp+strlen((char *)tmp),ret,strlen((char *)ret)+1);
free(ret);
}
if (xls_debug) {
printf("String %4u: %s<end>\n", pWB->sst.lastid-1, pWB->sst.string[pWB->sst.lastid-1].str);
}
}
// Jump list of rich text formatting runs
if ( (ofs < size)
&&(rt > 0) )
{
int rt_toread = min((size-ofs)/4, rt);
rt -= rt_toread;
ofs += rt_toread*4;
}
// Jump asian phonetic settings block
if ( (ofs < size)
&&(sz > 0) )
{
int sz_toread = min((size-ofs), sz);
sz -= sz_toread;
ofs += sz_toread;
}
pWB->sst.continued=0;
}
// Save current character count and count of rich text formatting runs and s
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
XLSReader.zip (64个子文件)
XLSReader
XLSReader.xcodeproj
project.xcworkspace
contents.xcworkspacedata 154B
xcuserdata
chenghxc.xcuserdatad
UserInterfaceState.xcuserstate 23KB
evan.xcuserdatad
WorkspaceSettings.xcsettings 332B
UserInterfaceState.xcuserstate 29KB
project.pbxproj 18KB
xcuserdata
chenghxc.xcuserdatad
xcschemes
xcschememanagement.plist 481B
XLSReader.xcscheme 2KB
evan.xcuserdatad
xcschemes
xcschememanagement.plist 481B
XLSReader.xcscheme 2KB
.DS_Store 6KB
XLSReader
config.h 3KB
XLScell.h 2KB
XLSReader.h 3KB
XLScell-Private.h 2KB
XLSReader-Prefix.pch 149B
libxls
include
.DS_Store 6KB
libxls
brdb.c.h 10KB
brdb.h 2KB
ole.h 4KB
xlsstruct.h 7KB
.DS_Store 6KB
xlstypes.h 2KB
xlstool.h 2KB
xls.h 3KB
src
getopt.c 4KB
xls.c 39KB
ole.c 16KB
.DS_Store 6KB
xls2csv.c 7KB
xlstool.c 18KB
XLScell.m 3KB
XLSReader.m 10KB
build
Release
XLSReader.framework
Headers 24B
Versions
Current 1B
A
Headers
config.h 3KB
brdb.c.h 10KB
XLScell.h 2KB
brdb.h 2KB
ole.h 4KB
XLSReader.h 3KB
XLScell-Private.h 2KB
xlsstruct.h 7KB
xlstypes.h 2KB
xlstool.h 2KB
xls.h 3KB
XLSReader 73KB
XLSReader 26B
XLSReader.framework.dSYM
Contents
Resources
DWARF
XLSReader 82KB
Info.plist 648B
Debug
XLSReader.framework
Headers 24B
Versions
Current 1B
A
Headers
config.h 3KB
brdb.c.h 10KB
XLScell.h 2KB
brdb.h 2KB
ole.h 4KB
XLSReader.h 3KB
XLScell-Private.h 2KB
xlsstruct.h 7KB
xlstypes.h 2KB
xlstool.h 2KB
xls.h 3KB
XLSReader 108KB
XLSReader 26B
共 64 条
- 1
chenghxc
- 粉丝: 9
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页