Friday, July 18, 2008

clip.cpp - A win32 clipboard utility


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <windows.h>

size_t textToClipboard(const char *text) {
     size_t length = std::strlen(text);
     size_t charsCopied = 0;
     if (OpenClipboard(NULL) != 0) {
          if (EmptyClipboard() != 0) {
               HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, length+1);
               if (hMem != NULL) {
                    void *p = GlobalLock(hMem);
                    if (p != NULL) {
                         std::memcpy(p, text, length+1);
                         GlobalUnlock(hMem);

                         if (SetClipboardData(CF_TEXT, hMem) != 0) {
                              charsCopied = length;
                         }
                    }
               }
          }
          CloseClipboard();
     }
     return charsCopied;
}

int main() {
     std::vector<char> input;
     bool done = false;
     while (done == false) {
          int ch = std::fgetc(stdin);
          if (ch != EOF) {
               input.push_back((char)ch);
          }
          else {
               done = true;
          }
     }
     input.push_back(0);

     size_t charsCopied = textToClipboard(&input[0]);
     std::printf("%u char(s) read from stdin.\n", input.size()-1);
     std::printf("%u char(s) copied to the clipboard\n", charsCopied);

     return 0;
}

Tuesday, July 8, 2008

What is a Distant Pocket?

Salutem plurimam.

Thinking, speaking and writing about math and science is a joy of mine. I hope to explore the ‘hows and whys’ of some interesting computational issues.

I’ve written a few small utilities which I’ll post (and discuss) the source code to. Also, I expect the chronological dimension will serve as an excellent viewport for in-progress projects.

Ground rules:

As a game developer, I’m under numerous NDAs. Often, I will only be able to address an issue tangentially.

Enguerrand is a pseudonym, with the intent of protecting my (equally anonymous) employer. I appreciate your respect in this matter.

Comments via private email or public posting are welcome. I will promptly delete spam and content-free remarks.

Finally, sincere thanks to Blogger for hosting this!