4 ** Copyright (C) 1993,94,95,96 Bernardo Innocenti
6 ** Parts of this file are:
8 ** Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
11 ** Miscellaneus useful functions
14 #include <exec/ports.h>
15 #include <workbench/startup.h>
17 #include <proto/exec.h>
18 #include <proto/dos.h>
19 #include <proto/icon.h>
20 #include <proto/graphics.h>
22 #include "XModulePriv.h"
26 #if 0 /* --- This part has been removed --- */
28 /* Function pointers to Kickstart-sensitive RastPort query code */
29 ULONG (*ReadAPen)(struct RastPort *RPort);
30 ULONG (*ReadBPen)(struct RastPort *RPort);
31 ULONG (*ReadDrMd)(struct RastPort *RPort);
35 /* Local function prototypes */
36 static ULONG OldGetAPen (struct RastPort *RPort);
37 static ULONG OldGetBPen (struct RastPort *RPort);
38 static ULONG OldGetDrMd (struct RastPort *RPort);
39 static ULONG NewGetAPen (struct RastPort *RPort);
40 static ULONG NewGetBPen (struct RastPort *RPort);
41 static ULONG NewGetDrMd (struct RastPort *RPort);
43 #endif /* --- This part has been removed --- */
47 GLOBALCALL struct DiskObject *GetProgramIcon (void)
49 /* Get program associated icon.
50 * This function will fail if we are not a son of Workbench.
51 * The returned DiskObject must be freed by FreeDiskObject().
54 struct DiskObject *dobj;
57 if (!WBenchMsg) return NULL;
59 olddir = CurrentDir (WBenchMsg->sm_ArgList->wa_Lock);
60 dobj = GetDiskObject (WBenchMsg->sm_ArgList->wa_Name);
68 GLOBALCALL struct Library *MyOpenLibrary (CONST_STRPTR name, ULONG ver)
73 while (!(lib = OpenLibrary (name, ver)))
75 UBYTE path[PATHNAME_MAX];
78 /* Search the library file in "PROGDIR:"... */
80 strcpy (path, "PROGDIR:");
81 AddPart (path, name, PATHNAME_MAX);
82 if (lib = OpenLibrary (path, ver))
85 strcpy (path, "PROGDIR:Libs");
86 AddPart (path, name, PATHNAME_MAX);
87 if (lib = OpenLibrary (path, ver))
90 /* Tell the user we can't find this library anywhere... */
91 if (!ShowRequest (MSG_OPENLIB_FAIL, MSG_RETRY_OR_CANCEL, name, ver))
95 if (progdir) UnLock (progdir);
102 GLOBALCALL void CantOpenLib (CONST_STRPTR name, LONG ver)
104 /* Notify the user that a library didn't open */
107 ShowRequest (MSG_OPENLIB_VER_FAIL, MSG_CONTINUE, name, ver);
109 ShowRequest (MSG_OPENLIB_FAIL, MSG_CONTINUE, name);
114 GLOBALCALL void KillMsgPort (struct MsgPort *mp)
116 /* Reply all pending messages and call DeletePort() */
120 Forbid(); /* is this really useful? */
122 /* Reply all pending Messages */
123 while (msg = GetMsg (mp))
133 GLOBALCALL struct TextAttr *CopyTextAttrPooled (void *pool, const struct TextAttr *source, struct TextAttr *dest)
135 /* Copy <source> textattr structure over <dest>, allocating and copying
136 * the ta_Name field. <dest>->ta_Name if FreeVec()ed before
137 * allocating the new one.
139 * Returns: <dest> if everything was ok, NULL for failure.
143 FreeVecPooled (pool, dest->ta_Name);
145 memcpy (dest, source, sizeof (struct TextAttr));
147 if (dest->ta_Name = AllocVecPooled (pool, strlen (source->ta_Name) + 1))
149 strcpy (dest->ta_Name, source->ta_Name);
157 GLOBALCALL UWORD CmpTextAttr (const struct TextAttr *ta1, const struct TextAttr *ta2)
159 /* Compares two TextAttr structures and returns 0 if they refer to
160 * the same font, a non-zero value otherwise.
163 if (ta1->ta_YSize == ta2->ta_YSize && ta1->ta_Style == ta2->ta_Style)
165 if (!ta1->ta_Name && !ta2->ta_Name)
168 if (!ta1->ta_Name || !ta2->ta_Name)
171 if (!strcmp (ta1->ta_Name, ta2->ta_Name))
180 /* More memory pools support */
184 GLOBCALL void *AllocVecPooled (void *pool, ULONG size)
191 if (mem = AllocPooled (pool, size))
193 if (mem = AsmAllocPooled (pool, size))
195 *((ULONG *)mem)++ = size;
202 GLOBCALL void FreeVecPooled (void *pool, void *memory)
208 FreePooled (pool, mem, *((ULONG *)mem));
210 AsmFreePooled (pool, mem, *((ULONG *)mem));
217 GLOBCALL void *CAllocVecPooled (void *pool, ULONG size)
225 if (mem = AllocPooled (pool, size))
227 if (mem = AsmAllocPooled (pool, size))
230 memset (mem, size, 0);
231 *((ULONG *)mem)++ = size;
237 #endif /* PORTABLE */
241 GLOBALCALL STRPTR DupStringPooled (void *pool, CONST_STRPTR source, STRPTR *dest)
243 /* Allocates a buffer big enough to fit the <source> string
244 * using AllocVecPooled() and then copies the contents of <source>
245 * there. If the buffer pointed by <dest> is not NULL,
246 * DupStringPooled() will first check to see if <source> and
247 * <dest> are identical, in which case no new buffer will be
248 * allocated and the result will be FALSE. Otherwise, <dest> will
249 * be deallocated and a new buffer will be allocated. Both <source> and <*dest>
253 * Non-zero if <dest> has been reallocated, FALSE otherwise.
260 if (!(strcmp (*dest, source)))
264 FreeVecPooled (pool, *dest);
269 if (*dest = AllocVecPooled (pool, strlen (source) + 1))
270 strcpy (*dest, source);
278 GLOBALCALL void FilterName (STRPTR name)
280 /* Finds and blanks out invalid characters in a string.
281 * Will also strip blanks at the end. Passing NULL is safe.
291 (name[i] >'~' && name[i] < '¡'))
297 /* Kill blanks at the end of the string */
298 for (--i; i > 0 ; i--)
299 if (name[i] == ' ') name[i] = '\0';
305 GLOBALCALL LONG PutIcon (CONST_STRPTR source, CONST_STRPTR dest)
307 /* Add the <source> icon to <dest> file */
309 struct DiskObject *dobj;
310 UBYTE buf[PATHNAME_MAX];
312 /* We do not alter existing icons */
313 if (dobj = GetDiskObject (dest))
315 FreeDiskObject (dobj);
319 /* Get source icon */
321 strcpy (buf, "PROGDIR:Icons");
322 AddPart (buf, source, PATHNAME_MAX);
323 if (!(dobj = GetDiskObject (buf)))
325 strcpy (buf, "ENV:Sys");
326 AddPart (buf, source, PATHNAME_MAX);
327 if (!(dobj = GetDiskObject (buf)))
329 /* Get default project icon */
330 dobj = GetDefDiskObject (WBPROJECT);
336 dobj->do_CurrentX = dobj->do_CurrentY = NO_ICON_POSITION;
338 if (!(dobj->do_DefaultTool[0]))
340 /* Get program path and store in icon's Default Tool */
344 dobj->do_DefaultTool = NULL;
346 if (WBenchMsg) /* WB */
347 progdir = WBenchMsg->sm_ArgList->wa_Lock;
349 progdir = GetProgramDir();
353 if (NameFromLock (progdir, buf, PATHNAME_MAX))
357 if (WBenchMsg) /* WB */
358 strncpy (progname, WBenchMsg->sm_ArgList->wa_Name, 32);
360 GetProgramName (progname, 32);
362 if(AddPart (buf, progname, PATHNAME_MAX))
363 dobj->do_DefaultTool = buf;
368 if (!dobj->do_DefaultTool) dobj->do_DefaultTool = BaseName;
369 PutDiskObject (dest, dobj);
370 FreeDiskObject (dobj);
379 static ULONG MakeBackupName (STRPTR buf, CONST_STRPTR s, CONST_STRPTR tmpl, ULONG n)
382 * Subfunction for BackupFile() - Constructs a file name for the backup.
383 * <tmpl> is the template to build the backup name.
384 * Each occurence of the '#' character in the template is replaced with
385 * an ASCII decimal representation of <n>. The '*' character is replaced
386 * with the original filename <s>.
389 * The result is stored in <buf>. Returns TRUE if at least one occurrence
390 * of the '#' wildcard has been replaced, otherwise returns FALSE.
393 * Does not check for buffer overflow.
397 BOOL wild_done = FALSE;
398 BOOL num_done = FALSE;
402 if ((*tmpl == '*') && (!wild_done))
407 while (*buf++ = *s++);
412 else if (*tmpl == '#')
414 /* Insert backup number */
417 *buf++ = (n / 10) + '0';
418 *buf++ = (n % 10) + '0';
422 else if (*tmpl == '\\')
423 /* Handle escape character */
435 GLOBALCALL LONG BackupFile (CONST_STRPTR src, CONST_STRPTR template, ULONG versions)
437 /* Creates backups of the given file */
439 UBYTE buf[PATHNAME_MAX], oldbuf[PATHNAME_MAX];
441 BPTR lock, dir, olddir;
443 if (!(lock = Lock (src, ACCESS_READ)))
446 /* CD to source directory: needed for relative paths */
447 dir = ParentDir (lock);
448 olddir = CurrentDir (dir);
451 if (!MakeBackupName (buf, src, template, 1))
458 /* Delete oldest backup if exists */
460 MakeBackupName (buf, src, template, versions);
463 /* Shift all others ahead... */
465 for (i = versions; i > 0; --i)
467 strcpy (oldbuf, buf);
468 MakeBackupName (buf, src, template, i);
469 Rename (buf, oldbuf);
471 DB (kprintf ("Backup name is: %s\n", buf));
475 /* And finally move file to its backup location */
478 /* Restore old current directory */
488 #if 0 /* --- This part has been removed --- */
490 GLOBALCALL void InstallGfxFunctions (void)
492 /* Install the correct routines to query
493 * the rendering colours and drawing mode.
496 if(GfxBase->lib_Version >= 39)
498 ReadAPen = NewGetAPen;
499 ReadBPen = NewGetBPen;
500 ReadDrMd = NewGetDrMd;
504 ReadAPen = OldGetAPen;
505 ReadBPen = OldGetBPen;
506 ReadDrMd = OldGetDrMd;
512 /* OldGetAPen(struct RastPort *RPort):
514 * Query the current primary rendering colour (old style).
517 static ULONG OldGetAPen (struct RastPort *RPort)
519 return((ULONG)RPort->FgPen);
524 /* OldGetBPen(struct RastPort *RPort):
526 * Query the current seconary rendering colour (old style).
529 static ULONG OldGetBPen (struct RastPort *RPort)
531 return((ULONG)RPort->BgPen);
536 /* OldGetDrMd(struct RastPort *RPort):
538 * Query the current drawing mode (old style).
541 static ULONG OldGetDrMd (struct RastPort *RPort)
543 return((ULONG)RPort->DrawMode);
548 /* NewGetAPen(struct RastPort *RPort):
550 * Query the current primary rendering colour (new style).
553 static ULONG NewGetAPen (struct RastPort *RPort)
555 return(GetAPen (RPort));
560 /* NewGetBPen(struct RastPort *RPort):
562 * Query the current seconary rendering colour (new style).
565 static ULONG NewGetBPen (struct RastPort *RPort)
567 return (GetBPen (RPort));
572 /* NewGetDrMd(struct RastPort *RPort):
574 * Query the current drawing mode (new style).
577 static ULONG NewGetDrMd (struct RastPort *RPort)
579 return(GetDrMd (RPort));
582 #endif /* --- This part has been removed --- */