1 /*
2 * fjobs.c -- Recording information about the file jobs
3 * Copyright (c) 1995-99 Akim Demaille, Miguel Santana
4 */
5
6 /*
7 * This file is part of a2ps.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 */
24
25 #include "a2ps.h"
26 #include "jobs.h"
27 #include "fjobs.h"
28 #include "routines.h"
29
30 typedef struct file_job fjob_t;
31
32 /*
33 * Creating/Removing a file_job named NAME, which is the NUM job,
34 * which tmp file is in TMPDIR, and mod time is default to RUN_TM
35 *
36 */
37 fjob_t *
_a2ps_file_job_new(uchar * name,int num,struct tm * run_tm)38 _a2ps_file_job_new (uchar *name, int num, struct tm *run_tm)
39 {
40 NEW (fjob_t, res);
41
42 /* liba2ps.h must not try to read a file. This is the job of its
43 clients. Hence, make sure, at least, to have reasonable values. */
44 res->name = name;
45 res->delegation_tmpname = NULL;
46 res->stdin_tmpname = NULL;
47
48 /* The type of the file is unknown */
49 res->type = NULL;
50
51 res->is_toc = false;
52
53 /* Set date to now */
54 res->mod_tm = *run_tm;
55
56 /* If for some reason (is a dir, is not readable...), will
57 * be later set to false */
58 res->printable = true;
59
60 /* By default it is not supposed to be stdin */
61 res->is_stdin = false;
62
63 /* Initialize the file dependant counters */
64 /* Add one to job->sheets and pages, because this initialization
65 * is done before the job went to the next page */
66
67 res->first_sheet = 0;
68 res->sheets = 0;
69 res->last_sheet = 0;
70 res->first_page = 0;
71 res->pages = 0;
72 res->last_page = 0;
73 res->top_line = 1;
74 res->top_page = 0;
75 res->lines = 1;
76 res->num = num;
77
78 return res;
79 }
80
81 /*
82 * Release a file_job
83 */
84 void
file_job_free(fjob_t * file_job)85 file_job_free (fjob_t *file_job)
86 {
87 XFREE (file_job->delegation_tmpname);
88 XFREE (file_job->stdin_tmpname);
89 free (file_job);
90 }
91
92 void
file_job_self_print(fjob_t * file,FILE * stream)93 file_job_self_print (fjob_t *file, FILE *stream)
94 {
95 fprintf (stream,
96 "File `%s': Pages %d-%d (%d), Sheets %d-%d (%d)\n",
97 file->name,
98 file->first_page, file->last_page, file->pages,
99 file->first_sheet, file->last_sheet, file->sheets);
100 fprintf (stream,
101 "\tdel-tmp: `%s', stdin-tmp: `%s', is_toc: %d",
102 UNNULL (file->delegation_tmpname),
103 UNNULL (file->stdin_tmpname),
104 file->is_toc);
105 }
106
107 /*
108 * Synchronise the current file informations onto JOB
109 */
110 void
file_job_synchronize_sheets(struct a2ps_job * job)111 file_job_synchronize_sheets (struct a2ps_job * job)
112 {
113 fjob_t * file = CURRENT_FILE (job);
114
115 /* Set the number of sheets printed */
116 if (!file->first_sheet)
117 file->first_sheet = job->sheets;
118
119 /* Is this actually a sheet number we never saw? */
120 file->last_sheet = job->sheets;
121 file->sheets = file->last_sheet - file->first_sheet + 1;
122 }
123
124 /*
125 * Synchronise the current file informations onto JOB
126 */
127 void
file_job_synchronize_pages(struct a2ps_job * job)128 file_job_synchronize_pages (struct a2ps_job * job)
129 {
130 fjob_t * file = CURRENT_FILE (job);
131
132 /* Set the number of pages/lines printed */
133 if (!file->first_page)
134 file->first_page = job->pages;
135
136 if (!file->top_page)
137 file->top_page = 1;
138
139 file->last_page = job->pages;
140 file->top_line = CURRENT_FILE (job)->lines;
141
142 file->pages = file->last_page - file->first_page + 1;
143 }
144
145 /*
146 * Release the temp file associated to that file_job
147 */
148 void
file_job_unlink_tmpfile(fjob_t * file)149 file_job_unlink_tmpfile (fjob_t * file)
150 {
151 if (file->delegation_tmpname)
152 unlink (file->delegation_tmpname);
153 if (file->stdin_tmpname)
154 unlink (file->stdin_tmpname);
155 }
156
157 /*
158 * Compare two files upon the name
159 */
160 int
file_name_cmp(fjob_t * f1,fjob_t * f2)161 file_name_cmp (fjob_t *f1, fjob_t *f2)
162 {
163 return ustrcmp (f1->name, f2->name);
164 }
165