Your daily WTF
11-Jul-08
I found this today while doing code review.
it was ment to be a sleep timer…
I just about shit a brick when I saw this.
for(int i=0; i<PAUSE_LENGTH; i++)
{
i++;
}
Rantings from Steven smethurst
I found this today while doing code review.
it was ment to be a sleep timer…
I just about shit a brick when I saw this.
for(int i=0; i<PAUSE_LENGTH; i++)
{
i++;
}
This code snippet should let you add a new post to your wordpress database 2.5.1
require_once('wp-config.php');
// create post object
class wm_mypost {
var $post_title;
var $post_content;
var $post_status; /* publish, private */
var $post_author; /* author user id (optional) */
var $post_name; /* slug (optional) */
var $post_type; /* 'page' or 'post' (optional, defaults to 'post') */
var $comment_status; /* open or closed for commenting (optional) */
var $post_category ;
}
// initialize post object
$wm_mypost = new wm_mypost();
$wm_mypost->post_title = "Title2 ". date( 'r' );
$wm_mypost->post_content = "content3";
$wm_mypost->post_status = 'publish';
$wm_mypost->post_author = 1;
// Catagorys
$post_category = split("," , "one");
foreach($post_category as $key=>$val) {
$post_category[$key] = get_cat_ID($val);
}
$wm_mypost->post_category = $post_category ;
// Optional; uncomment as needed
// $wm_mypost->post_type = 'page';
// $wm_mypost->comment_status = 'closed';
// feed object to wp_insert_post
$post_ID = wp_insert_post($wm_mypost);
echo date( 'r' ) . "\n";
echo "post_ID:". $post_ID . "\n";
I got asked how to create a simple averaging class.
If you where feeling smart you could enhance this class in to a template class for a object that has the =,+,>,< operator. But I’m feeling lazy today.
class CMinMaxAvg
{
private :
int m_count;
int m_total;
int m_min;
int m_max;
public:
CMinMaxAvg() {
m_count = 0 ;
m_total = 0 ;
m_min = 0 ;
m_max = 0 ;
}
void add( int iNum ) {
if( m_count == 0 ) {
SetMax( iNum, true ) ;
SetMin( iNum, true );
} SetMax( iNum, false );
SetMin( iNum, false );
m_count ++;
m_total += iNum ;
}
void SetMax( int iNum, bool force=true ) {
if( m_max < iNum || force ) {
m_max = iNum ;
}
}
void SetMin( int iNum, bool force=true ) {
if( m_min > iNum || force) {
m_min = iNum ;
}
}
float GetAvg( ) {
return (float)m_total/(float)m_count ;
}
int GetMax() {
return m_max ;
}
int GetMin() {
return m_min ;
}
};
I create a lot of windows application to make things easier for my customers. The simple act of coping a file from an email to a certain directory can become the most complicated tasks for a certain type of customer.
I use an Scriptable Install System provided by NullSoft called NSIS (Nullsoft Scriptable Install System). Ita great system that just plain works. Its free, open source, and can be used for commercial products, Great plugin system, Dead simple for simple things, ect.
The manual has lots of examples and function documentation to help get you started.
Over the last few years of using this program I have created a library of useful little snippets of code. Free free to comment with your own.
Opening a directory
ExecShell “open” ‘”$INSTDIR”‘
BringToFront
Registering/unregistering personal ActiveX files
UnRegDLL “$SYSDIR\spin32.ocx”
RegDLL “$SYSDIR\spin32.ocx”
Checking if a process is running
This uses the FindProcDLL::FindProc plug-in (here also):
StrCpy $1 “mybin.exe”
FindProcDLL::FindProc “$1″
;0 = Process was not found
;1 = Process was found
;605 = Unable to search for process
;606 = Unable to identify system type
;607 = Unsupported OS
;632 = Process name is invalid
StrCmp $R0 0 0 error
File “mybin.exe” ; Can’t use a variable
Goto end
error:
MessageBox MB_OK|MB_ICONSTOP “The application $1 is currently running. Press CTRL-ALT-DEL to display the list of running processes.”
Quit
end:
I’m often surprised how many times this question has come up by beginner programmers.
How do you tell the size of a file in win32?
This method will fail on files greater then 4GB, and its slower then other methods but it quick and easy as long as you are not dealing with files greater then 4gb.
FILE * h_file = NULL ;
if( h_file = fopen( h_file, "rb" ) != NULL ) {
fseek(h_file, 0, SEEK_END);
long file_size = ftell(h_file);
fclose( h_file );
}