NO

Author Topic: Anybody got code to cnvt a line of text to printf-able form?  (Read 7368 times)

Rainbow Sally

  • Guest
Anybody got code to cnvt a line of text to printf-able form?
« on: January 05, 2005, 07:41:05 AM »
Currently using MAKE to write strings but I'd like to read a line of text from a file and add prefix and suffix quotes to it.  

Output must be ANSI.  (Not unicode, so no resources utilties will work.)

This means we need to be able to add backslashes to quote chars and other special characters on the fly.  

[I'll post the code and delete this msg in a day or two if I end up having to write it from scratch... Whew!  I'm learning so much so quickly that my utilities are becomming obsolete almost as fast as I write 'em!]

hellork

  • Guest
Re: Anybody got code to cnvt a line of text to printf-able form?
« Reply #1 on: November 21, 2010, 06:04:42 AM »
I'm all for using the most convenient tool for the job. PHP has an addcslashes command. This script takes code piped to it and turns it into a "C one-liner." Yeah... there is such a thing, in Linux, at least, or an equivalent shell like win bash http://win-bash.sourceforge.net/ will probably run it on Windows, heh heh (george bush laugh).

Code: [Select]
#!/usr/bin/php
gcc -xc -<<<$'<?php
$file=@fopen("php://stdin","r") or die ("input error\n");
while ($line=fgets($file,1024)){
printf("%s",addcslashes($line,"\\\'\0..\37!@\177..\377"));
}
fclose($file);
?>
'&&./a.out

dildargee1

  • Guest
Re: Anybody got code to cnvt a line of text to printf-able form?
« Reply #2 on: January 20, 2015, 06:17:12 AM »
Also whitespace at the end of a line makes semicolons not get repaired on that line. On the other hand, this is good, because you can prevent an extra semicolon from being automatically removed by putting a space after it. There are a couple places in C code where an extra semicolon is needed, such as after anonymous structs.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Anybody got code to cnvt a line of text to printf-able form?
« Reply #3 on: January 20, 2015, 07:26:28 AM »
Also whitespace at the end of a line makes semicolons not get repaired on that line. On the other hand, this is good, because you can prevent an extra semicolon from being automatically removed by putting a space after it. There are a couple places in C code where an extra semicolon is needed, such as after anonymous structs.