Changeset 0b772f5 in mainline


Ignore:
Timestamp:
2009-12-04T23:16:10Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7e0cb78
Parents:
0902edfe
Message:

Save clipboard data in a file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/clipboard.c

    r0902edfe r0b772f5  
    3131 */
    3232/** @file
     33 * @brief System clipboard API.
     34 *
     35 * The clipboard data is stored in a file and it is shared by the entire
     36 * system.
     37 *
     38 * @note Synchronization is missing. File locks would be ideal for the job.
    3339 */
    3440
    3541#include <stdlib.h>
    3642#include <string.h>
     43#include <sys/types.h>
     44#include <sys/stat.h>
     45#include <fcntl.h>
     46#include <macros.h>
    3747#include <errno.h>
     48#include <clipboard.h>
    3849
    39 static char *clipboard_data = NULL;
     50/** File used to store clipboard data */
     51#define CLIPBOARD_FILE "/data/clip"
     52
     53#define CHUNK_SIZE      4096
    4054
    4155/** Copy string to clipboard.
     
    4660 * @param str   String to put to clipboard or NULL.
    4761 * @return      Zero on success or negative error code.
    48  *
    4962 */
    5063int clipboard_put_str(const char *str)
    5164{
    52         if (clipboard_data != NULL)
    53                 free(clipboard_data);
     65        int fd;
     66        const char *sp;
     67        ssize_t to_write, written, left;
    5468
    55         clipboard_data = str_dup(str);
     69        fd = open(CLIPBOARD_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0666);
     70        if (fd < 0)
     71                return EIO;
     72
     73        left = str_size(str);
     74        sp = str;
     75
     76        while (left > 0) {
     77                to_write = min(left, CHUNK_SIZE);
     78                written = write(fd, sp, to_write);
     79
     80                if (written < 0) {
     81                        close(fd);
     82                        unlink(CLIPBOARD_FILE);
     83                        return EIO;
     84                }
     85
     86                sp += written;
     87                left -= written;
     88        }
     89
     90        if (close(fd) != EOK) {
     91                unlink(CLIPBOARD_FILE);
     92                return EIO;
     93        }
     94
    5695        return EOK;
    5796}
    5897
     98/** Get a copy of clipboard contents.
     99 *
     100 * Returns a new string that can be deallocated with free().
     101 *
     102 * @param str   Here pointer to the newly allocated string is stored.
     103 * @return      Zero on success or negative error code.
     104 */
    59105int clipboard_get_str(char **str)
    60106{
    61         *str = (clipboard_data != NULL) ? str_dup(clipboard_data) : NULL;
     107        int fd;
     108        char *sbuf, *sp;
     109        ssize_t to_read, n_read, left;
     110        struct stat cbs;
     111
     112        if (stat(CLIPBOARD_FILE, &cbs) != EOK)
     113                return EIO;
     114
     115        sbuf = malloc(cbs.size + 1);
     116        if (sbuf == NULL)
     117                return ENOMEM;
     118
     119        fd = open(CLIPBOARD_FILE, O_RDONLY);
     120        if (fd < 0) {
     121                free(sbuf);
     122                return EIO;
     123        }
     124
     125        sp = sbuf;
     126        left = cbs.size;
     127
     128        while (left > 0) {
     129                to_read = min(left, CHUNK_SIZE);
     130                n_read = read(fd, sp, to_read);
     131
     132                if (n_read < 0) {
     133                        close(fd);
     134                        free(sbuf);
     135                        return EIO;
     136                }
     137
     138                sp += n_read;
     139                left -= n_read;
     140        }
     141
     142        close(fd);
     143
     144        *sp = '\0';
     145        *str = sbuf;
     146
    62147        return EOK;
    63148}
Note: See TracChangeset for help on using the changeset viewer.