Tag Archive for console

C Console Scripting Framework

#include <stdio.h>
//#include <stdlib.h> //for system pause

//USAGE (in DOS):   dir blah | yourProgramName > outFile.txt
// (use /b for JUST file and folder names)
// (use /b /ad for JUST folder names)

void printsln(char *s) {printf("%s
", s);}

void error(char *s){printsln(s); exit(1);}

bool ngets(char *s, int n) {
int i = 0;
char c;
c = getchar();
if (c==EOF) {s[i] = 0; return false;}
while(c!='
'){
if(i>=n) error("input stream overflowed buffer");
s[i++] = (char)c;
c = getchar();
}
s[i] = 0;
return true;
}

int main(int argc, char *argv[])
{
char s[10000]; //note: possible (in theory) security hole
while(ngets(s, 10000)) { //security hole closed.

printf("/new/%s
", s, s);  //TWEAK THIS LINE!!!

}
//system("pause");
return 0;
}

source

Better Unicode Text Wrapping Function

# This recipe refers:
#
#  <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061" >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061</a>

import re
rx=re.compile(u"([u2e80-uffff])", re.UNICODE)

def cjkwrap(text, width, encoding="utf8"):
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
[' ','n', ''][(len(line)-line.rfind('n')-1
+ len(word.split('n',1)[0] ) >= width) or
line[-1:] == '' and 2],
word),
rx.sub(r'1 ', unicode(text,encoding)).split(' ')
).replace('', '').encode(encoding)

source

Wrap Text to Eighty Columns

def wrap_text(txt, col = 80):
import re
re.sub("/(.{1,#{col}})( +|$n?)|(.{1,#{col}})/", "13n", txt)
# warning: snipplr tends to escape the quotes and slashes, causing this
# snippet to fail when pasted in.
return txt

source