Sample Header Ad - 728x90

m4 macro using variable number of tabs to line up a column

2 votes
1 answer
603 views
This is the macro I have so far: define(debug_macro', #ifndef $1_DEBUG #define $1_DEBUG'forloop(i',1',eval(5-(len($1)/4+ifelse(eval(len($1)%4 == 0)), 0', 1')), ')dnl0 #endif ') The goal is to put the correct number of tabs between the _DEBUG and the 0 character so they all line up. There may be missing parens or quotes but hopefully you get the idea of where I'm going. I'm using Xcode and have my tabs set to 4. Here are the changes I made to accommodate that: divert(`-1') # forloop(var, from, to, stmt) define(`forloop', pushdef($1', $2')_forloop($1', $2', $3', $4')popdef($1')') define(`_forloop', $4'ifelse($1, `$3', , define($1', incr($1))_forloop($1', $2', $3', $4')')') divert`'dnl changecom()dnl define(debug_macro', #ifndef $1_DEBUG #define $1_DEBUG'forloop(i', 1', ifelse(eval(len($1) >= 16), 1, 1, eval(5-(len($1)/4+ifelse(eval(len($1)%4 > 1), 0, 0', 1')))), ')0 #endif ') changecom()dnl Since I couldn't find 'forloop.m4' on my system I just included it. The division by 4 rounds so I added the check for when the remainder is greater than 1. I also change the run of spaces for a single tab character. For the interested I use this macro to define constants I use to turn on debugging of different areas of my application. This file is checked into git. There is then a file that is personal and not checked in that looks something like this: //#define NETWORK_DEBUG 1 The file is included in the myapp.pch file before including the above file. That way each programmer can have their own debug areas turned on or off as they please.
Asked by MyTallest (123 rep)
Dec 19, 2014, 09:07 PM
Last activity: Dec 22, 2014, 08:22 PM