29Nov/090
String matching with C++ PCRE
bool regex_match( const char *pattern, const std::string str, std::vector *substrings )
{
const char *err;
int erroffset = 0;
pcre *re = pcre_compile( pattern, 0, &err, &erroffset, NULL );
int ovector[90];
size_t rc;
if ( !re )
{
bug( "%s: regular expression compilation failed at: %d: %s", __FUNCTION__, erroffset, err );
return false;
}
if ( ( rc = pcre_exec(re, NULL, str.c_str(), str.length(), 0, 0, ovector, 90 ) ) < 0 )
switch( rc )
{
case PCRE_ERROR_NOMATCH: return false;
default: bug( "%s: Matching error: %d", __FUNCTION__, rc ); return false;
}
if ( substrings )
for ( size_t i = 0; i < rc; i++ )
substrings->push_back( str.substr( ovector[2*i], ovector[2*i+1] - ovector[2*i] ) );
return true;
}
bool simple_match( const char *pattern, const char *str )
{
std::string newPattern = "^";
for ( size_t i = 0; i < strlen(pattern); i++ )
{
if ( !isalnum( pattern[i] ) && !iscntrl(pattern[i]) )
{
if ( pattern[i] == '*' )
{
newPattern += ".*";
continue;
}
else
newPattern += '\\';
}
newPattern += pattern[i];
}
newPattern += "$";
return regex_match( newPattern.c_str(), str, NULL );
}
25Nov/092
no such file to load — mkmf (LoadError)
Ever get this error when installing a gem?
/usr/bin/ruby1.9 extconf.rb install mysql
extconf.rb:10:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:10:in `'
It happens when you don't have the development library installed. Easy enough to fix, just install ruby1.9-dev.
For ubuntu:
sudo apt-get install libruby1.9
25Nov/090
Ruby utility script to send email
Someone over at MUDBytes asked about sending emails from within their game. They were looking to do this from C++, which as we all know is a pain in the ass.
I wrote up this quick utility script in ruby for sending emails easily, without the risk of passing stuff to the command line.
Usage:
ruby sendemail.rb from@nospam.com to@nospam.com My Subject Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ornare, magna eu feugiat rhoncus, tellus tellus pulvinar lacus, sit amet malesuada risus leo id eros. Pellentesque placerat arcu nec massa hendrerit tristique. Lorem ipsum dolor sit amet, consectetur adipiscing elit. CTRL-C
This is an easy way of sending emails, and can be configured to authenticate with a server.
# Copyright (c) 2009 Sam O'Connor
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'rubygems'
require 'net/smtp'
require 'tmail'
require 'optparse'
require 'ostruct'
options = OpenStruct.new
options.smtp_server = 'localhost'
options.smtp_port = 25
options.smtp_user = nil
options.smtp_pass = nil
options.smtp_authtype = nil
opts = OptionParser.new do |opts|
opts.banner = "Usage sendmail.rb [options]\n\n\n\n"
opts.separator ""
opts.separator "Specific options:"
opts.on("-s", "--server [SERVER]", "Use a different SMTP server", "") do |server|
options.smtp_server = server
end
opts.on("-p", "--port [PORT]", Integer, "Use a different SMTP port.", "") do |port|
options.smtp_port = port
end
opts.on("-u", "--user [USER]", String, "Authenticate with a user", "") do |user|
options.smtp_user = user
end
opts.on("-p", "--pass [PASS]", String, "Password for user", "") do |pass|
options.smtp_pass = pass
end
opts.on("-t", "--type [TYPE]", [:plain, :login, :cram_md5], "Select authentication type (plain ,login, cram_md5). Normally use plain") do |t|
options.smtp_authtype = t
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
mail = TMail::Mail.new
mail.from = $stdin.gets
mail.to = $stdin.gets
mail.subject = $stdin.gets
body = String.new
until $stdin.eof?
body << $stdin.gets
end
mail.body = body
smtp = Net::SMTP.new(options.smtp_server, options.smtp_port )
smtp.start( 'localhost.localdomain', options.smtp_user, options.smtp_pass, options.smtp_authtype ) do |smtp|
smtp.send_message( mail.to_s, mail.from, mail.to )
end