Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / clipboard.cpp
blob6b54a5265f1eae7e8671b3eeeb1a04ee38cb8dda
1 /*
2 * Copyright 2005, Haiku Inc.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Jonas Sundström, jonas@kirilla.com
7 * Axel Dörfler, axeld@pinc-software.de.
8 */
11 #include <Application.h>
12 #include <Clipboard.h>
13 #include <File.h>
14 #include <FindDirectory.h>
15 #include <Path.h>
16 #include <String.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <getopt.h>
24 extern const char *__progname;
25 static const char *kProgramName = __progname;
27 static int sExitValue = EXIT_SUCCESS;
30 class ClipboardApp : public BApplication {
31 public:
32 ClipboardApp(void);
33 virtual ~ClipboardApp(void);
35 virtual void ReadyToRun(void);
36 virtual void ArgvReceived(int32 argc, char **argv);
38 private:
39 void Usage(void);
40 status_t Load(const char *path);
41 status_t Save(const char *path);
42 status_t Copy(const char *string);
43 status_t Clear(void);
44 status_t Print(bool debug);
46 bool fGotArguments;
50 ClipboardApp::ClipboardApp(void)
51 : BApplication("application/x-vnd.haiku-clipboard"),
52 fGotArguments(false)
57 ClipboardApp::~ClipboardApp(void)
62 void
63 ClipboardApp::ArgvReceived(int32 argc, char **argv)
65 status_t status = B_OK;
67 static struct option const kLongOptions[] = {
68 {"load", required_argument, 0, 'l'},
69 {"save", required_argument, 0, 's'},
70 {"copy", required_argument, 0, 'c'},
71 {"clear", no_argument, 0, 'r'},
72 {"stdin", no_argument, 0, 'i'},
73 {"print", no_argument, 0, 'p'},
74 {"debug", no_argument, 0, 'd'},
75 {"help", no_argument, 0, 'h'},
76 {NULL}
79 int c;
80 while ((c = getopt_long(argc, argv, "l:s:o:c:ripdh", kLongOptions, NULL))
81 != -1) {
82 switch (c) {
83 case 'l':
84 status = Load(optarg);
85 break;
86 case 's':
87 case 'o':
88 status = Save(optarg);
89 break;
90 case 'c':
91 status = Copy(optarg);
92 break;
93 case 'r':
94 status = Clear();
95 break;
96 case 'i':
97 status = Copy(NULL);
98 break;
99 case 'p':
100 status = Print(false);
101 break;
102 case 'd':
103 status = Print(true);
104 break;
105 case 0:
106 break;
107 case 'h':
108 // help text is printed in ReadyToRun()
109 return;
110 default:
111 status = B_ERROR;
112 break;
116 if (status == B_OK)
117 fGotArguments = true;
118 else
119 sExitValue = EXIT_FAILURE;
123 void
124 ClipboardApp::ReadyToRun(void)
126 if (fGotArguments == false)
127 Usage();
129 PostMessage(B_QUIT_REQUESTED);
133 void
134 ClipboardApp::Usage(void)
136 printf("usage: %s [-olcirpd]\n"
137 " -s, --save=file\tSave clipboard to file (flattened BMessage)\n"
138 " -o\t\t\tthe same\n" // ToDo: (\"-\" for standard output/input)\n"
139 " -l, --load=file\tLoad clipboard from file (flattened BMessage)\n"
140 " -c, --copy=string\tPut the given string on the clipboard\n"
141 " -i, --stdin\t\tLoad clipboard with string from standard input\n\n"
143 " -r, --clear\t\tRemove all contents from clipboard\n\n"
145 " -p, --print\t\tPrint clipboard to standard output\n"
146 " -d, --debug\t\tPrint clipboard message to stdout\n\n"
148 " -h, --help\t\tDisplay this help and exit\n",
149 kProgramName);
153 status_t
154 ClipboardApp::Load(const char *path)
156 status_t status = B_OK;
157 BFile file;
159 status = file.SetTo(path, B_READ_ONLY);
160 if (status != B_OK) {
161 fprintf(stderr, "%s: Could not open file: %s\n", kProgramName,
162 strerror(status));
163 return status;
166 // get BMessage from file
167 BMessage clipFromFile;
168 status = clipFromFile.Unflatten(&file);
169 if (status != B_OK) {
170 fprintf(stderr, "%s: Could not read clip: %s\n", kProgramName,
171 strerror(status));
172 return status;
175 // load clip into clipboard
176 if (!be_clipboard->Lock()) {
177 fprintf(stderr, "%s: could not lock clipboard.\n", kProgramName);
178 return B_ERROR;
181 be_clipboard->Clear();
183 BMessage *clip = be_clipboard->Data();
184 *clip = clipFromFile;
186 status = be_clipboard->Commit();
187 if (status != B_OK) {
188 fprintf(stderr, "%s: could not commit data to clipboard.\n",
189 kProgramName);
190 be_clipboard->Unlock();
191 return status;
194 be_clipboard->Unlock();
195 return B_OK;
199 status_t
200 ClipboardApp::Save(const char *path)
202 status_t status = B_OK;
203 BFile file;
205 status = file.SetTo(path, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
206 if (status != B_OK) {
207 fprintf(stderr, "%s: Could not create file: %s\n", kProgramName,
208 strerror(status));
209 return status;
212 // get clip
213 if (!be_clipboard->Lock()) {
214 fprintf(stderr, "%s: could not lock clipboard.\n", kProgramName);
215 return B_ERROR;
218 BMessage *clip = be_clipboard->Data();
220 be_clipboard->Unlock();
222 // flatten clip to file
223 status = clip->Flatten(&file);
224 if (status != B_OK) {
225 fprintf(stderr, "%s: Could not write data: %s\n", kProgramName,
226 strerror(status));
227 return status;
230 return B_OK;
234 status_t
235 ClipboardApp::Copy(const char *string)
237 status_t status = B_OK;
238 BString inputString;
240 if (string == NULL) {
241 // read from standard input
242 int c;
243 while ((c = fgetc(stdin)) != EOF) {
244 inputString += (char)c;
247 string = inputString.String();
250 // get clipboard BMessage
252 if (be_clipboard->Lock()) {
253 be_clipboard->Clear();
255 BMessage *clip = be_clipboard->Data();
257 // add data to clipboard
258 clip->AddData("text/plain", B_MIME_TYPE, string, strlen(string));
260 status = be_clipboard->Commit();
261 if (status != B_OK) {
262 fprintf(stderr, "%s: could not commit data to clipboard.\n",
263 kProgramName);
264 be_clipboard->Unlock();
265 return status;
268 be_clipboard->Unlock();
269 } else {
270 fprintf(stderr, "%s: could not lock clipboard.\n", kProgramName);
271 return B_ERROR;
274 return B_OK;
278 status_t
279 ClipboardApp::Clear(void)
281 status_t status = B_OK;
283 if (!be_clipboard->Lock()) {
284 fprintf(stderr, "%s: could not lock clipboard.\n", kProgramName);
285 return B_ERROR;
288 be_clipboard->Clear();
290 status = be_clipboard->Commit();
291 if (status != B_OK) {
292 fprintf(stderr, "%s: could not clear clipboard.\n", kProgramName);
293 be_clipboard->Unlock();
294 return status;
297 be_clipboard->Unlock();
298 return B_OK;
302 status_t
303 ClipboardApp::Print(bool debug)
305 // get clip & print contents
306 // (or the entire clip, in case of --debug)
308 if (!be_clipboard->Lock()) {
309 fprintf(stderr, "%s: could not lock clipboard.\n", kProgramName);
310 return B_ERROR;
313 BMessage *clip = be_clipboard->Data();
315 if (debug)
316 clip->PrintToStream();
317 else {
318 const char * textBuffer;
319 ssize_t textLength;
320 clip->FindData("text/plain", B_MIME_TYPE, (const void **)&textBuffer,
321 &textLength);
323 if (textBuffer != NULL && textLength > 0) {
324 BString textString(textBuffer, textLength);
325 printf("%s\n", textString.String());
329 be_clipboard->Unlock();
330 return B_OK;
334 // #pragma mark -
338 main()
340 ClipboardApp clip_app;
341 clip_app.Run();
343 return sExitValue;