strfbits.c.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. strfbits.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='strdecr.c.html' title=' strdecr.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='strincr.c.html' title=' strincr.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * size_t strfbits (char buffer [], size_t length, char const * operands [], char const * operator, unsigned flagword);
  24. *
  25. * format.h
  26. *
  27. * format buffer with an enumerated list of the bits in a flagword;
  28. * each flagword bit position corresponds to a string in operands[]
  29. * and operator is the string separating formatted operands;
  30. *
  31. * enumeration continues until all bits are enumerated or operands
  32. * are exhausted or the buffer fills;
  33. *
  34. * for example, the following formats buffer with the literal string
  35. * &quot;one, three, five, six&quot; since those bits are set;
  36. *
  37. * char buffer[100];
  38. * char const operator = &quot;, &quot;;
  39. * char const *operands[] =
  40. * {
  41. * &quot;zero&quot;,
  42. * &quot;one&quot;,
  43. * &quot;two&quot;,
  44. * &quot;three&quot;,
  45. * &quot;four&quot;,
  46. * &quot;five&quot;,
  47. * &quot;six&quot;,
  48. * &quot;seven&quot;,
  49. * &quot;eight&quot;,
  50. * &quot;nine&quot;,
  51. * &quot;ten&quot;,
  52. * (char *)(0)
  53. * };
  54. * flag_t flags = 0x006C;
  55. *
  56. * strfbits (buffer, sizeof(buffer), operands, operator, flags);
  57. *
  58. * we decrement length before starting to reserve room for the NUL
  59. * terminator; not room ... no write; we then add length to buffer
  60. * before to compute the terminator address then subtract it after
  61. * to compute the buffer start; this minimizes indexing and offset
  62. * calculations within the loop;
  63. *
  64. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  65. * Copyright (c) 2001-2006 by Charles Maier Associates;
  66. * Licensed under the Internet Software Consortium License;
  67. *
  68. *--------------------------------------------------------------------*/
  69. #ifndef STRFBITS_SOURCE
  70. #define STRFBITS_SOURCE
  71. #include &lt;unistd.h&gt;
  72. #include &quot;../tools/memory.h&quot;
  73. #include &quot;../tools/flags.h&quot;
  74. size_t strfbits (char buffer [], size_t length, char const * operands [], char const * operator, unsigned flagword)
  75. {
  76. char * string = (char *)(buffer);
  77. char const *separator = &quot;&quot;;
  78. if (length--)
  79. {
  80. buffer += length;
  81. while ((*operands) &amp;&amp; (flagword))
  82. {
  83. if (flagword &amp; 1)
  84. {
  85. char const *symbol;
  86. for (symbol = separator; (*symbol) &amp;&amp; (string &lt; buffer); symbol++)
  87. {
  88. *string++ = *symbol;
  89. }
  90. for (symbol = *operands; (*symbol) &amp;&amp; (string &lt; buffer); symbol++)
  91. {
  92. *string++ = *symbol;
  93. }
  94. separator = operator;
  95. }
  96. flagword &gt;&gt;= 1;
  97. operands++;
  98. }
  99. *string = (char) (0);
  100. buffer -= length;
  101. }
  102. return (string - (char *)(buffer));
  103. }
  104. #endif
  105. </pre>
  106. <div class='footerlink'>
  107. [<a href='strdecr.c.html' title=' strdecr.c '>PREV</a>]
  108. [<a href='toolkit.html' title=' Index '>HOME</a>]
  109. [<a href='strincr.c.html' title=' strincr.c '>NEXT</a>]
  110. </div>
  111. </body>
  112. </html>