- Now splits paragraphs into multiple lines, with each line fitting the
[AROS.git] / workbench / devs / printtofile / printtofile.c
blobe95c40b8ed43476dec9291b2de06c94e6ea2a106
1 /*
2 * Copyright (C) 2012, The AROS Development Team. All rights reserved.
3 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
5 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
6 */
8 #include <aros/debug.h>
10 #include <exec/errors.h>
12 #include <proto/exec.h>
13 #include <proto/dos.h>
14 #include <proto/asl.h>
16 #include LC_LIBDEFS_FILE
18 #include "printtofile_intern.h"
20 BOOL pu_OpenFile(struct PrintToFileBase *PrintToFileBase,
21 struct PrintToFileUnit *pu)
23 struct FileRequester *fr;
24 BOOL ok;
26 ObtainSemaphore(&pu->pu_Lock);
27 if (!pu->pu_FileReq) {
28 fr = AllocAslRequestTags(ASL_FileRequest,
29 ASLFR_TitleText,"File to print to...",
30 ASLFR_DoSaveMode, TRUE,
31 ASLFR_RejectIcons, TRUE,
32 TAG_END);
33 if (fr == NULL) {
34 ReleaseSemaphore(&pu->pu_Lock);
35 return IOERR_OPENFAIL;
37 pu->pu_FileReq = fr;
38 } else {
39 fr = pu->pu_FileReq;
42 D(bug("fr = %p\n", fr));
43 ok = AslRequestTags(fr, TAG_END);
44 D(bug("ok = %d (%s %s)\n", ok, fr-fr_Drawer, fr->fr_File));
46 if (ok) {
47 BPTR here;
49 if (fr->fr_Drawer) {
50 BPTR lock = Lock(fr->fr_Drawer, SHARED_LOCK);
51 if (!lock)
52 return IOERR_OPENFAIL;
53 here = CurrentDir(lock);
54 } else {
55 here = CurrentDir(BNULL);
56 CurrentDir(here);
59 if (fr->fr_File) {
60 pu->pu_File = Open(fr->fr_File, MODE_NEWFILE);
61 } else {
62 pu->pu_File = BNULL;
64 D(bug("pu->pu_File = %p\n", BADDR(pu->pu_File)));
66 CurrentDir(here);
69 if (pu->pu_File == BNULL) {
70 ReleaseSemaphore(&pu->pu_Lock);
71 return IOERR_OPENFAIL;
74 return 0;
77 AROS_LH1(void, BeginIO,
78 AROS_LHA(struct IOStdReq *, io, A1),
79 struct PrintToFileBase *, PrintToFileBase, 5, PrintToFile)
81 AROS_LIBFUNC_INIT
83 struct PrintToFileUnit *pu = (struct PrintToFileUnit *)io->io_Unit;
84 LONG err;
86 D(bug("BeginIO: io_Command = %d\n", io->io_Command));
87 D(bug("pu_File=%p\n", BADDR(pu->pu_File)));
88 if (pu->pu_File == BNULL) {
89 err = pu_OpenFile(PrintToFileBase, pu);
90 if (err != 0) {
91 D(bug("err = %d\n", err));
92 io->io_Error = err;
93 goto end;
97 switch (io->io_Command) {
98 case CMD_FLUSH:
99 case CMD_UPDATE:
100 io->io_Error = 0;
101 break;
102 case CMD_WRITE:
103 err = Write(pu->pu_File, io->io_Data, io->io_Length);
104 if (err < 0) {
105 io->io_Error = IoErr();
106 io->io_Actual = 0;
107 } else {
108 io->io_Error = 0;
109 io->io_Actual = err;
111 break;
112 default:
113 io->io_Error = IOERR_NOCMD;
114 break;
117 end:
118 if (!(io->io_Flags & IOF_QUICK))
119 ReplyMsg(&io->io_Message);
120 return;
122 AROS_LIBFUNC_EXIT
125 AROS_LH1(LONG, AbortIO,
126 AROS_LHA(struct IORequest *, io, A1),
127 struct PrintToFileBase *, PrintToFileBase, 6, PrintToFile)
129 AROS_LIBFUNC_INIT
130 return IOERR_NOCMD;
131 AROS_LIBFUNC_EXIT
134 static int GM_UNIQUENAME(Init)(struct PrintToFileBase *PrintToFileBase)
136 int i;
138 for (i = 0; i < PRINTTOFILE_UNITS; i++) {
139 InitSemaphore(&PrintToFileBase->pf_Unit[i].pu_Lock);
142 if ((PrintToFileBase->pf_DOSBase = OpenLibrary("dos.library", 35))) {
143 if ((PrintToFileBase->pf_AslBase = OpenLibrary("asl.library", 36))) {
144 D(bug("initted\n"));
145 return TRUE;
147 CloseLibrary(PrintToFileBase->pf_DOSBase);
150 return FALSE;
153 static int GM_UNIQUENAME(Expunge)(struct PrintToFileBase *PrintToFileBase)
155 int i;
157 for (i = 0; i < PRINTTOFILE_UNITS; i++) {
158 FreeFileRequest(PrintToFileBase->pf_Unit[i].pu_FileReq);
161 CloseLibrary(PrintToFileBase->pf_AslBase);
162 CloseLibrary(PrintToFileBase->pf_DOSBase);
163 D(bug("expunged\n"));
165 return TRUE;
168 static int GM_UNIQUENAME(Open)(struct PrintToFileBase *PrintToFileBase, struct IOStdReq* io, ULONG unitnum, ULONG flags)
170 D(bug("open unit %d\n", unitnum));
171 if (unitnum < 0 || unitnum >= PRINTTOFILE_UNITS)
172 return FALSE;
174 io->io_Unit = (struct Unit *)&PrintToFileBase->pf_Unit[unitnum];
175 D(bug("io unit %p\n", io->io_Unit));
176 return TRUE;
179 static int GM_UNIQUENAME(Close)(struct PrintToFileBase *PrintToFileBase, struct IOStdReq *io)
181 struct PrintToFileUnit *pu = (struct PrintToFileUnit *)io->io_Unit;
183 D(bug("close: %p , %p\n", pu, BADDR(pu->pu_File)));
184 io->io_Unit = NULL;
185 if (pu->pu_File == BNULL)
186 return TRUE;
188 Close(pu->pu_File);
189 pu->pu_File = BNULL;
190 ReleaseSemaphore(&pu->pu_Lock);
192 return TRUE;
195 ADD2OPENDEV(GM_UNIQUENAME(Open), 0)
196 ADD2CLOSEDEV(GM_UNIQUENAME(Close), 0)
198 ADD2INITLIB(GM_UNIQUENAME(Init), 0)
199 ADD2EXPUNGELIB(GM_UNIQUENAME(Expunge), 0)