~ubuntu-branches/ubuntu/raring/notecase/raring

« back to all changes in this revision

Viewing changes to src/lib/FormatIOExecutable.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nathan Handler
  • Date: 2008-12-21 13:09:58 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081221130958-0ri77h0x7j1dclkq
Tags: 1.9.8-0ubuntu1
New upstream release (LP: #307752)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
6
 
// File: Implements export to standalone executable
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
#include "FormatIOExecutable.h"
10
 
#include "FormatIOEncHtml.h"
11
 
#include "DocumentIterator.h"
12
 
#include "debug.h"
13
 
#include "../config.h"
14
 
#include "../support.h"
15
 
#include <stdio.h>      //fopen
16
 
#include <glib.h>
17
 
#include <gtk/gtk.h>
18
 
#include <string.h>
19
 
 
20
 
#ifdef _WIN32
21
 
 #include <io.h>
22
 
 #ifndef __MINGW32__
23
 
  #define strcasecmp stricmp
24
 
 #endif
25
 
#else
26
 
 #include <sys/types.h> //chmod
27
 
 #include <sys/stat.h>
28
 
#endif
29
 
 
30
 
std::string GetAppPath();
31
 
 
32
 
FormatIO_Executable::FormatIO_Executable()
33
 
{
34
 
        m_nCurParentID = -1;    //TOFIX use Idx for faster operation
35
 
        m_nCurNodeID = -1;
36
 
        m_nLastNodeLevel = -1;
37
 
        m_nCurTextLine = 0;
38
 
        m_nCoversionFailuresCnt = 0;
39
 
}
40
 
 
41
 
FormatIO_Executable::~FormatIO_Executable()
42
 
{
43
 
}
44
 
 
45
 
int FormatIO_Executable::Load(const char *szFile, NoteDocument &doc)
46
 
{
47
 
        std::string strSrcExe = GetAppPath();
48
 
        FILE *pExe = fopen(strSrcExe.c_str(), "rb");
49
 
        if(NULL != pExe)
50
 
        {
51
 
                //check "magic" marker adt the end of the file
52
 
                fseek(pExe, -8, SEEK_END);
53
 
                char szBuffer[10];
54
 
                size_t nRead = 0;
55
 
                nRead = fread(szBuffer, 8, 1, pExe);
56
 
                szBuffer[8] = '\0';
57
 
                if(0 != strcmp("NOTECASE", szBuffer)){
58
 
                        fclose(pExe);
59
 
                        return DOC_LOAD_FORMAT_ERROR;
60
 
                }
61
 
 
62
 
                //marker is correct, read document start offset
63
 
                INT64 nStandaloneOffset = -1;
64
 
 
65
 
                fseek(pExe, -16, SEEK_END);
66
 
                int nEndOffset = ftell(pExe);
67
 
 
68
 
                nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
69
 
                //convert from network byte order (big-endian) to processor default
70
 
                if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
71
 
                        nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
72
 
                fclose(pExe);
73
 
 
74
 
                //now load the document
75
 
                FormatIO_Base *pLoader = new FormatIO_EncHTML;
76
 
                ((FormatIO_EncHTML *)pLoader)->SetLoadEmbedded(nStandaloneOffset, (INT64)nEndOffset);
77
 
                ((FormatIO_EncHTML *)pLoader)->SetPassword(doc.GetPassword());
78
 
                int nRes = pLoader->Load(strSrcExe.c_str(), doc);
79
 
 
80
 
                //copy data from the real loader
81
 
                m_strGeneratorApp = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorApp;
82
 
                m_strGeneratorVer = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorVer;
83
 
 
84
 
                delete pLoader;
85
 
                return nRes;
86
 
        }
87
 
 
88
 
        return DOC_LOAD_ERROR;
89
 
}
90
 
 
91
 
int FormatIO_Executable::Save(const char *szFile, NoteDocument &doc)
92
 
{
93
 
        //
94
 
        // first create a copy of current Notecase application file
95
 
        //
96
 
        std::string strSrcExe = GetAppPath();
97
 
        FILE *pExe = fopen(strSrcExe.c_str(), "rb");
98
 
        if(NULL == pExe)
99
 
                return DOC_SAVE_ERR_EXE_OPEN;
100
 
 
101
 
        //check if this exe has already embedded document (that is not being copied)
102
 
        // - check "magic" marker at the end of the file
103
 
        INT64 nStandaloneOffset = -1;
104
 
        fseek(pExe, -8, SEEK_END);
105
 
        char szBuffer1[10];
106
 
        int nRead = 0;
107
 
        nRead = fread(szBuffer1, 8, 1, pExe);
108
 
        szBuffer1[8] = '\0';
109
 
        if(0 == strcmp("NOTECASE", szBuffer1)){
110
 
                //marker is correct, read document start offset
111
 
                fseek(pExe, -16, SEEK_END);
112
 
                nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
113
 
                //convert from network byte order (big-endian) to processor default
114
 
                if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
115
 
                        nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
116
 
        }
117
 
        fseek(pExe, 0, SEEK_SET);
118
 
 
119
 
        FILE *pOut = fopen(szFile, "wb");
120
 
        if(NULL == pOut){
121
 
                fclose(pExe);
122
 
                return DOC_SAVE_ERR_FILE_OPEN;
123
 
        }
124
 
 
125
 
        int nTotal = 0;
126
 
        char szBuffer[10000];
127
 
        size_t nWritten = 0;
128
 
        while((nRead = fread(szBuffer, 1, sizeof(szBuffer), pExe)) > 0){
129
 
                if(nStandaloneOffset >= 0 && (nTotal + nRead) > nStandaloneOffset){
130
 
                        nWritten = fwrite(szBuffer, 1, (size_t)(nStandaloneOffset-nTotal), pOut);
131
 
                        break;
132
 
                }
133
 
                else{
134
 
                        nWritten = fwrite(szBuffer, 1, nRead, pOut);
135
 
                        nTotal += nRead;
136
 
                }
137
 
        }
138
 
        fclose(pExe);
139
 
 
140
 
        //
141
 
        // next append the document
142
 
        //
143
 
        INT64 nDocStartPos = ftell(pOut);
144
 
        fclose(pOut);
145
 
 
146
 
        //Document is always in stored in EncHtml format (because it is compressed)
147
 
        //if user does not have a password we use default one "NOTECASE"
148
 
        std::string strPass = doc.GetPassword();
149
 
        if(strPass.size() == 0)
150
 
                strPass = "NOTECASE";
151
 
 
152
 
        FormatIO_EncHTML exporter;
153
 
        exporter.SetPassword(strPass.c_str());
154
 
        int nRes = exporter.Save(szFile, doc, true);
155
 
        if(DOC_SAVE_OK != nRes){
156
 
                remove(szFile); //remove output file on error
157
 
                return nRes;
158
 
        }
159
 
 
160
 
        //now write a header to recognize document embedded in the exe
161
 
        pOut = fopen(szFile, "ab");
162
 
        if(NULL == pOut){
163
 
                remove(szFile); //remove output file on error
164
 
                return DOC_SAVE_ERR_FILE_OPEN;
165
 
        }
166
 
 
167
 
        //write offset of the begining of the document
168
 
        //convert from processor default to network byte order (big-endian)
169
 
        if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
170
 
                nDocStartPos = GINT64_TO_BE(nDocStartPos);
171
 
        nWritten = fwrite(&nDocStartPos, sizeof(INT64), 1, pOut);
172
 
 
173
 
        //write "magic" letters to mark embedded document
174
 
        nWritten = fwrite("NOTECASE", strlen("NOTECASE"), 1, pOut);
175
 
        fclose(pOut);
176
 
 
177
 
#ifndef _WIN32
178
 
        //set default file permission (note that chmod(755) would faild for me)
179
 
        mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IROTH; //0x0744
180
 
        chmod(szFile, mode);
181
 
#endif
182
 
 
183
 
 
184
 
        return DOC_SAVE_OK;
185
 
}
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: Implements export to standalone executable
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
#include "FormatIOExecutable.h"
 
10
#include "FormatIOEncHtml.h"
 
11
#include "DocumentIterator.h"
 
12
#include "debug.h"
 
13
#include "../config.h"
 
14
#include "../support.h"
 
15
#include <stdio.h>      //fopen
 
16
#include <glib.h>
 
17
#include <gtk/gtk.h>
 
18
#include <string.h>
 
19
 
 
20
#ifdef _WIN32
 
21
 #include <io.h>
 
22
 #ifndef __MINGW32__
 
23
  #define strcasecmp stricmp
 
24
 #endif
 
25
#else
 
26
 #include <sys/types.h> //chmod
 
27
 #include <sys/stat.h>
 
28
#endif
 
29
 
 
30
std::string GetAppPath();
 
31
 
 
32
FormatIO_Executable::FormatIO_Executable()
 
33
{
 
34
        m_nCurParentID = -1;    //TOFIX use Idx for faster operation
 
35
        m_nCurNodeID = -1;
 
36
        m_nLastNodeLevel = -1;
 
37
        m_nCurTextLine = 0;
 
38
        m_nCoversionFailuresCnt = 0;
 
39
}
 
40
 
 
41
FormatIO_Executable::~FormatIO_Executable()
 
42
{
 
43
}
 
44
 
 
45
int FormatIO_Executable::Load(const char *szFile, NoteDocument &doc)
 
46
{
 
47
        std::string strSrcExe = GetAppPath();
 
48
        FILE *pExe = fopen(strSrcExe.c_str(), "rb");
 
49
        if(NULL != pExe)
 
50
        {
 
51
                //check "magic" marker adt the end of the file
 
52
                fseek(pExe, -8, SEEK_END);
 
53
                char szBuffer[10];
 
54
                size_t nRead = 0;
 
55
                nRead = fread(szBuffer, 8, 1, pExe);
 
56
                szBuffer[8] = '\0';
 
57
                if(0 != strcmp("NOTECASE", szBuffer)){
 
58
                        fclose(pExe);
 
59
                        return DOC_LOAD_FORMAT_ERROR;
 
60
                }
 
61
 
 
62
                //marker is correct, read document start offset
 
63
                INT64 nStandaloneOffset = -1;
 
64
 
 
65
                fseek(pExe, -16, SEEK_END);
 
66
                int nEndOffset = ftell(pExe);
 
67
 
 
68
                nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
 
69
                //convert from network byte order (big-endian) to processor default
 
70
                if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
 
71
                        nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
 
72
                fclose(pExe);
 
73
 
 
74
                //now load the document
 
75
                FormatIO_Base *pLoader = new FormatIO_EncHTML;
 
76
                ((FormatIO_EncHTML *)pLoader)->SetLoadEmbedded(nStandaloneOffset, (INT64)nEndOffset);
 
77
                ((FormatIO_EncHTML *)pLoader)->SetPassword(doc.GetPassword());
 
78
                int nRes = pLoader->Load(strSrcExe.c_str(), doc);
 
79
 
 
80
                //copy data from the real loader
 
81
                m_strGeneratorApp = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorApp;
 
82
                m_strGeneratorVer = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorVer;
 
83
 
 
84
                delete pLoader;
 
85
                return nRes;
 
86
        }
 
87
 
 
88
        return DOC_LOAD_ERROR;
 
89
}
 
90
 
 
91
int FormatIO_Executable::Save(const char *szFile, NoteDocument &doc)
 
92
{
 
93
        //
 
94
        // first create a copy of current Notecase application file
 
95
        //
 
96
        std::string strSrcExe = GetAppPath();
 
97
        FILE *pExe = fopen(strSrcExe.c_str(), "rb");
 
98
        if(NULL == pExe)
 
99
                return DOC_SAVE_ERR_EXE_OPEN;
 
100
 
 
101
        //check if this exe has already embedded document (that is not being copied)
 
102
        // - check "magic" marker at the end of the file
 
103
        INT64 nStandaloneOffset = -1;
 
104
        fseek(pExe, -8, SEEK_END);
 
105
        char szBuffer1[10];
 
106
        int nRead = 0;
 
107
        nRead = fread(szBuffer1, 8, 1, pExe);
 
108
        szBuffer1[8] = '\0';
 
109
        if(0 == strcmp("NOTECASE", szBuffer1)){
 
110
                //marker is correct, read document start offset
 
111
                fseek(pExe, -16, SEEK_END);
 
112
                nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
 
113
                //convert from network byte order (big-endian) to processor default
 
114
                if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
 
115
                        nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
 
116
        }
 
117
        fseek(pExe, 0, SEEK_SET);
 
118
 
 
119
        FILE *pOut = fopen(szFile, "wb");
 
120
        if(NULL == pOut){
 
121
                fclose(pExe);
 
122
                return DOC_SAVE_ERR_FILE_OPEN;
 
123
        }
 
124
 
 
125
        int nTotal = 0;
 
126
        char szBuffer[10000];
 
127
        size_t nWritten = 0;
 
128
        while((nRead = fread(szBuffer, 1, sizeof(szBuffer), pExe)) > 0){
 
129
                if(nStandaloneOffset >= 0 && (nTotal + nRead) > nStandaloneOffset){
 
130
                        nWritten = fwrite(szBuffer, 1, (size_t)(nStandaloneOffset-nTotal), pOut);
 
131
                        break;
 
132
                }
 
133
                else{
 
134
                        nWritten = fwrite(szBuffer, 1, nRead, pOut);
 
135
                        nTotal += nRead;
 
136
                }
 
137
        }
 
138
        fclose(pExe);
 
139
 
 
140
        //
 
141
        // next append the document
 
142
        //
 
143
        INT64 nDocStartPos = ftell(pOut);
 
144
        fclose(pOut);
 
145
 
 
146
        //Document is always in stored in EncHtml format (because it is compressed)
 
147
        //if user does not have a password we use default one "NOTECASE"
 
148
        std::string strPass = doc.GetPassword();
 
149
        if(strPass.size() == 0)
 
150
                strPass = "NOTECASE";
 
151
 
 
152
        FormatIO_EncHTML exporter;
 
153
        exporter.SetPassword(strPass.c_str());
 
154
        int nRes = exporter.Save(szFile, doc, true);
 
155
        if(DOC_SAVE_OK != nRes){
 
156
                remove(szFile); //remove output file on error
 
157
                return nRes;
 
158
        }
 
159
 
 
160
        //now write a header to recognize document embedded in the exe
 
161
        pOut = fopen(szFile, "ab");
 
162
        if(NULL == pOut){
 
163
                remove(szFile); //remove output file on error
 
164
                return DOC_SAVE_ERR_FILE_OPEN;
 
165
        }
 
166
 
 
167
        //write offset of the begining of the document
 
168
        //convert from processor default to network byte order (big-endian)
 
169
        if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
 
170
                nDocStartPos = GINT64_TO_BE(nDocStartPos);
 
171
        nWritten = fwrite(&nDocStartPos, sizeof(INT64), 1, pOut);
 
172
 
 
173
        //write "magic" letters to mark embedded document
 
174
        nWritten = fwrite("NOTECASE", strlen("NOTECASE"), 1, pOut);
 
175
        fclose(pOut);
 
176
 
 
177
#ifndef _WIN32
 
178
        //set default file permission (note that chmod(755) would faild for me)
 
179
        mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IROTH; //0x0744
 
180
        chmod(szFile, mode);
 
181
#endif
 
182
 
 
183
 
 
184
        return DOC_SAVE_OK;
 
185
}