Initial commit.
[pdfium.git] / fpdfsdk / src / fpdfsave.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4  \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../include/fsdk_define.h"\r
8 #include "../include/fpdfsave.h"\r
9 #include "../include/fpdfedit.h"\r
10 #if _FX_OS_ == _FX_ANDROID_\r
11 #include "time.h"\r
12 #else\r
13 #include <ctime>\r
14 #endif\r
15 \r
16 class CFX_IFileWrite:public IFX_StreamWrite\r
17 {\r
18         \r
19 public:\r
20         CFX_IFileWrite();\r
21         FX_BOOL                         Init( FPDF_FILEWRITE * pFileWriteStruct );\r
22         virtual FX_BOOL         WriteBlock(const void* pData, size_t size);\r
23         virtual void            Release(){};\r
24         \r
25 protected:\r
26         FPDF_FILEWRITE*         m_pFileWriteStruct;\r
27 };\r
28 \r
29 CFX_IFileWrite::CFX_IFileWrite()\r
30 {\r
31         m_pFileWriteStruct = NULL;\r
32 }\r
33 \r
34 FX_BOOL CFX_IFileWrite::Init( FPDF_FILEWRITE * pFileWriteStruct )\r
35 {\r
36         if (!pFileWriteStruct)\r
37                 return FALSE;\r
38         else\r
39         {\r
40                 m_pFileWriteStruct = pFileWriteStruct;\r
41         }\r
42         return TRUE;\r
43 }\r
44 \r
45 FX_BOOL CFX_IFileWrite::WriteBlock(const void* pData, size_t size)\r
46 {\r
47         if (m_pFileWriteStruct)\r
48         {\r
49                 m_pFileWriteStruct->WriteBlock( m_pFileWriteStruct, pData, size );\r
50                 return TRUE;\r
51         }\r
52         else \r
53                 return FALSE;\r
54 }\r
55 \r
56 FPDF_BOOL _FPDF_Doc_Save(FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,FPDF_DWORD flags, FPDF_BOOL bSetVersion,\r
57                                                  int fileVerion)\r
58 {\r
59         CPDF_Document* pDoc = (CPDF_Document*)document;\r
60         if (!pDoc) \r
61                 return 0;\r
62         \r
63         if ( flags < 1 || flags > 2 )\r
64         {\r
65                 flags = 0;\r
66         }\r
67         \r
68         CPDF_Creator FileMaker(pDoc);\r
69         if(bSetVersion)\r
70                 FileMaker.SetFileVersion(fileVerion);\r
71         CFX_IFileWrite* pStreamWrite = NULL;\r
72         FX_BOOL bRet;\r
73         pStreamWrite = new CFX_IFileWrite;\r
74         pStreamWrite->Init( pFileWrite );\r
75         bRet = FileMaker.Create(pStreamWrite, flags);\r
76         delete pStreamWrite;\r
77         return bRet;\r
78 }\r
79 \r
80 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(    FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,\r
81                                                                                                 FPDF_DWORD flags )\r
82 {\r
83         return _FPDF_Doc_Save(document, pFileWrite, flags, FALSE , 0);\r
84 }\r
85 \r
86 \r
87 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(       FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,\r
88         FPDF_DWORD flags, int fileVersion)\r
89 {\r
90         return _FPDF_Doc_Save(document, pFileWrite, flags, TRUE , fileVersion);\r
91 }\r