print.ecp

text/plain

Filename: print.ecp
Type: text/plain
Part: 0
Message: Strange problem with ecpg
/*
 * File   : print.ec
 * Purpose: This file contains the routines to print some reports.
 * Autor  : Andreas Theofilu
 *
 * Copyright (C) 1998, 1999 by Theos Soft.
 */

/* 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <Xmt/Xmt.h>
#include <Xmt/Create.h>
#include <Xmt/Converters.h>
#include <Xmt/Procedures.h>
#include <Xmt/WidgetType.h>
#include <Xmt/Dialogs.h>
#include "platten.h"
#include "printu.h"

EXEC SQL include sqlca;
EXEC SQL type disk_t is struct
{
	long number;
	char title[41];
	char b_side[41];
	char singer[41];
	char pressed[11];
	char zeit[6];
	char zeitb[6];
	char lpt[41];
};

EXEC SQL type lpmaster_t is struct
{
	long lpnum;
	char lptitel[61];
	char lpkenn[3];
	char lpdate[11];
	long lpdiscid;
	char lpcategory[21];
};

EXEC SQL type lpchild_t is struct
{
	long snum;
	int spos;
	char stitel[41];
	char ssinger[41];
	char szeit[6];
};
typedef struct
{
	long number;
	char title[41];
	char b_side[41];
	char singer[41];
	char pressed[11];
	char zeit[6];
	char zeitb[6];
	char lpt[41];
} disk_t;

typedef struct
{
	long lpnum;
	char lptitel[61];
	char lpkenn[3];
	char lpdate[11];
	long lpdiscid;
	char lpcategory[21];
} lpmaster_t;

typedef struct
{
	long snum;
	int spos;
	char stitel[41];
	char ssinger[41];
	char szeit[6];
} lpchild_t;
void prSingles ();
void PageSingleHeader (float *y);
void MakeSingleCVS ();

void prLps ();
float PrintLpsHeader (float y, lpmaster_t *lpmaster);
void MakeLpsCVS ();

void PrintSingles ()
{
Boolean aret, answer;

	aret = XmtAskForBoolean (toplevel, "AskPrintSingle", NULL, NULL, NULL,
			NULL, XmtCancelButton, XmDIALOG_QUESTION, True,
			&answer, NULL);

	if (!aret)
	   return;

	if (answer)		/* True = Print Singles */
	   prSingles ();
}

void PrintLps ()
{
Boolean aret, answer;

	aret = XmtAskForBoolean (toplevel, "AskPrintLps", NULL, NULL, NULL,
			NULL, XmtCancelButton, XmDIALOG_QUESTION, True,
			&answer, NULL);

	if (!aret)
	   return;

	if (answer)		/* True = Print Singles */
	{
	   XmtDisplayBusyCursor (toplevel);
	   prLps ();
	   XmtDisplayDefaultCursor (toplevel);
	}
}

void prSingles ()
{
EXEC SQL BEGIN DECLARE SECTION;
	disk_t disk;
EXEC SQL END DECLARE SECTION;
float x, y;
char hv0[128], hv1[128];
Boolean Start = True;
Boolean FirstPage = True;

	XmtDisplayBusyCursor (toplevel);

	if (option.chPrinter == 2)
	{
	   MakeSingleCVS ();
	   XmtDisplayDefaultCursor (toplevel);
	   return;
	}

	if (!PsOpenPrint ((option.chPrinter == 0) ? option.ipPrinter : option.ipFile, option.chPrinter))
	{
	   XmtDisplayDefaultCursor (toplevel);
	   return;
	}

	PsNewPage ();
	PsSetMetric (PsPT);
	PsChooseFont (PsHELVETICA_BOLD, 18);
	x = 46.0;
	y = 756.0;
	PsNewPath ();
	PsSetPosition (x, y);
	PsDrawText ("Liste aller Singles nach Interpret(in) sortiert");

	if (GetPrintStatus ())
	   return;

	y -= 20.0;

	EXEC SQL declare prsicurs cursor for
		select * from disk order by singer, title;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   XmtDisplayDefaultCursor (toplevel);
	   return;
	}

	EXEC SQL open prsicurs;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   XmtDisplayDefaultCursor (toplevel);
	   return;
	}
	EXEC SQL fetch in prsicurs into :disk.number, :disk.title,
		:disk.b_side, :disk.singer, :disk.pressed, :disk.zeit,
		:disk.zeitb, :disk.lpt;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   XmtDisplayDefaultCursor (toplevel);
	   EXEC SQL close prsicurs;
	   EXEC SQL free prsicurs;
	   return;
	}

	while (SQLCODE == 0)
	{
	   rtrim (disk.title);
	   rtrim (disk.b_side);
	   rtrim (disk.singer);
	   rtrim (disk.zeit);
	   rtrim (disk.zeitb);
	   rtrim (disk.lpt);

	   if (Start)
	   {
	      if (!FirstPage)
		 PsNewPage ();
	      else
		 FirstPage = False;

	      PageSingleHeader (&y);
	      Start = False;

	      if (GetPrintStatus ())
	      {
	         EXEC SQL close prsicurs;
	         EXEC SQL free prsicurs;
	         return;
	      }
	   }

	   y -= 11.0;
	   PsNewPath ();
	   PsSetPosition (x, y);
	   sprintf (hv0, "%ld", disk.number);
	   PsDrawText (hv0);
	   PsNewPath ();
	   PsSetPosition (x + 44.0, y);
	   PsDrawText (disk.title);
	   PsNewPath ();
	   PsSetPosition (x + 257.0, y);
	   PsDrawText (disk.singer);
	   PsNewPath ();
	   PsSetPosition (x + 492.0, y);
	   PsDrawText (disk.zeit);
	   y -= 11.0;
	   PsNewPath ();
	   PsSetPosition (x + 44.0, y);
	   PsDrawText (disk.b_side);
	   strcpy (hv0, disk.pressed);
	   if (hv0[0])
	   {
	      PsNewPath ();
	      PsSetPosition (x + 257, y);
	      PsDrawText (hv0);
	   }

	   PsNewPath ();
	   PsSetPosition (x + 492.0, y);
	   PsDrawText (disk.zeitb);
	   y -= 2.0;
	   PsNewPath ();
	   PsDrawLine (x, y, x + 524.0, y);
	   PsClosePath (PsSTROKE);

	   if (y < 47.0)
	   {
	      PsChooseFont (PsHELVETICA, 8);
	      PsNewPath ();
	      PsSetPosition (526.0, 24.0);
	      sprintf (hv0, "Seite: %d", PsGetPage ());
	      PsDrawText (hv0);
	      y = 756.0;
	      Start = True;
	      PsShowPage ();

	      if (GetPrintStatus ())
	      {
	         EXEC SQL close prsicurs;
	         EXEC SQL free prsicurs;
	         return;
	      }
	   }

	   EXEC SQL fetch in prsicurs into :disk.number, :disk.title,
		:disk.b_side, :disk.singer, :disk.pressed, :disk.zeit,
		:disk.zeitb, :disk.lpt;
	}

	EXEC SQL close prsicurs;
	EXEC SQL free prsicurs;

	PsChooseFont (PsHELVETICA, 8);
	PsNewPath ();
	PsSetPosition (526.0, 24.0);
	sprintf (hv0, "Seite: %d", PsGetPage ());
	PsDrawText (hv0);
	PsShowPage ();
	PsClosePrint ();
	XmtDisplayDefaultCursor (toplevel);
}

void PageSingleHeader (float *y)
{
float x;

	x = 46.0;
	PsChooseFont (PsHELVETICA_BOLD, 10);
	PsNewPath ();
	PsSetPosition (x, *y);
	PsDrawText ("Nummer");
	PsNewPath ();
	PsSetPosition (x + 44.0, *y);
	PsDrawText ("Plattentitel");
	PsNewPath ();
	PsSetPosition (x + 257.0, *y);
	PsDrawText ("Interpret(in)");
	PsNewPath ();
	PsSetPosition (x + 492.0, *y);
	PsDrawText ("Zeit");
	PsChooseFont (PsHELVETICA, 10);
	PsNewPath ();
	PsSetMetric (PsPT);
	PsSetLineWidth (1);
	*y = *y - 2.0;
	PsDrawLine (x, *y, x + 524.0, *y);
	PsDrawLine (x + 42.0, *y + 10.0, x + 42.0, 36.0);
	PsDrawLine (x + 255.0, *y + 10.0, x + 255.0, 36.0);
	PsDrawLine (x + 490.0, *y + 10.0, x + 490.0, 36.0);
	PsClosePath (PsSTROKE);
}

/*
 * Folgende Routine erzeugt eine CVS-Datei, welche als Feldtrenner
 * jenes Zeichen enthaelt, das unter Optionen angegeben wurde. Weiters
 * wird auch der unter Optionen angegebene Dateiname verwendet.
 */

void MakeSingleCVS ()
{
EXEC SQL BEGIN DECLARE SECTION;
	disk_t disk;
EXEC SQL END DECLARE SECTION;
int fd;
char hv0[1024], hv1[128];

	if ((fd = open (option.ipFile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
	{
	   XmtDisplayError (toplevel, "ErrorOpenFile", NULL);
	   return;
	}

	EXEC SQL declare prsicvscurs cursor for
		select * from disk order by number;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   return;
	}

	EXEC SQL open prsicvscurs;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   return;
	}
	EXEC SQL fetch in prsicvscurs into :disk.number, :disk.title,
		:disk.b_side, :disk.singer, :disk.pressed, :disk.zeit,
		:disk.zeitb, :disk.lpt;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   EXEC SQL close prsicvscurs;
	   EXEC SQL free prsicvscurs;
	   return;
	}

	sprintf (hv0, "NUMBER%cTITLE%cB_SIDE%cSINGER%cPRESSED%cTIMEA%cTIMEB%cLPT%c\n",
		option.ipTrenn[0], option.ipTrenn[0], option.ipTrenn[0],
		option.ipTrenn[0], option.ipTrenn[0], option.ipTrenn[0],
		option.ipTrenn[0], option.ipTrenn[0]);
	write (fd, hv0, strlen (hv0));

	while (SQLCODE == 0)
	{
	   strcpy (hv1, disk.pressed);
	   rtrim (disk.title);
	   rtrim (disk.b_side);
	   rtrim (disk.singer);
	   rtrim (disk.zeit);
	   rtrim (disk.zeitb);
	   rtrim (disk.lpt);

	   sprintf (hv0, "%ld%c%s%c%s%c%s%c%s%c%s%c%s%c%s%c\n",
		disk.number, option.ipTrenn[0], disk.title, option.ipTrenn[0],
		disk.b_side, option.ipTrenn[0], disk.singer, option.ipTrenn[0],
		hv1, option.ipTrenn[0], disk.zeit, option.ipTrenn[0],
		disk.zeitb, option.ipTrenn[0], disk.lpt, option.ipTrenn[0]);
	   write (fd, hv0, strlen (hv0));

	   EXEC SQL fetch in prsicvscurs into :disk.number, :disk.title,
		:disk.b_side, :disk.singer, :disk.pressed, :disk.zeit,
		:disk.zeitb, :disk.lpt;
	}

	close (fd);
}

void prLps ()
{
EXEC SQL BEGIN DECLARE SECTION;
	lpmaster_t lpmaster;
	lpchild_t lpchild;
EXEC SQL END DECLARE SECTION;
float x, y;
char hv0[256], hv1[256];

	if (!PsOpenPrint ((option.chPrinter == 0) ? option.ipPrinter : option.ipFile, option.chPrinter))
	   return;

	PsNewPage ();
	PsSetMetric (PsMILLIMETER);
	PsChooseFont (PsHELVETICA_BOLD, 18);
	x = 25.6;
	y = 270.0;
	PsNewPath ();
	PsSetPosition (x, y);
	PsDrawText ("Sortierte Liste der Langspielplatten, CDs und MCs");
	y -= 10.0;
	PsChooseFont (PsHELVETICA, 10);

	if (GetPrintStatus ())
	   return;


	/*
	 * Im folgenden werden die Daten aus den Tabellen gelesen und
         * in Postscriptcode umgewandelt.
	 */
	EXEC SQL prepare id_ch from
		'select * from lpchild where snum = ? order by spos
		 for read only';

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   return;
	}

	EXEC SQL declare prlpccurs cursor for id_ch;

	EXEC SQL declare prlpmcurs cursor for
		select * from lpmaster order by lptitel;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   return;
	}

	EXEC SQL open prlpmcurs;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   return;
	}

	EXEC SQL fetch in prlpmcurs into :lpmaster.lpnum, :lpmaster.lptitel,
		:lpmaster.lpkenn, :lpmaster.lpdate, :lpmaster.lpdiscid,
		:lpmaster.lpcategory;

	if (SQLCODE != 0 || CheckSqlError ())
	{
	   DisplaySqlError (toplevel);
	   EXEC SQL close prlpmcurs;
	   EXEC SQL free prlpmcurs;
	   return;
	}

	while (SQLCODE == 0)
	{
	   if (y < 130.0)
	   {
	      PsNewPath ();
	      PsChooseFont (PsHELVETICA, 8);
	      PsSetPosition (x + 160, 20);
	      sprintf (hv0, "Seite: %d", PsGetPage ());
	      PsDrawText (hv0);
	      PsShowPage ();
	      PsNewPage ();
	      PsChooseFont (PsHELVETICA, 10);
	      y = 270.0;

	      if (GetPrintStatus ())
	      {
	         EXEC SQL close prlpmcurs;
	         EXEC SQL free prlpmcurs;
	         return;
	      }
	   }

	   rtrim (&lpmaster.lpcategory[0]);
	   y = PrintLpsHeader (y, &lpmaster);

	   EXEC SQL open prlpccurs using :lpmaster.lpnum;

	   if (SQLCODE != 0 || CheckSqlError ())
	   {
	      DisplaySqlError (toplevel);
	      EXEC SQL close prlpmcurs;
	      EXEC SQL free prlpmcurs;
	      return;
	   }

	   EXEC SQL fetch in prlpccurs into :lpchild.snum, :lpchild.spos,
			:lpchild.stitel, :lpchild.ssinger, :lpchild.szeit;

	   while (SQLCODE == 0)
	   {
	      /*
               * Drucken eines einzelnen Titels.
	       */
	      PsNewPath ();
	      PsSetPosition (x, y);
	      sprintf (hv0, "%d:", lpchild.spos);
	      PsDrawText (hv0);
	      PsNewPath ();
	      PsSetPosition (x + 10, y);
	      PsDrawText (rtrim (lpchild.stitel));
	      PsNewPath ();
	      PsSetPosition (x + 85, y);
	      PsDrawText (rtrim (lpchild.ssinger));
	      PsNewPath ();
	      PsSetPosition (x + 160, y);
	      PsDrawText (lpchild.szeit);
	      y -= 5.0;

	      /*
	       * Naechsten Datensatz holen.
	       */
	      EXEC SQL fetch in prlpccurs into :lpchild.snum, :lpchild.spos,
			:lpchild.stitel, :lpchild.ssinger, :lpchild.szeit;

	      if (SQLCODE == 0 && y < 25)
	      {
	         PsNewPath ();
		 PsChooseFont (PsHELVETICA, 8);
		 PsSetPosition (x + 160, 20);
		 sprintf (hv0, "Seite: %d", PsGetPage ());
		 PsDrawText (hv0);
		 PsShowPage ();
		 PsNewPage ();
		 y = 270;
	         PsChooseFont (PsHELVETICA, 10);
		 y = PrintLpsHeader (y, &lpmaster);

	         if (GetPrintStatus ())
		 {
	            EXEC SQL close prlpmcurs;
	            EXEC SQL free prlpmcurs;
	            EXEC SQL close prlpccurs;
		    EXEC SQL free prlpccurs;
		    return;
		 }
	      }

	   }

	   y -= 10;

	   if (SQLCODE == SQLNOTFOUND)
	      EXEC SQL close prlpccurs;
	   else
	   {
	      int save_status = SQLCODE;

	      if (has_trans)
		 EXEC SQL rollback work;

	      SQLCODE = save_status;
	   }

	   if (SQLCODE == 0 || SQLCODE == SQLNOTFOUND)
	      EXEC SQL fetch in prlpmcurs into :lpmaster.lpnum, :lpmaster.lptitel,
		:lpmaster.lpkenn, :lpmaster.lpdate, :lpmaster.lpdiscid,
		:lpmaster.lpcategory;
	}

	if (SQLCODE < 0)
	   DisplaySqlError (toplevel);

	PsShowPage ();
	PsClosePrint ();
	EXEC SQL close prlpmcurs;
	EXEC SQL free prlpmcurs;
	EXEC SQL free prlpccurs;
}

float PrintLpsHeader (float gy, lpmaster_t *lpmaster)
{
float x, y;
char hv0[256], hv1[128];

	x = 25.6;
	y = gy;
	PsNewPath ();
	PsSetPosition (x, y);

	if (!strcmp (lpmaster->lpkenn, "CD"))
	   sprintf (hv1, "Kategorie/ID: %s (%08x)", lpmaster->lpcategory,
		lpmaster->lpdiscid);
	else
	   sprintf (hv1, "Kategorie: %s", lpmaster->lpcategory);

	sprintf (hv0, "Nummer / Typ: %ld / %s     %s", lpmaster->lpnum,
		lpmaster->lpkenn, hv1);
	PsDrawText (hv0);
	PsNewPath ();
	PsSetPosition (x + 120.0, y);
	strcpy (hv1, lpmaster->lpdate);
	sprintf (hv0, "Pressdatum: %s", hv1);
	PsDrawText (hv0);
	y -= 5.0;
	sprintf (hv0, "Tonträgertitel: %s", rtrim (lpmaster->lptitel));
	PsNewPath ();
	PsSetPosition (x, y);
	PsDrawText (hv0);
	y -= 10.0;
	PsChooseFont (PsHELVETICA_BOLD, 10);
	PsNewPath ();
	PsSetPosition (x, y);
	PsDrawText ("Pos.");
	PsNewPath ();
	PsSetPosition (x + 10.0, y);
	PsDrawText ("Songtitel");
	PsNewPath ();
	PsSetPosition (x + 85.0, y);
	PsDrawText ("Interpret(in) / Gruppe");
	PsNewPath ();
	PsSetPosition (x + 160.0, y);
	PsDrawText ("Zeit");
	PsChooseFont (PsHELVETICA, 10);
	PsNewPath ();
	PsSetLineWidth (1);
	PsDrawLine (x, y - 1.0, x + 170.0, y - 1.0);
	PsClosePath (PsSTROKE);
	y -= 6.0;
	return y;
}