C Board  

Go Back   C Board > Community Boards > General Discussions
Welcome, dwks.
You last visited: 03-10-2007 at 03:31 PM
Private Messages: Unread 2, Total 278.
User CP FAQ Members List Calendar Search New Posts Mark Forums Read Open Buddy List Log Out


Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 02-11-2007, 12:17 PM   #1
dwks
DWK
 
dwks's Avatar
 
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
The rules files that come with codeform include C, C++, and Java; the output rules files include CSS (you may have to supply a stylesheet), inline CSS, and HTML; and the styles include 1, scite, devcpp, and kate.

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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-11-2007, 02:44 PM   #2
twomers
Registered Pusher
 
twomers's Avatar
 
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-11-2007, 04:49 PM   #3
dwks
DWK
 
dwks's Avatar
 
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-11-2007, 05:27 PM   #4
dwks
DWK
 
dwks's Avatar
 
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-12-2007, 12:40 PM   #5
twomers
Registered Pusher
 
twomers's Avatar
 
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;
}
Make that program. Put it in the codeform dir, and make a shortcut to it on your desktop or something. Copy the code you want coloured to the clipboard, run the program, get lovely code on the clipboard, and there you have it.

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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-12-2007, 01:31 PM   #6
SlyMaelstrom
Drink your milk, kids!
 
SlyMaelstrom's Avatar
 
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-12-2007, 05:04 PM   #7
dwks
DWK
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 4,087
Quote:
Originally Posted by SlyMaelstrom
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".
Yeah, or "five times the speed". That's still a little ambigous. "Five times faster" is the best.

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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-12-2007, 06:39 PM   #8
SlyMaelstrom
Drink your milk, kids!
 
SlyMaelstrom's Avatar
 
Join Date: May 2004
Location: Out of scope
Posts: 2,938
Quote:
Originally Posted by dwks
Yeah, or "five times the speed". That's still a little ambigous. "Five times faster" is the best.
Five times faster sound good... I think that's what my Cable ISP uses in their commercials. However, I'd rather the read be uncertain as to what I meant than have them certain that I was saying something I didn't mean to.

Quote:
Originally Posted by dwks
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.
Heh, define positive context. If I made an syntax coloring application that was half the speed of yours and I said in my advertisement "Half the speed of Code Form!", should I get my hopes up in that maybe some viewers will read that as "Twice the speed?"


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)));
If you have that option, then awesome... if not, maybe it can be an option for the next revision? 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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-13-2007, 03:33 PM   #9
dwks
DWK
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 4,087
Quote:
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)));
If you have that option, then awesome... if not, maybe it can be an option for the next revision? Nice work from what I can see, though. Me likes.
No, it doesn't, but it's simple enough to implement because codeform is so flexible. I did it in a few minutes; here's your code coloured with that version.
Code:
printf("Example: %d",sin(atoi((const char*)foo)));
The colours are changable, of course. Here was the command line:
Code:
C:\>outclip | codeform_parenthesis -f rules\_vbb -f parenthesis -f rules\c_1 | clipboard
Here is the rules file parenthesis:
Code:
=parenthesis

parenthesisbb=$(formbb)
parenthesisba=$(formba)
parenthesisa=$(forma)

$(parenthesisbb)red$(parenthesisba):$(parenthesisa):\
$(parenthesisbb)green$(parenthesisba):$(parenthesisa):\
$(parenthesisbb)blue$(parenthesisba):$(parenthesisa)
(You can add more than three colours.) The colours wrap if you have, say, four parenthesis like (((()))).

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;
> }
>
From that you should be able to make the source file. If you don't have experience with diff output, tell me and I'll PM you the whole source file.
__________________
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-13-2007, 09:00 PM   #10
Dave_Sinkula
Just Lurking
 
Dave_Sinkula's Avatar
 
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-14-2007, 05:02 AM   #11
twomers
Registered Pusher
 
twomers's Avatar
 
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-14-2007, 09:45 AM   #12
kawk
C maniac
 
Join Date: Aug 2004
Location: Calgary, AB, CANADA
Posts: 107
Quote:
Originally Posted by Dave_Sinkula
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.]
Yes, you can. At least you could for codeform 1.10 . . . so you should be able to for this newer version .
__________________
"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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-14-2007, 10:15 AM   #13
dwks
DWK
 
dwks's Avatar
 
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
indent-like formatting

Quote:
>>[do you have any intention of adding a] one-stop-shop slurp-and-burp crap-code-to-nifty-code thingamabob?

An omniscient, omnipotent and infallible code-beautifier?
I hadn't really thought about it. I might, but my intention with codeform was to make it completely language-independent (I failed ). 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
Even though I said in the features list that codeform supported such code, it doesn't. The fix is really simple, though. Remove the line
Code:
if(call_type_cdat(rules, tf)) return 1;
from call_one_type() and put it where call_one_type() is called and format the if to look nice. (Or just put the above line as the last line in call_one_type().)
Code:
                if(call_one_type(rules, tf)) redo = 1;
->
Code:
                if(call_one_type(rules, tf) || call_type_cdat(rules, tf)) {
                    redo = 1;
                }
I'll upload the latest version, with that change, as soon as possible, but I have not done so yet. [edit] Okay, I've re-uploaded codeform. The .zip contains this modification now. [/edit]

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
is unfounded.
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);
}
Since isb is 1, get_string() is always called at least once, so tlen is initialized. If you wish, you can eliminate the warning with this:
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 Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Old 02-14-2007, 06:50 PM   #14
Dave_Sinkula
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Location: Coon Rapids, MN, US
Posts: 4,045
Quote:
Originally Posted by dwks
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
Danke. Hyperskimming isn't doing me any favors.

Quote:
Originally Posted by dwks
indent-like formatting


I hadn't really thought about it. I might, but my intention with codeform was to make it completely language-independent (I failed ). 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.
Just a thought.
__________________
Pay no attention to that man behind the curtain!
We now return you to your regularly scheduled programming...
Dave_Sinkula is offline Report Post   Reply With Quote Multi-Quote This Message Quick reply to this message
Reply


Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Options

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -7. The time now is 12:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2007, Jelsoft Enterprises Ltd.