George V. Reilly

NVelocity: loading templates from embedded resources

In last week's tip on using the NVelocity template formatting engine, I described what to set to load a template from an absolute path.

Here's the magic necessary to get NVelocity to load a template from an embedded resource:

VelocityEngine engine = new VelocityEngine();
ExtendedProperties properties = new ExtendedProperties();
properties.AddProperty("resource.loader", "assembly");
properties.AddProperty("assembly.resource.loader.class",
    "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity");
properties.AddProperty("assembly.resource.loader.assembly", "StencilFormatter");
engine.Init(properties);

NVelocity templates and absolute paths

We've started using the NVelocity template formatting engine. We were absolutely stymied for an hour, trying to figure out how to get it working with an absolute path to the template file, instead of the relative path shown in the doc­u­men­ta­tion.

The trick is to set file.resource.loader.path. Here's how to load C:\foo\bar\­some­file.vm:

ExtendedProperties props = new ExtendedProperties();
props.AddProperty("file.resource.loader.path", new ArrayList(new string[]{".", "C:\\"}));
velocity.Init(props);

template = velocity.GetTemplate("foo\\bar\\somefile.vm");

Printf %n

In my post about Printf Tricks a couple of years ago, I mentioned that "%n is dangerous and disabled by default in Visual Studio 2005."

I got email today from someone who was porting a large codebase to VS 2005. He was getting an assert from %n and he needed a way to get past it. He intends to fix the uses of %n when he has a chance.

I spent several minutes digging around in MSDN and came up with set_print­f_­coun­t_out­put. Wikipedi­a's Format string attack page led me to Exploiting Format String Vul­ner­a­bil­i­ties, which describes in detail how %n (and %s) may be exploited.

In short, if you continue.

The String Pod

I recently learned about string pods and chain pods. In essence, they are pocket-sized monopods. You screw a six-foot string into the tripod socket of your camera, step on the other end of the string, and pull it taut. The tension on the string reduces camera shake.

My string pod tutorial shows how I made the string pod, as well as some before and after shots.

Before now, I used to try to find a handy surface or wall to brace the camera, when taking photos without flash. Often there isn't such a surface. I have a little 3-inch pocket tripod that I carry with me all the time, but I haven't continue.

Python Batchfile Wrapper

I've been getting into Python lately. One problem that I've en­coun­tered under Windows, is that input redi­rec­tion doesn't work if you use the .py file as­so­ci­a­tion to run the script; e.g.:

C:\> foo.py < input.txt

There's a well-known input redi­rec­tion bug. The fix is to explicitly use python.exe to run the script.

A related problem for me was that stdin was opened as a text file, not a binary file, so \r bytes were being discarded from binary input files. The fix is to run python.exe -u (unbuffered binary input and output).

I didn't want to hardcode the path to python.exe in a batch file, so I came up with the continue.

Never Sleep(0) in an Infinite Loop

I ran into a problem installing some COM+ components today. The installer was using Regsvcs.exe to register each COM+ component. I noticed after a while that the installer wasn't making any progress and that my dual-proc system was stuck at 50% CPU uti­liza­tion. I attached a debugger to the offending process, regsvcs, and found that it was stuck in the following infinite loop (dis­as­sem­bly courtesy of Reflector):

internal void System.EnterpriseServices.CatalogSync.Wait()
{
  if (this._set)
  {
    RegistryKey key1
      = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\CLSID");
    while (true)
    {
      int num1 = (int) key1.GetValue("CLBVersion", 0);
 
continue.

Using Log4Net from a COM+ Application

I spent far too much time on Friday trying to make log4net work in a COM+ ap­pli­ca­tion.

Someone else had done part of the work necessary, by creating an ap­pli­ca­tion.config for the COM+ ap­pli­ca­tion and setting a custom Ap­pli­ca­tion Root Directory. This was enough to ensure that most of the managed code in the ap­pli­ca­tion got their con­fig­u­ra­tion settings; log4net being the exception.

It took some additional work to realize that we needed to add two assembly attributes:

[assembly: log4net.Config.Repository("unique-name")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile="application.config")]

The repository name just needs to be a unique string. We used the name of the assembly.

Aerobie AeroPress

Aerobie AeroPress home page

CoffeeGeek thread

Adler's American recipe

Luke­Seu­bert's Smooth Americano recipe

I like my Americano made with 175F water to the top of the "2" oval, with our standard ten-second stir, and press with no steep time. Then diluted 1:1 after pressing.

Mark likes his brewed with much hotter water, with a 40 second steep time and he likes to push all of the water through the press, rather than diluting aftewards. His recipe has much more edge (which I might call bitterness).

Sweet Maria's in­struc­tions

Lots of reviews

Lack of 'soul'

Negative pressure

Neutral pressure

Espresso definition

Crema

"Espresso strength"

I find that the drip-through, even with long steep or long stir times, isn't continue.

0xFF...FF / 10 == 0x19...99

A few weeks ago, I wrote a C++ routine to parse decimal numbers using the overflow detection principles of SafeInt. I couldn't find anything in the libraries that actually did a good job of checking for overflow.

Briefly, to see if unsigned values A+B overflow, check if (A > MAX_UINT - B). Similarly, A*B will overflow if (A > MAX_UINT / B).

// Convert a string to an unsigned. Returns 'true' iff conversion is legitimate.
bool
StringToUnsigned(
    const string& str,
    unsigned&     rUint)
{
    rUint = 0;

    if (str.empty())
        
continue.

Vim Syntax Highlighting for FlexWiki

We use FlexWiki at work. It's an ASP.NET-based wiki, a low-overhead, organic way of sharing knowledge.

The only built-in means of editing a page in FlexWiki is to type into an HTML textbox, which is a horrendous user experience. There's no WYSIWYG feedback showing you whether you've got the wiki markup right.

Back in December, Emma and I went to the Oregon coast for a week. We had no Internet access and long dark evenings, so I spent quite a bit of time on my laptop, working on a couple of projects. One was a new theme (skin) for DasBlog, which I didn't finish to my sat­is­fac­tion. I really ought to continue.

Previous » « Next