logo
Powered by QM on a Rpi server
Home
About OpenQM
Sales and Downloads
Help and Support
About
Login

KnowledgeBase 00048: User Written Conversion Codes

Last updated: 17 Feb 2017
Applies to: All versions
Search  
Top level index       Full Index Search Tips
Previous article     Next article

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.


The Conversion Subroutine

A user written conversion code is a subroutine declared as

   SUBROUTINE NAME(RESULT, SRC, STATE, IS.OCONV) 
where
RESULTis the returned conversion result.
SRCis the data to be converted.
STATEis 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.OCONVis 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.


Example

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 

Using the New Code

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") 


Related Articles

None.



Please tell us if this article was helpful
Very     Slightly     Not at all
Comments
Email (optional)