Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / UAS / DtSR / TextRun.C
1 // $XConsortium: TextRun.cc /main/3 1996/06/11 17:41:52 cde-hal $
2 /*      Copyright (c) 1995 FUJITSU LIMITED      */
3 /*      All Rights Reserved                     */
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include <strstream.h>
10
11 main(int argc, char** argv)
12 {
13     if (argc < 3) {
14         fprintf(stderr, "Usage: %s offset length\n", argv[0]);
15         return 1;
16     }
17
18     int offset = atoi(argv[1]);
19     int length = atoi(argv[2]);
20
21     if (offset < 0) {
22         fprintf(stderr, "(ERROR) offset must be non-negative\n");
23         return 1;
24     }
25     if (length <= 0) {
26         fprintf(stderr, "(ERROR) length must be positive\n");
27         return 1;
28     }
29
30     ostrstream text;
31
32     char ch;
33     while (cin.get(ch)) text << ch;
34
35     char* buf = text.str();
36     *(buf + text.pcount()) = '\0';
37     char* p = buf;
38
39     if (buf == NULL || *buf == '\0') {
40         fprintf(stderr, "(ERROR) empty input stream\n");
41         return 1;
42     }
43
44     int n;
45     for (; offset > 0; p += n, offset -= n) {
46         n = mblen(p, MB_CUR_MAX);
47         assert( n > 0 );
48     }
49     assert( offset == 0 );
50
51     fprintf(stdout, "\"%.*s\"\n", length, p);
52 }