|Powered by QM on a Rpi server|
This article was originally published as a Tip of the Week.
QM provides the ability for developers to add new conversion codes that can then be used in all situations where the standard built-in codes can be used.
A user written conversion code is a subroutine declared as
SUBROUTINE NAME(RESULT, SRC, STATE, IS.OCONV)where
|RESULT||is the returned conversion result.|
|SRC||is the data to be converted.|
|STATE||is the value to be returned by the STATUS() function after the conversion is complete. This should be 0 for success, 1 if the conversion fails, or 2 if the code is not valid (e.g. use of an output only code in an input conversion).|
|IS.OCONV||is true if the subroutine is called for an output conversion, false for an input conversion.|
The subroutine must be catalogued in a mode that makes it available to all accounts in which it is to be used.
The example below is a base 36 encoding where a non-negative numeric value is encoded to a series of alphanumeric characters in the minimum possible space.
subroutine b36(result, (src), state, is.oconv) $catalogue b36 global if is.oconv then result = '' if src matches '1N0N' then loop result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'[rem(src, 36)+1, 1] : result src = idiv(src, 36) while src repeat state = 0 end else state = 1 end end else result = 0 for i = 1 to len(src) j = index('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', upcase(src[i,1]), 1) - 1 if j < 0 then state = 1 result = '' return end result = result * 36 + j next i state = 0 end return end
The user written conversion code can be used in all places where a built-in code can be used. The conversion name is the same as the catalogue name of the conversion subroutine with a U prefix added. For example, the base 36 encoding shown above could be used as
ENCODED.CLIENT.NO = OCONV(CLIENT.NO, "UB36")