![]() |
02-11-2007, 12:17 PM
|
#1 |
|
DWK
Join Date: Apr 2005
Location: Canada
Posts: 4,087
|
codeform version 1.2.0
I
have finished re-writing codeform. The previous version was called
v1.10, but this version is so much better that I decided to increment
the minor version number and call it version 1.2.0.
![]() Codeform is a code formatter. Other terms for this include "code colourer", "code painter", and "syntax highlighter". It's written in ANSI standard C (sorry, no GUI). Version 1.2.0 is released under the GNU GPL. It has much better rules files than v1.10, including variables and file sourcing; it does binary searching, making it less than a fifth the speed (I know I said a quarter, but for 99% of the tests, it's a fifth); it supports unlimited lines . . . I'll quote the history file. Code:
1.2.0 [date]:
Complete re-write of codeform. Features include:
- Much faster than codeform 1.10 (about a quarter of the speed)
- Binary search for rules making it much faster for many rules
- Sequential keywords of the same colour have one tag
- Unlimited line length for rules and input files
- Support for multiple input, output, and rules files
- Inline rules which can be specified on the command line
- Greatly improved data files which support variables and part sharing
- Automatic termination of strings that don't terminate
- Support for comments inside comments (ie, /**/ inside #)
- Much more generic and easier to extend (few hard-coded assumptions)
- Comments in the code so it's pretty much readable
I have tested codeform under Windows and Linux. It works on both. One note: the rules files must have Linux line endings for them to work on both platforms. Download it and tell me what you think! Codeform's main page is here: http://dwks.theprogrammingsite.com/myprogs/codeform.htm To download codeform, go here: http://dwks.theprogrammingsite.com/m...n/codeform.zip If you want to see codeform in action, jump to here: http://dwks.theprogrammingsite.com/m...m/formself.htm I often use codeform in conjunction with outclip (which prints the contents of the clipboard) and clipboard (which puts its input into the clipboard), which I will post as soon as I get rid of their bugs. If anyone wants to implement these themselves, go ahead . . . This formats the clipboard into BBCode. Code:
C:\>outclip | codeform rules\c_1_vbb | clipboard
__________________
dwk Seek and ye shall find. quaere et invenies. (Latin by dwks.) "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot "I have not failed. I've just found 10,000 ways that won't work." -- Thomas Alva Edison "The only real mistake is the one from which we learn nothing." -- John Powell My website: http://dwks.theprogrammingsite.com/ Other programming boards: codeform version 1.2.0 online is here! |
dwks is online now
|
|
02-11-2007, 02:44 PM
|
#2 |
|
Registered Pusher
Join Date: Dec 2005
Location: Ireland
Posts: 1,527
|
This is a beautiful thing
Oh, it paints the braces colours too ... different colouring for different types ... */& are now red ... I like ![]()
__________________
"I will show you fear in a handful of dust." - TS Eliot Foreman: You stash your drugs in a lupus textbook? House: It's never lupus |
twomers is offline
|
|
02-11-2007, 04:49 PM
|
#3 |
|
DWK
Join Date: Apr 2005
Location: Canada
Posts: 4,087
|
Thank you.
I think the default style might have too much blue, but hey, if you think so, use a different style.Since twomers would like it, I've uploaded my outclip and clipboard programs. They're Windows-specific, obviously, and have virtually no error checking (if I put it in it doesn't work . . .). I'm not a great Windows programmer, so feel free to improve on them. http://dwks.theprogrammingsite.com/m.../down/clip.zip The archive includes outclip.c, outclip.exe, clipboard.c, and clipboard.exe. [edit] It's 4,088 bytes. [/edit][edit=2] To clarify, "a fifth the speed" means five times faster, not five times slower. [/edit]
__________________
dwk Seek and ye shall find. quaere et invenies. (Latin by dwks.) "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot "I have not failed. I've just found 10,000 ways that won't work." -- Thomas Alva Edison "The only real mistake is the one from which we learn nothing." -- John Powell My website: http://dwks.theprogrammingsite.com/ Other programming boards: codeform version 1.2.0 online is here! Last edited by dwks : 02-11-2007 at 05:00 PM. |
dwks is online now
|
|
02-11-2007, 05:27 PM
|
#4 |
|
DWK
Join Date: Apr 2005
Location: Canada
Posts: 4,087
|
I've
re-uploaded codeform. There was a bug in the rules file c_1 (which
practically everything uses) that manifested itself when you used the
_vbb rules file (which everyone here would probably use). Download it
again, or make c_1 look like this (the blue lines were added):
Code:
[...]
29: [:$(midwordb):$(midworda)
30: ]:*:*
31: (:*:*
32: ):*:*
33: {:*:*
34: }:*:*
35: lt=<
36: gt=>
37: $(lt):*:*
38: $(gt):*:*
__________________
dwk Seek and ye shall find. quaere et invenies. (Latin by dwks.) "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot "I have not failed. I've just found 10,000 ways that won't work." -- Thomas Alva Edison "The only real mistake is the one from which we learn nothing." -- John Powell My website: http://dwks.theprogrammingsite.com/ Other programming boards: codeform version 1.2.0 online is here! |
dwks is online now
|
|
02-12-2007, 12:40 PM
|
#5 |
|
Registered Pusher
Join Date: Dec 2005
Location: Ireland
Posts: 1,527
|
>> To clarify, "a fifth the speed" means five times faster, not five times slower.
Not that you would notice either way or anything ![]() Or if like me you want a fast and easy way aboot (sorry) it: Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <windows.h>
using namespace std;
bool copy_to_clipboard( LPCTSTR pszText )
{
size_t nBytes;
unsigned int format;
HGLOBAL hGlobal;
LPTSTR pClipboardData;
#ifdef UNICODE
format = CF_UNICODETEXT;
#else
format = CF_TEXT;
#endif
if( !OpenClipboard( NULL ) ) return false;
EmptyClipboard();
nBytes = ( _tcslen( pszText ) + 1 ) * sizeof( TCHAR );
hGlobal = GlobalAlloc( GMEM_MOVEABLE, nBytes );
if( !hGlobal )
{
std::cerr<< "\nERROR: hGlobal! Nothing copied to clipboard";
CloseClipboard();
return false;
}
pClipboardData = (char*)GlobalLock( hGlobal );
if( !pClipboardData )
{
std::cerr<< "\nERROR: pClopboardData! Nothing copied to clipboard";
CloseClipboard();
return false;
}
memcpy( pClipboardData, pszText, nBytes );
GlobalUnlock( hGlobal );
SetClipboardData( format, hGlobal );
CloseClipboard();
return true;
}
std::string get_clipboard( void )
{
string data;
if(OpenClipboard(NULL))
{
HANDLE GlobalHWnd = GetClipboardData( CF_TEXT );
if ( GlobalHWnd != 0 )
{
char* text = (char *)GlobalLock(GlobalHWnd);
data = text;
GlobalUnlock(GlobalHWnd);
}
CloseClipboard();
}
return data;
}
int main( void )
{
const std::string tofile = "thing1.txt";
const std::string fromfile = "thing1.cpp";
// Save clipboard to file
std::ofstream out( fromfile.c_str() );
if ( !out )
return 0;
out<< get_clipboard();
out.close();
// Use codeform to paint the code nicely, and save it to file
system( ("codeform -o " + tofile + " -f rules\\cpp_1_vbb " + fromfile).c_str() );
// Now get the data from aforementioned file
std::ifstream in( tofile.c_str() );
if ( !in )
return 0;
// Copy to clipboard.
std::string File, Line;
while ( std::getline( in, Line ) )
File += Line + "\n";
copy_to_clipboard( File.c_str() );
in.close();
return 0;
}
EDIT: Plus, you get the fun of using system calls!
__________________
"I will show you fear in a handful of dust." - TS Eliot Foreman: You stash your drugs in a lupus textbook? House: It's never lupus Last edited by twomers : 02-12-2007 at 12:43 PM. |
twomers is offline
|
|
02-12-2007, 01:31 PM
|
#6 |
|
Drink your milk, kids!
Join Date: May 2004
Location: Out of scope
Posts: 2,938
|
For future reference, "a fifth of the speed" would, infact, be five times slower. It seems you've identified that, though.
![]() I think you're looking for "a fifth of the (processing) time". ![]()
__________________
• Intel Core 2 Duo E6600 @ 3.4Ghz OC •
• ASUS P5B Deluxe v 0711 • 2GB Corsair XMS2 DDR2 800 @ 945Mhz 5-5-5-12 • • nVidia GeForce 7600GT • Creative SoundBlaster Audigy2 ZS • • 2x 250GB Western Digital Caviar • • AeroCool ExtremEngine 3T • OCZ GameXStream 600W • |
SlyMaelstrom is offline
|
|
02-12-2007, 05:04 PM
|
#7 | |
|
DWK
Join Date: Apr 2005
Location: Canada
Posts: 4,087
|
Quote:
I think "half the speed" and "twice the speed" (when used in a positive context, like a features list) often both mean "two times faster", though they are opposites. >not that you would notice Actually, it means <500 ms instead of >2100 ms to format itself on one of my computers. If you format some Allegro source code (file.c, the biggest I could find), the old version takes >7000 ms. So if you're formatting lots of code or very frequently (a web server for example) then speed does matter. [edit] Note that this is a slow computer. For my 1.8GHz computer, it probably wouldn't matter. [/edit]
__________________
dwk Seek and ye shall find. quaere et invenies. (Latin by dwks.) "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot "I have not failed. I've just found 10,000 ways that won't work." -- Thomas Alva Edison "The only real mistake is the one from which we learn nothing." -- John Powell My website: http://dwks.theprogrammingsite.com/ Other programming boards: codeform version 1.2.0 online is here! |
|
dwks is online now
|
|
02-12-2007, 06:39 PM
|
#8 | ||
|
Drink your milk, kids!
Join Date: May 2004
Location: Out of scope
Posts: 2,938
|
Quote:
![]() Quote:
![]() Anyway, nice application. My monitor is a little dark so some of the colors on your default color page there is hard to see, but I couldn't tell. Do you match parenthesis and braces for viewability? I always though that would be nice to have something like: Code:
printf("Example: %d",sin(atoi((const char*)foo)));
Nice work from what I can see, though. Me likes.
__________________
• Intel Core 2 Duo E6600 @ 3.4Ghz OC •
• ASUS P5B Deluxe v 0711 • 2GB Corsair XMS2 DDR2 800 @ 945Mhz 5-5-5-12 • • nVidia GeForce 7600GT • Creative SoundBlaster Audigy2 ZS • • 2x 250GB Western Digital Caviar • • AeroCool ExtremEngine 3T • OCZ GameXStream 600W • |
||
SlyMaelstrom is offline
|
|
02-13-2007, 03:33 PM
|
#9 | |
|
DWK
Join Date: Apr 2005
Location: Canada
Posts: 4,087
|
Quote:
Code:
printf("Example: %d",sin(atoi((const char*)foo)));
Code:
C:\>outclip | codeform_parenthesis -f rules\_vbb -f parenthesis -f rules\c_1 | clipboard Code:
=parenthesis parenthesisbb=$(formbb) parenthesisba=$(formba) parenthesisa=$(forma) $(parenthesisbb)red$(parenthesisba):$(parenthesisa):\ $(parenthesisbb)green$(parenthesisba):$(parenthesisa):\ $(parenthesisbb)blue$(parenthesisba):$(parenthesisa) Here's a diff of codeform.c and codeform_parenthesis.c: Code:
57a58
> TYPE_PARENTHESIS,
246a248
> int type_parenthesis(struct onerule_t *rule, struct typefunc_t *tf);
272a275
> {"parenthesis", (size_t)-1, 1, 9999, type_parenthesis},
1969a1973,1999
> int type_parenthesis(struct onerule_t *rule, struct typefunc_t *tf) {
> static size_t level = 0;
> char c;
>
> if(!rule->data.number) return 0;
> if(tf->func.number) return 0;
>
> if(**tf->p != '(' && **tf->p != ')') return 0;
> c = **tf->p;
>
> if(c == ')') {
> if(--level == (size_t)-1) level = rule->data.number/2 - 1;
> }
>
> append_rulelist(0, 0, tf->list, tf->out);
>
> fputs(rule->data.data[level * 2], tf->out);
> print_chars(tf->p, 1, tf->out);
> fputs(rule->data.data[level * 2 + 1], tf->out);
>
> if(c == '(') {
> if(++level >= rule->data.number/2) level = 0;
> }
>
> return 1;
> }
>
__________________
dwk Seek and ye shall find. quaere et invenies. (Latin by dwks.) "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot "I have not failed. I've just found 10,000 ways that won't work." -- Thomas Alva Edison "The only real mistake is the one from which we learn nothing." -- John Powell My website: http://dwks.theprogrammingsite.com/ Other programming boards: codeform version 1.2.0 online is here! |
|
dwks is online now
|
|
02-13-2007, 09:00 PM
|
#10 |
|
Just Lurking
Join Date: Oct 2002
Location: Coon Rapids, MN, US
Posts: 4,045
|
I
downloaded it the other day, but haven't had much time to tinker with
it yet. I should have really RTMF before asking, but for HTML can you
specify a background? And if it is not yet in there, do you have any
intention of adding indent-like handling: making it a one-stop-shop slurp-and-burp crap-code-to-nifty-code thingamabob?
[Damn my technical writing sucks as of late.] ![]()
__________________
Pay no attention to that man behind the curtain! We now return you to your regularly scheduled programming... |
Dave_Sinkula is offline
|
|
02-14-2007, 05:02 AM
|
#11 |
|
Registered Pusher
Join Date: Dec 2005
Location: Ireland
Posts: 1,527
|
>>one-stop-shop slurp-and-burp crap-code-to-nifty-code thingamabob?
An omniscient, omnipotent and infallible code-beautifier?
__________________
"I will show you fear in a handful of dust." - TS Eliot Foreman: You stash your drugs in a lupus textbook? House: It's never lupus |
twomers is offline
|
|
02-14-2007, 09:45 AM
|
#12 | |
|
C maniac
Join Date: Aug 2004
Location: Calgary, AB, CANADA
Posts: 107
|
Quote:
.
__________________
"The biggest pain is the pain of a new idea." www.theprogrammingsite.com I use Dev-C++, make, gcc, cygwin, MS-Visual 6.0 Intro. |
|
kawk is offline
|
|
02-14-2007, 10:15 AM
|
#13 | |
|
DWK
Join Date: Apr 2005
Location: Canada
Posts: 4,087
|
HTML backgrounds
Yes, you can specify a background. Just change the text in =start (yes, I modelled it after POD), possibly on the command line. Code:
$ codeform -e '=start:<html><head><title>codeform</title></head><body bgcolor="#000000">' -f rules/c_1_html Quote:
). It would definitely be interesting to try to make a real formatter
and make codeform live up to its name. I'll let you know if I decide to.Unterminated comment bug twomers PMed me with a bug that I had overlooked in the last stages of development. Try an unterminated comment or string. Code:
//comment Code:
/*comment Code:
"string Code:
if(call_type_cdat(rules, tf)) return 1; Code:
if(call_one_type(rules, tf)) redo = 1; Code:
if(call_one_type(rules, tf) || call_type_cdat(rules, tf)) {
redo = 1;
}
Warning with GCC 3.4.2 Also, the warning that GCC gives Code:
codeform.c:738: warning: 'tlen' might be used uninitialized in this function Code:
void add_rules_file(struct rules_t *rules, const char *fn) {
FILE *fp = open_file(fn, "r");
char *str = 0;
int line = 0, isb;
size_t tlen, len, flen = 0, alen = 0;
if(!fp) return;
do {
len = 0;
isb = 1;
while(isb && (tlen = get_string(&str, len, &alen, fp)) != len) {
flen = tlen;
if(flen && str[flen-1] == '\n') str[--flen] = 0;
isb = is_backslashed(str+len, str+flen);
len = flen;
line ++;
if(len && str[len-1] == '\\') str[--len] = 0;
}
add_rule_var(rules, str, fn, line);
} while(tlen != len);
if(fp != stdin) fclose(fp);
free(str);
}
Code:
size_t tlen = 0, len, flen = 0, alen = 0;
__________________
dwk Seek and ye shall find. quaere et invenies. (Latin by dwks.) "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot "I have not failed. I've just found 10,000 ways that won't work." -- Thomas Alva Edison "The only real mistake is the one from which we learn nothing." -- John Powell My website: http://dwks.theprogrammingsite.com/ Other programming boards: codeform version 1.2.0 online is here! Last edited by dwks : 02-14-2007 at 01:33 PM. |
|
dwks is online now
|
|
02-14-2007, 06:50 PM
|
#14 | ||
|
Just Lurking
Join Date: Oct 2002
Location: Coon Rapids, MN, US
Posts: 4,045
|
Quote:
Quote:
__________________
Pay no attention to that man behind the curtain! We now return you to your regularly scheduled programming... |
||
Dave_Sinkula is offline
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|