[alt.sources.amiga] Simple Lattice C front-end for the ARP File requestor

jkh@MEEPMEEP.PCS.COM (Jordan K. Hubbard) (05/07/91)

Seems like this might save folks some work, so here it is..

It was my first attempt at doing anything under Lattice C for
the Amiga, so be kind..

					Jordan

#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 04/25/1991 17:02 UTC by jkh@meepmeep
# Source directory /usr3
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#    798 -rw-r--r-- test.c
#   2619 -rw-r--r-- arpreq.c
#    769 -rw-r--r-- arpreq.h
#
# ============= test.c ==============
if test -f 'test.c' -a X"$1" != X"-c"; then
	echo 'x - skipping test.c (File already exists)'
else
echo 'x - extracting test.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'test.c' &&
/*
X * Short test program for file requestor front-end.
X *
X * April 3rd, 1991.
X * Jordan K. Hubbard
X *
X */
X
#include "arpreq.h"
X
/*
X * We _must_ declare this here or the file requestor will not link.
X * I used to declare it in the file requestor code directly then realized
X * that this would hose an application using the arp requestor somewhere
X * else. Better to just have the "application" declare it. The file requestor
X * front-end will open it only if necessary.
X */
X
APTR ArpBase;
X
main()
{
X     char file[64], dir[64];
X     char path[128];
X
X     /* Set initial contents */
X     file[0] = '\0';
X     strcpy(dir, "SYS:");
X
X     if (ArpRequestFile(0, "This is a test", file, dir, path))
X	  printf("You selected %s\n", path);
X     else
X	  printf("File selection operation was cancelled.\n");
}
SHAR_EOF
chmod 0644 test.c ||
echo 'restore of test.c failed'
Wc_c="`wc -c < 'test.c'`"
test 798 -eq "$Wc_c" ||
	echo 'test.c: original size 798, current size' "$Wc_c"
fi
# ============= arpreq.c ==============
if test -f 'arpreq.c' -a X"$1" != X"-c"; then
	echo 'x - skipping arpreq.c (File already exists)'
else
echo 'x - extracting arpreq.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'arpreq.c' &&
#include "arpreq.h"
X
#pragma libcall ArpBase FileRequest 126 801
X
/*
X * April 3rd, 1991.
X * Jordan K. Hubbard
X * (My first amiga program! Wow! Golly!)
X *
X * This routine front-ends the ARP standard file requester.
X *
X * Calling syntax:
X *
X * int status;
X * status = ArpRequestFile(win, title, file, dir, path)
X * struct Window *win;
X * char *title, *file, *dir, *path;
X *
X * "win" is a pointer to a window structure - used solely
X * to determine which screen the requestor should pop up on (e.g. it
X * will share "window"'s screen). If NULL, the workbench screen is used.
X *
X * "title" is a constant text string used to display the banner message
X * at the top of the requestor.
X *
X * "file" and "dir" are pointers to space that the USER has allocated
X * to hold the resulting directory and file components of the selected
X * pathname.
X *
X * "path" is a convenience variable that will contain a *copy* of file
X * and dir (and so must also have sufficient space allocated for it by the
X * user) properly joined so that the resulting selected file can be
X * directly open()'d regardless of current directory. If NULL, it will
X * be ignored. "file" and "dir" remain initializaed properly in either
X * case.
X *
X * status is returned as TRUE (!0) if user has selected a file or FALSE (0)
X * if the selection was CANCEL'd.
X *
X */
X
static void pathify(char *, char *, char *);
X
int ArpRequestFile(struct Window *w, char *title, char *file, char *dir,
X		   char *path)
{
X     ArpFileRequester req;
X     int status, opened = 0;
X     
X     /* Open the ARP library if application hasn't already done so */
X     if (!ArpBase) {
X	  ArpBase = (APTR)OpenLibrary("arp.library", 0);
X	  if (ArpBase == (APTR)0) {
X	       printf("Can't open arp.library.\n");
X	       return 0;
X	  }
X	  else /* make sure I remember to close it */
X	       opened = 1;
X     }
X     /* Set up the important fields */
X     req.title = title;
X     req.file = file;
X     req.dir = dir;
X     req.window = w;
X     req.funcFlags = 0;	/* no custom function yet */
X     status = FileRequest(&req);
X     if (opened)	/* If I opened it, be neat */
X	  CloseLibrary(ArpBase);
X     if (status && path)
X	  pathify(path, dir, file);
X     return status;
}
X
/*
X * glue a directory and file specification together in such a way
X * that they form a complete path name that can be passed to open().
X */
static void pathify(char *path, char *dir, char *file)
{
X     int len = strlen(dir);
X     
X     if (!len)
X	  path[0] = '\0';
X     else {
X	  strcpy(path, dir);
X	  if (dir[len - 1] != ':') {
X	       path[len] = '/';
X	       path[len + 1] = '\0';
X	  }
X     }
X     strcat(path, file);
}
SHAR_EOF
chmod 0644 arpreq.c ||
echo 'restore of arpreq.c failed'
Wc_c="`wc -c < 'arpreq.c'`"
test 2619 -eq "$Wc_c" ||
	echo 'arpreq.c: original size 2619, current size' "$Wc_c"
fi
# ============= arpreq.h ==============
if test -f 'arpreq.h' -a X"$1" != X"-c"; then
	echo 'x - skipping arpreq.h (File already exists)'
else
echo 'x - extracting arpreq.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'arpreq.h' &&
/*
X * This is my first shot at defining the C glue necessary to use the
X * ARP file requester from C.
X *
X * April 3rd, 1991
X * Jordan K. Hubbard
X *
X */
X
#include <exec/types.h>
#include <intuition/intuition.h>
X
struct _filereq {
X     char *title;		/* Title of file requester	*/
X     char *file;		/* File name			*/
X     char *dir;			/* Starting directory		*/
X     struct Window *window;	/* Window to share screen with	*/
X     UBYTE funcFlags;		/* Tell ARP to call our stuff	*/
X     UBYTE reserved1;		/* like it says - hands off	*/
X     APTR func;			/* Custom callback		*/
X     ULONG reserved2;		/* No touchie here neither	*/
};
typedef struct _filereq ArpFileRequester;
X
extern int ArpRequestFile(struct Window *w, char *title, char *file,
X			  char *dir, char *path);
X
SHAR_EOF
chmod 0644 arpreq.h ||
echo 'restore of arpreq.h failed'
Wc_c="`wc -c < 'arpreq.h'`"
test 769 -eq "$Wc_c" ||
	echo 'arpreq.h: original size 769, current size' "$Wc_c"
fi
exit 0

peter@sugar.hackercorp.com (Peter da Silva) (05/09/91)

I wish I'd thought of this for my file requestor sample program:

In article <m0ja9DE-0003FIC@meepmeep.pcs.com> jkh@MEEPMEEP.PCS.COM (Jordan K. Hubbard) writes:
> Seems like this might save folks some work, so here it is..

If you make the "main" look like this it'd actually be useful:

! main(ac, av)
+ int ac;
+ char **av;
> {
>      char file[64], dir[64];
>      char path[128];
> 
>      /* Set initial contents */
>      file[0] = '\0';
!      strcpy(dir, ac>2?av[2]:"SYS:");
> 
!      if (ArpRequestFile(0, ac>1?av[1]:"Enter file name", file, dir, path))
! 	  printf("%s\n", path);
>      else
! 	  return 5;
+      return 0;
> }

(changed lines marked with !, new lines with +)

This way you can use it as:

arprequest >t:tempfile "Prompt" [initial:directory]
if not warn
  do_something_with_tempfile
endif

Or even, for the courageous:

arprequest >env:tempfile ...
-- 
Peter da Silva.   `-_-'
<peter@sugar.hackercorp.com>.