Article View: comp.windows.open-look
Article #2879Extras menu: replacement for Sun's capitalize program
From: Liam R. E. Quin
Date: Sun, 24 May 1992 03:59
Date: Sun, 24 May 1992 03:59
249 lines
6106 bytes
6106 bytes
If you use SunView or OpenWindows or the XView toolkit, you'll have noticed
the Extras->Capitalize menu. This lets you select a string ("happy boy", say)
and turn it into "Happy Boy", "HAPPY BOY" etc.
But it doesn't work with accented characters, and "abc def -> Abc Def" mode
doesn't work if the string was in ALL CAPS.
This version fixes that, and addes a -L locale option.
To test it, you might need to make use of the enclosed 8bit shell script.
Given no arguments, this version doesn't have Sun's heuristic behaviour.
Also, note the different spelling, capitalize vs. capitalise.
: To unbundle, sh this file
echo x - Makefile 1>&2
sed 's/^X//' >Makefile <<'@@@End of Makefile'
XPROG=capitalise
XDESTDIR=/usr/local/bin
X
Xcapitalise: capitalise.o
X $(CC) $(CFLAGS) -o capitalise capitalise.o
X
Xinstall: $(PROG)
X install -s capitalise $(DESTDIR)
X
Xclean: tidy
X rm -f capitalise
X
Xtidy:
X rm -f *.o core *.out
@@@End of Makefile
echo x - capitalise.c 1>&2
sed 's/^X//' >capitalise.c <<'@@@End of capitalise.c'
X/* capitalise.c -- Copyright 1992 Liam R. E. Quin (lee@sq.com).
X *
X * $Id: capitalise.c,v 1.1 92/05/23 23:58:58 lee Exp $
X */
X
X#include <stdio.h>
X#include <locale.h>
X#include <ctype.h>
X
Xextern int errno;
X
Xchar *Revision = "@(#) $Id: capitalise.c,v 1.1 92/05/23 23:58:58 lee Exp $";
Xchar *progname = "captialise";
X
Xstatic void AllToLower();
Xstatic void FirstToUpper();
Xstatic void FirstToUpperAndRestToLower();
Xstatic void AllToUpper();
Xstatic void (* FileFunc)() = FirstToUpperAndRestToLower;
X
Xint
Xmain(argc, argv)
X int argc;
X char *argv[];
X{
X extern char *getenv();
X extern char *setlocale();
X extern int optind, getopt();
X extern char *optarg;
X int ch;
X char *Locale;
X int ErrorFlag = 0;
X
X progname = argv[0];
X
X if ((Locale = getenv("LC-CTYPE")) == (char *) NULL) {
X Locale = "iso_8859_1";
X }
X
X (void) setlocale(LC_CTYPE, Locale); /* don't care if it fails */
X
X while ((ch = getopt(argc, argv, "ulcwL:xXhHvV")) != EOF) {
X switch (ch) {
X case 'L':
X Locale = optarg;
X if (setlocale(LC_CTYPE, Locale) == (char *) NULL) {
X int e = errno;
X fprintf(stderr, "%s: couldn't set locale ", progname);
X errno = e;
X perror(Locale);
X /* but don't exit, in case w're being used as a
X * cut and paste filter...
X */
X }
X break;
X case 'c':
X FileFunc = FirstToUpper;
X break;
X case 'l':
X FileFunc = AllToLower;
X break;
X case 'u':
X FileFunc = AllToUpper;
X break;
X case 'w':
X FileFunc = FirstToUpperAndRestToLower;
X break;
X case 'x':
X case 'X':
X case 'h':
X case 'H':
X ErrorFlag = (-1);
X break;
X case 'v':
X case 'V':
X fprintf(stderr, "%s: %s\n", progname, Revision);
X break;
X case '?':
X ErrorFlag = 1;
X }
X }
X
X if (ErrorFlag) {
X fprintf(stderr, "Usage: %s [options] [file...]\n", progname);
X fprintf(stderr, "%s: options are:\n", progname);
X fputs("\
X -L str -- set LC_CTYPE locale to str [default: iso_8859_1]\n\
X -l -- convert all characters to lower case\n\
X -c -- Convert Only The First Character Of Each Word\n\
X -w -- Force Words To Look Like This [default]\n\
X -u -- CONVERT EVERY CHARACTER TO UPPER CASE\n\
X", stderr);
X
X exit( ErrorFlag > 0 ? 1 : 0); /* 0 means -x was used */
X }
X
X if (optind >= argc) {
X Process("-");
X } else {
X while (optind < argc) {
X Process(argv[optind++]);
X }
X }
X
X exit(0);
X}
X
XProcess(FileName)
X char *FileName;
X{
X if (FileName[0] == '-' && FileName[1] == '\0') {
X (* FileFunc)(stdin);
X } else {
X FILE *Fp;
X
X if ((Fp = fopen(FileName, "r")) == (FILE *) NULL) {
X int e = errno;
X
X fprintf(stderr, "%s: input file ", progname);
X errno = e;
X perror(FileName);
X exit(1);
X }
X
X (* FileFunc)(Fp);
X
X fclose(Fp);
X }
X}
X
Xstatic void
XAllToUpper(Fp)
X FILE *Fp;
X{
X int ch;
X
X while ((ch = getc(Fp)) != EOF) {
X if (islower(ch)) putchar(toupper(ch));
X else putchar(ch);
X }
X}
X
Xstatic void
XFirstToUpper(Fp)
X FILE *Fp;
X{
X int ch;
X
X int AtStart = 1;
X
X while ((ch = getc(Fp)) != EOF) {
X if (isalnum(ch)) {
X if (AtStart) {
X if (islower(ch)) putchar(toupper(ch));
X else putchar(ch);
X AtStart = 0;
X } else {
X putchar(ch);
X }
X } else {
X putchar(ch);
X AtStart = 1;
X }
X }
X}
X
Xstatic void
XFirstToUpperAndRestToLower(Fp)
X FILE *Fp;
X{
X int ch;
X
X int AtStart = 1;
X
X while ((ch = getc(Fp)) != EOF) {
X if (isalnum(ch)) {
X if (AtStart) {
X if (islower(ch)) putchar(toupper(ch));
X else putchar(ch);
X AtStart = 0;
X } else {
X if (isupper(ch)) putchar(tolower(ch));
X else putchar(ch);
X }
X } else {
X putchar(ch);
X AtStart = 1;
X }
X }
X}
X
Xstatic void
XAllToLower(Fp)
X FILE *Fp;
X{
X int ch;
X
X while ((ch = getc(Fp)) != EOF) {
X if (isupper(ch)) putchar(tolower(ch));
X else putchar(ch);
X }
X}
@@@End of capitalise.c
echo x - 8bit 1>&2
sed 's/^X//' >8bit <<'@@@End of 8bit'
X#! /bin/sh
Xstty -istrip -parenb cs8
Xstty pass8 # might not work
@@@End of 8bit
echo end of archive
exit 0
--
Liam Quin, lee@sq.com, SoftQuad, Toronto, 416 239-4801; the barefoot programmer
``House,--(though your own), yet in England a place of no security, as ruffians
may enter it & pillage it as they please, and drag the owner away to prison,
whenever the Minister thinks proper.'' -- Pigott's Political Dict., 1795
Technical Details
Message-ID:
<1992May24.035926.3744@sq.sq.com>Path:
arc1-ca.pugleaf.net!wupost!cs.utexas.edu!utgpu!utzoo!sq!leeShow Full Headers
Complete Article Headers
Newsgroups: comp.windows.open-look,alt.sources Path: arc1-ca.pugleaf.net!wupost!cs.utexas.edu!utgpu!utzoo!sq!lee From: lee@sq.sq.com (Liam R. E. Quin) Subject: Extras menu: replacement for Sun's capitalize program Message-ID: <1992May24.035926.3744@sq.sq.com> Organization: SoftQuad Inc., Toronto, Canada Date: Sun, 24 May 92 03:59:26 GMT Lines: 249