* some small cleanup

* fFirstPage should be of type off_t



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24532 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich
2008-03-23 16:01:03 +00:00
parent e26a4ce3e9
commit 96d23158ea
2 changed files with 85 additions and 46 deletions
+2 -2
View File
@@ -56,7 +56,7 @@ public:
class PrintJobReader { class PrintJobReader {
BFile fJobFile; // the job file BFile fJobFile; // the job file
int32 fNumberOfPages; // the number of pages in the job file int32 fNumberOfPages; // the number of pages in the job file
int32 fFirstPage; // the page number of the first page off_t fFirstPage; // the page number of the first page
BMessage fJobSettings; // the settings extracted from the job file BMessage fJobSettings; // the settings extracted from the job file
off_t* fPageIndex; // start positions of pages in the job file off_t* fPageIndex; // start positions of pages in the job file
@@ -80,7 +80,7 @@ public:
float GetScale() const; float GetScale() const;
// retrieve page // retrieve page
status_t GetPage(int no, PrintJobPage& pjp); status_t GetPage(int32 no, PrintJobPage& pjp);
}; };
#endif #endif
+83 -44
View File
@@ -27,12 +27,18 @@ THE SOFTWARE.
*/ */
#include <stdio.h>
#include <Picture.h>
#include <PrintJob.h>
#include "PrintJobReader.h" #include "PrintJobReader.h"
// Implementation of PrintJobPage
#include <stdio.h>
#include <Picture.h>
#include <PrintJob.h>
// #pragma mark --- PrintJobPage
PrintJobPage::PrintJobPage() PrintJobPage::PrintJobPage()
: fNextPicture(-1) : fNextPicture(-1)
@@ -42,6 +48,7 @@ PrintJobPage::PrintJobPage()
{ {
} }
PrintJobPage::PrintJobPage(const PrintJobPage& copy) PrintJobPage::PrintJobPage(const PrintJobPage& copy)
: fJobFile(copy.fJobFile) : fJobFile(copy.fJobFile)
, fNextPicture(copy.fNextPicture) , fNextPicture(copy.fNextPicture)
@@ -51,7 +58,9 @@ PrintJobPage::PrintJobPage(const PrintJobPage& copy)
{ {
} }
PrintJobPage& PrintJobPage::operator=(const PrintJobPage& copy) {
PrintJobPage& PrintJobPage::operator=(const PrintJobPage& copy)
{
if (this != &copy) { if (this != &copy) {
fJobFile = copy.fJobFile; fJobFile = copy.fJobFile;
fNextPicture = copy.fNextPicture; fNextPicture = copy.fNextPicture;
@@ -62,42 +71,56 @@ PrintJobPage& PrintJobPage::operator=(const PrintJobPage& copy) {
return *this; return *this;
} }
PrintJobPage::PrintJobPage(BFile* jobFile, off_t start) PrintJobPage::PrintJobPage(BFile* jobFile, off_t start)
: fJobFile(*jobFile) : fJobFile(*jobFile)
, fPicture(0) , fPicture(0)
, fStatus(B_ERROR) , fStatus(B_ERROR)
{ {
off_t size; off_t size;
if (fJobFile.GetSize(&size) != B_OK || start > size) return; if (fJobFile.GetSize(&size) != B_OK || start > size)
if (fJobFile.Seek(start, SEEK_SET) != start) return; return;
if (fJobFile.Read(&fNumberOfPictures, sizeof(fNumberOfPictures)) == sizeof(fNumberOfPictures)) {
fJobFile.Seek(40 + sizeof(off_t), SEEK_CUR); if (fJobFile.Seek(start, SEEK_SET) != start)
return;
off_t nextPage;
if (fJobFile.Read(&fNumberOfPictures, sizeof(int32)) == sizeof(int32)) {
// (sizeof(int32) * 10) == padding in _page_header_
fJobFile.Seek(sizeof(off_t) + sizeof(int32) * 10, SEEK_CUR);
fNextPicture = fJobFile.Position(); fNextPicture = fJobFile.Position();
fStatus = B_OK; fStatus = B_OK;
} }
} }
status_t PrintJobPage::InitCheck() const {
status_t PrintJobPage::InitCheck() const
{
return fStatus; return fStatus;
} }
status_t PrintJobPage::NextPicture(BPicture& picture, BPoint& point, BRect& rect) {
if (fPicture >= fNumberOfPictures) return B_ERROR; status_t PrintJobPage::NextPicture(BPicture& picture, BPoint& point, BRect& rect)
fPicture ++; {
if (fPicture >= fNumberOfPictures)
return B_ERROR;
fPicture++;
fJobFile.Seek(fNextPicture, SEEK_SET); fJobFile.Seek(fNextPicture, SEEK_SET);
fJobFile.Read(&point, sizeof(point)); fJobFile.Read(&point, sizeof(BPoint));
fJobFile.Read(&rect, sizeof(rect)); fJobFile.Read(&rect, sizeof(BRect));
status_t rc = picture.Unflatten(&fJobFile); status_t rc = picture.Unflatten(&fJobFile);
fNextPicture = fJobFile.Position(); fNextPicture = fJobFile.Position();
if (rc != B_OK) {
if (rc != B_OK)
fPicture = fNumberOfPictures; fPicture = fNumberOfPictures;
}
return rc; return rc;
} }
// Implementation of PrintJobReader // # pragma mark --- PrintJobReader
PrintJobReader::PrintJobReader(BFile* jobFile) PrintJobReader::PrintJobReader(BFile* jobFile)
: fJobFile(*jobFile) : fJobFile(*jobFile)
@@ -110,71 +133,87 @@ PrintJobReader::PrintJobReader(BFile* jobFile)
BPrintJob::print_file_header header; BPrintJob::print_file_header header;
#endif #endif
fJobFile.Seek(0, SEEK_SET); fJobFile.Seek(0, SEEK_SET);
if (fJobFile.Read(&header, sizeof(header)) == sizeof(header) && if (fJobFile.Read(&header, sizeof(header)) == sizeof(header)) {
fJobSettings.Unflatten(&fJobFile) == B_OK) { if (fJobSettings.Unflatten(&fJobFile) == B_OK) {
fNumberOfPages = header.page_count; fNumberOfPages = header.page_count;
fFirstPage = header.first_page; fFirstPage = header.first_page;
BuildPageIndex(); fPageIndex = new off_t[fNumberOfPages];
BuildPageIndex();
}
} }
} }
PrintJobReader::~PrintJobReader() {
PrintJobReader::~PrintJobReader()
{
delete[] fPageIndex; delete[] fPageIndex;
} }
status_t PrintJobReader::InitCheck() const {
status_t PrintJobReader::InitCheck() const
{
return fNumberOfPages > 0 ? B_OK : B_ERROR; return fNumberOfPages > 0 ? B_OK : B_ERROR;
} }
void PrintJobReader::BuildPageIndex() {
fPageIndex = new off_t[fNumberOfPages]; void PrintJobReader::BuildPageIndex()
{
for (int page = 0; page < fNumberOfPages; page ++) { off_t next_page;
int32 pictures; int32 number_of_pictures;
off_t next_page; for (int32 page = 0; page < fNumberOfPages; ++page) {
// add position to page index
fPageIndex[page] = fJobFile.Position(); fPageIndex[page] = fJobFile.Position();
if (fJobFile.Read(&number_of_pictures, sizeof(int32)) == sizeof(int32)
// determine start position of next page && fJobFile.Read(&next_page, sizeof(off_t)) == sizeof(off_t)
if (fJobFile.Read(&pictures, sizeof(pictures)) == sizeof(pictures) && && fPageIndex[page] < next_page) {
fJobFile.Read(&next_page, sizeof(next_page)) == sizeof(next_page) &&
fPageIndex[page] < next_page) {
fJobFile.Seek(next_page, SEEK_SET); fJobFile.Seek(next_page, SEEK_SET);
} else { } else {
fNumberOfPages = 0; delete fPageIndex; fPageIndex = NULL; fNumberOfPages = 0;
delete fPageIndex;
fPageIndex = NULL;
return; return;
} }
} }
} }
status_t PrintJobReader::GetPage(int page, PrintJobPage& pjp) {
status_t PrintJobReader::GetPage(int32 page, PrintJobPage& pjp)
{
if (0 <= page && page < fNumberOfPages) { if (0 <= page && page < fNumberOfPages) {
PrintJobPage p(&fJobFile, fPageIndex[page]); PrintJobPage p(&fJobFile, fPageIndex[page]);
if (p.InitCheck() == B_OK) { if (p.InitCheck() == B_OK) {
pjp = p; return B_OK; pjp = p;
return B_OK;
} }
} }
return B_ERROR; return B_ERROR;
} }
BRect PrintJobReader::PaperRect() const {
BRect PrintJobReader::PaperRect() const
{
BRect r; BRect r;
fJobSettings.FindRect("paper_rect", &r); fJobSettings.FindRect("paper_rect", &r);
return r; return r;
} }
BRect PrintJobReader::PrintableRect() const {
BRect PrintJobReader::PrintableRect() const
{
BRect r; BRect r;
fJobSettings.FindRect("printable_rect", &r); fJobSettings.FindRect("printable_rect", &r);
return r; return r;
} }
void PrintJobReader::GetResolution(int32 *xdpi, int32 *ydpi) const {
void PrintJobReader::GetResolution(int32 *xdpi, int32 *ydpi) const
{
fJobSettings.FindInt32("xres", xdpi); fJobSettings.FindInt32("xres", xdpi);
fJobSettings.FindInt32("yres", ydpi); fJobSettings.FindInt32("yres", ydpi);
} }
float PrintJobReader::GetScale() const { float PrintJobReader::GetScale() const
{
float scale = 1.0; float scale = 1.0;
fJobSettings.FindFloat("scale", &scale); fJobSettings.FindFloat("scale", &scale);
return scale; return scale;