| 1 | /* |
|---|
| 2 | * neercs console-based window manager |
|---|
| 3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
|---|
| 4 | * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 5 | * 2008-2010 Pascal Terjan <pterjan@linuxfr.org> |
|---|
| 6 | * All Rights Reserved |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #include "config.h" |
|---|
| 16 | #include <caca.h> |
|---|
| 17 | #include <string.h> |
|---|
| 18 | #include <sys/stat.h> |
|---|
| 19 | #include "neercs.h" |
|---|
| 20 | |
|---|
| 21 | void dump_to_file(struct screen_list *screen_list) |
|---|
| 22 | { |
|---|
| 23 | char filename[14] = "hardcopy.0"; |
|---|
| 24 | int n = 0; |
|---|
| 25 | struct stat bufstat; |
|---|
| 26 | void * export; |
|---|
| 27 | size_t len, wrote; |
|---|
| 28 | FILE * out; |
|---|
| 29 | |
|---|
| 30 | /* FIXME maybe use glob and get next one directly */ |
|---|
| 31 | while (n<9999 && !stat(filename, &bufstat)) |
|---|
| 32 | { |
|---|
| 33 | n++; |
|---|
| 34 | sprintf(&filename[9], "%d", n); |
|---|
| 35 | } |
|---|
| 36 | if (n>=9999) |
|---|
| 37 | { |
|---|
| 38 | debug("Too many hardcopy files in current directory\n"); |
|---|
| 39 | return; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | export = caca_export_canvas_to_memory(screen_list->cv, "ansi", &len); |
|---|
| 43 | if (!export) |
|---|
| 44 | { |
|---|
| 45 | debug("Failed to export to ansi\n"); |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | out = fopen(filename, "w"); |
|---|
| 50 | if (!out) |
|---|
| 51 | { |
|---|
| 52 | debug("Failed to open output file %s: %s\n", filename, strerror(errno)); |
|---|
| 53 | return; |
|---|
| 54 | } |
|---|
| 55 | wrote = fwrite(export, len, 1, out); |
|---|
| 56 | if (!wrote) |
|---|
| 57 | { |
|---|
| 58 | debug("Failed to write to output file: %s\n", strerror(errno)); |
|---|
| 59 | return; |
|---|
| 60 | } |
|---|
| 61 | free(export); |
|---|
| 62 | } |
|---|