Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* PDCurses */
  2.  
  3. #include "pdcsdl.h"
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. /*man-start**************************************************************
  9.  
  10. clipboard
  11. ---------
  12.  
  13. ### Synopsis
  14.  
  15.     int PDC_getclipboard(char **contents, long *length);
  16.     int PDC_setclipboard(const char *contents, long length);
  17.     int PDC_freeclipboard(char *contents);
  18.     int PDC_clearclipboard(void);
  19.  
  20. ### Description
  21.  
  22.    PDC_getclipboard() gets the textual contents of the system's
  23.    clipboard. This function returns the contents of the clipboard in the
  24.    contents argument. It is the responsibility of the caller to free the
  25.    memory returned, via PDC_freeclipboard(). The length of the clipboard
  26.    contents is returned in the length argument.
  27.  
  28.    PDC_setclipboard copies the supplied text into the system's
  29.    clipboard, emptying the clipboard prior to the copy.
  30.  
  31.    PDC_clearclipboard() clears the internal clipboard.
  32.  
  33. ### Return Values
  34.  
  35.    indicator of success/failure of call.
  36.    PDC_CLIP_SUCCESS        the call was successful
  37.    PDC_CLIP_MEMORY_ERROR   unable to allocate sufficient memory for
  38.                            the clipboard contents
  39.    PDC_CLIP_EMPTY          the clipboard contains no text
  40.    PDC_CLIP_ACCESS_ERROR   no clipboard support
  41.  
  42. ### Portability
  43.                              X/Open  ncurses  NetBSD
  44.     PDC_getclipboard            -       -       -
  45.     PDC_setclipboard            -       -       -
  46.     PDC_freeclipboard           -       -       -
  47.     PDC_clearclipboard          -       -       -
  48.  
  49. **man-end****************************************************************/
  50.  
  51. /* global clipboard contents, should be NULL if none set */
  52.  
  53. static char *pdc_SDL_clipboard = NULL;
  54.  
  55. int PDC_getclipboard(char **contents, long *length)
  56. {
  57.     int len;
  58.  
  59.     PDC_LOG(("PDC_getclipboard() - called\n"));
  60.  
  61.     if (!pdc_SDL_clipboard)
  62.         return PDC_CLIP_EMPTY;
  63.  
  64.     len = strlen(pdc_SDL_clipboard);
  65.     if ((*contents = malloc(len + 1)) == NULL)
  66.         return PDC_CLIP_MEMORY_ERROR;
  67.  
  68.     strcpy(*contents, pdc_SDL_clipboard);
  69.     *length = len;
  70.  
  71.     return PDC_CLIP_SUCCESS;
  72. }
  73.  
  74. int PDC_setclipboard(const char *contents, long length)
  75. {
  76.     PDC_LOG(("PDC_setclipboard() - called\n"));
  77.  
  78.     if (pdc_SDL_clipboard)
  79.     {
  80.         free(pdc_SDL_clipboard);
  81.         pdc_SDL_clipboard = NULL;
  82.     }
  83.  
  84.     if (contents)
  85.     {
  86.         if ((pdc_SDL_clipboard = malloc(length + 1)) == NULL)
  87.             return PDC_CLIP_MEMORY_ERROR;
  88.  
  89.         strcpy(pdc_SDL_clipboard, contents);
  90.     }
  91.  
  92.     return PDC_CLIP_SUCCESS;
  93. }
  94.  
  95. int PDC_freeclipboard(char *contents)
  96. {
  97.     PDC_LOG(("PDC_freeclipboard() - called\n"));
  98.  
  99.     /* should we also free empty the system clipboard? probably not */
  100.  
  101.     if (contents)
  102.     {
  103.         /* NOTE: We free the memory, but we can not set caller's pointer
  104.            to NULL, so if caller calls again then will try to access
  105.            free'd memory.  We 1st overwrite memory with a string so if
  106.            caller tries to use free memory they won't get what they
  107.            expect & hopefully notice. */
  108.  
  109.         /* memset(contents, 0xFD, strlen(contents)); */
  110.  
  111.         if (strlen(contents) >= strlen("PDCURSES"))
  112.             strcpy(contents, "PDCURSES");
  113.  
  114.         free(contents);
  115.     }
  116.  
  117.     return PDC_CLIP_SUCCESS;
  118. }
  119.  
  120. int PDC_clearclipboard(void)
  121. {
  122.     PDC_LOG(("PDC_clearclipboard() - called\n"));
  123.  
  124.     if (pdc_SDL_clipboard)
  125.     {
  126.         free(pdc_SDL_clipboard);
  127.         pdc_SDL_clipboard = NULL;
  128.     }
  129.  
  130.     return PDC_CLIP_SUCCESS;
  131. }
  132.