| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // TX string to TX-text conversion and handling functions |
|---|
| 34 | // |
|---|
| 35 | // Author: J. van Wijk |
|---|
| 36 | // |
|---|
| 37 | // JvW 24-07-2005 Initial version, split off from TXUTIL |
|---|
| 38 | |
|---|
| 39 | #include <txlib.h> |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /*****************************************************************************/ |
|---|
| 43 | // Convert C-string to dynamically allocated TXLIB text structure |
|---|
| 44 | /*****************************************************************************/ |
|---|
| 45 | char **txString2Text // RET TXLIB text structure |
|---|
| 46 | ( |
|---|
| 47 | char *string, // IN null terminated string |
|---|
| 48 | int *maxlen, // INOUT line length |
|---|
| 49 | int *lines // OUT nr of lines |
|---|
| 50 | ) |
|---|
| 51 | { |
|---|
| 52 | char **text = NULL; // function return |
|---|
| 53 | int line = 0; // nr of lines done |
|---|
| 54 | int lpsize; // nr of line pointers |
|---|
| 55 | int length; // length of substring |
|---|
| 56 | char *start; // start of line |
|---|
| 57 | char *next; // end of line / next |
|---|
| 58 | |
|---|
| 59 | ENTER(); |
|---|
| 60 | |
|---|
| 61 | if (string != NULL) |
|---|
| 62 | { |
|---|
| 63 | lpsize = (strlen( string) * 3 / (*maxlen)) +9; // worst-case |
|---|
| 64 | //- Note: Fails on string with many very short lines => text is clipped |
|---|
| 65 | |
|---|
| 66 | TRACES(("Strlen: %d Maxlen: %d => Alloc %d pointers\n", |
|---|
| 67 | strlen(string), *maxlen, lpsize)); |
|---|
| 68 | |
|---|
| 69 | if ((text = TxAlloc( lpsize, sizeof(char *))) != NULL) |
|---|
| 70 | { |
|---|
| 71 | for ( start = string, line = 0; |
|---|
| 72 | start && strlen( start) && (line < lpsize -1); |
|---|
| 73 | start = next, line++) |
|---|
| 74 | { |
|---|
| 75 | length = strcspn( start, "\n"); |
|---|
| 76 | TRACES(("found newline at pos: %d\n", length)); |
|---|
| 77 | if (length > *maxlen) // no newline in sight |
|---|
| 78 | { |
|---|
| 79 | for ( length = *maxlen; |
|---|
| 80 | (length > 0) && (start[ length] != ' '); |
|---|
| 81 | length --) |
|---|
| 82 | { |
|---|
| 83 | //- find wordwrap position (strrchr would start at end!) |
|---|
| 84 | } |
|---|
| 85 | TRACES(("found word-wrap space at pos: %d\n", length)); |
|---|
| 86 | if (length == 0) // one very long word, split |
|---|
| 87 | { |
|---|
| 88 | length = *maxlen; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | TRACES(("line:%d length:%d s:'%-40.40s'\n", line, length, start)); |
|---|
| 92 | text[ line] = TxAlloc( 1, length +1); |
|---|
| 93 | strncpy( text[ line], start, length); |
|---|
| 94 | text[ line][length] = '\0'; |
|---|
| 95 | TRACES(("text[%d]:'%s'\n", line, text[line])); |
|---|
| 96 | |
|---|
| 97 | next = start + length; |
|---|
| 98 | if (((*next == ' ') && (*(next+1) != ' ')) || (*next == '\n')) |
|---|
| 99 | { |
|---|
| 100 | TRACES(( "skipping one whitespace at: '%-40.40s'\n", next)); |
|---|
| 101 | next++; // skip at most 1 whitespace |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | text[ line] = NULL; // explicit end-pointer |
|---|
| 105 | |
|---|
| 106 | TRACES(("line: %d\n", line)); |
|---|
| 107 | *lines = line; // nr of lines generated |
|---|
| 108 | *maxlen = 0; |
|---|
| 109 | while (line--) |
|---|
| 110 | { |
|---|
| 111 | length = strlen( text[ line]); |
|---|
| 112 | if (length > *maxlen) |
|---|
| 113 | { |
|---|
| 114 | *maxlen = length; // determine maximum length |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | TRACES(("lines: %d with maxlen: %d\n", *lines, *maxlen)); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | RETURN (text); |
|---|
| 121 | } // end 'txString2Text' |
|---|
| 122 | /*---------------------------------------------------------------------------*/ |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /*****************************************************************************/ |
|---|
| 126 | // Free memory allocated for dynamic TXLIB text |
|---|
| 127 | /*****************************************************************************/ |
|---|
| 128 | void txFreeText |
|---|
| 129 | ( |
|---|
| 130 | char **text // IN dyn. allocated text |
|---|
| 131 | ) |
|---|
| 132 | { |
|---|
| 133 | char **line; |
|---|
| 134 | |
|---|
| 135 | ENTER(); |
|---|
| 136 | |
|---|
| 137 | if (text != NULL) |
|---|
| 138 | { |
|---|
| 139 | for (line = text; *line; line++) |
|---|
| 140 | { |
|---|
| 141 | TxFreeMem( *line); |
|---|
| 142 | } |
|---|
| 143 | TxFreeMem( text); |
|---|
| 144 | } |
|---|
| 145 | VRETURN (); |
|---|
| 146 | } // end 'txFreeText' |
|---|
| 147 | /*---------------------------------------------------------------------------*/ |
|---|