dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / UAS / DtSR / TextRun.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: TextRun.cc /main/3 1996/06/11 17:41:52 cde-hal $
24 /*      Copyright (c) 1995 FUJITSU LIMITED      */
25 /*      All Rights Reserved                     */
26
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <sstream>
32 using namespace std;
33
34 main(int argc, char** argv)
35 {
36     if (argc < 3) {
37         fprintf(stderr, "Usage: %s offset length\n", argv[0]);
38         return 1;
39     }
40
41     int offset = atoi(argv[1]);
42     int length = atoi(argv[2]);
43
44     if (offset < 0) {
45         fprintf(stderr, "(ERROR) offset must be non-negative\n");
46         return 1;
47     }
48     if (length <= 0) {
49         fprintf(stderr, "(ERROR) length must be positive\n");
50         return 1;
51     }
52
53     ostringstream text;
54
55     char ch;
56     while (cin.get(ch)) text << ch;
57
58     char* buf = (char *)text.str().c_str();
59     *(buf + text.str().size()) = '\0';
60     char* p = buf;
61
62     if (buf == NULL || *buf == '\0') {
63         fprintf(stderr, "(ERROR) empty input stream\n");
64         return 1;
65     }
66
67     int n;
68     for (; offset > 0; p += n, offset -= n) {
69         n = mblen(p, MB_CUR_MAX);
70         assert( n > 0 );
71     }
72     assert( offset == 0 );
73
74     fprintf(stdout, "\"%.*s\"\n", length, p);
75 }