Skip to content

Instantly share code, notes, and snippets.

@blippy
blippy / cmdsplit.tcl
Created June 10, 2016 12:36
Split a line as if it were a command
#!/usr/bin/env tclsh
# http://wiki.tcl.tk/21701
proc cmdSplit command {
if {![info complete $command]} {error "non complete command"}
set res ""; # the list of words
set chunk ""
foreach word [split $command " \t"] {
# testing each word until the word being tested makes the
# command up to it complete
@blippy
blippy / simult.cc
Last active June 10, 2016 12:34
Apply function concurrently on a vector of args
#include <functional>
#include <iostream>
#include <string>
#include <future>
#include <thread>
#include <vector>
#include <unistd.h> // for sleep. Not needed generally
@blippy
blippy / run.cc
Created June 9, 2016 11:44
Execute a shell command, reading its output
#include <iostream>
#include <stdexcept>
#include <string>
std::string run(const char* cmd)
{
FILE *in;
char buff[512];
std::string result;
@blippy
blippy / slurp.cc
Last active March 1, 2021 21:06
Slurp a file in C++
#include <fstream>
#include <sstream>
std::string slurp(const char *filename)
{
std::ifstream in;
in.open(filename, std::ifstream::in | std::ifstream::binary);
std::stringstream sstr;
sstr << in.rdbuf();
in.close();
@blippy
blippy / fmtd.cc
Last active August 16, 2018 20:25
Format a double for output
std::string dout(double d)
{
std::ostringstream s;
s.precision(2);
s.width(10);
s << std::fixed;
s << d;
return s.str();
}
@blippy
blippy / naps.cob
Created May 16, 2016 13:48
An implementation of naps in COBOL
000100 identification division.
000200 program-id. naps.
000300 environment division.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EPICS-FILE ASSIGN TO
"/home/mcarter/.mca/work/s3/epics.rep"
ORGANIZATION IS LINE SEQUENTIAL.
select sectors-file assign to
"sectors.txt"
@blippy
blippy / ostream.cc
Last active June 19, 2016 10:12
Example of writing to cout or filename
#include <fstream>
#include <iostream>
void prin_vecvec(vecvec_t & vvs, const char *sep, const char *recsep, const char *filename )
{
std::ofstream ofs;
bool use_file = strlen(filename);
if(use_file) ofs.open(filename, std::ofstream::out);
ostream &os = use_file ? ofs : cout ;
@blippy
blippy / istream.cc
Last active March 13, 2018 16:08
Reading from cin or file
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <string>
void reading()
{
std::string filename = ... ;
std::ifstream ifs;
if(filename.size()>0) ifs.open(filename);
@blippy
blippy / xterm.desktop
Created March 31, 2016 10:34
Add xterm to menus
[Desktop Entry]
Version=0.0.0
Type=Application
Name=Xterm Terminal
GenericName=Xterm Terminal
Comment=A lightweight terminal
Icon=/usr/share/icons/nuoveXT2/22x22/apps/terminal.png
Exec=xterm %F
#Terminal=false
MimeType=text/html;
@blippy
blippy / mtime.cc
Last active March 15, 2016 14:57
Formatted creation time of a file
#include <sys/stat.h>
...
struct stat st;
stat("s2/yfetch.csv", &st);
time_t t = st.st_mtime;
struct tm atm;
localtime_r(&t, &atm);
char dtstamp[80];