xref: /openbsd-src/lib/libform/frm_data.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: frm_data.c,v 1.4 1998/07/24 02:37:27 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *   Author: Juergen Pfeifer <Juergen.Pfeifer@T-Online.de> 1995,1997        *
33  ****************************************************************************/
34 
35 #include "form.priv.h"
36 
37 MODULE_ID("$From: frm_data.c,v 1.4 1998/02/11 12:13:42 tom Exp $")
38 
39 /*---------------------------------------------------------------------------
40 |   Facility      :  libnform
41 |   Function      :  bool data_behind(const FORM *form)
42 |
43 |   Description   :  Check for off-screen data behind. This is nearly trivial
44 |                    becose the begin of a field is fixed.
45 |
46 |   Return Values :  TRUE   - there are off-screen data behind
47 |                    FALSE  - there are no off-screen data behind
48 +--------------------------------------------------------------------------*/
49 bool data_behind(const FORM *form)
50 {
51   bool result = FALSE;
52 
53   if (form && (form->status & _POSTED) && form->current)
54     {
55       FIELD *field;
56 
57       field = form->current;
58       if (!Single_Line_Field(field))
59 	{
60 	  result = (form->toprow==0) ? FALSE : TRUE;
61 	}
62       else
63 	{
64 	  result = (form->begincol==0) ? FALSE : TRUE;
65 	}
66     }
67   return(result);
68 }
69 
70 /*---------------------------------------------------------------------------
71 |   Facility      :  libnform
72 |   Function      :  static char * After_Last_Non_Pad_Position(
73 |                                    char *buffer,
74 |                                    int len,
75 |                                    int pad)
76 |
77 |   Description   :  Find the last position in the buffer that doesn't
78 |                    contain a padding character.
79 |
80 |   Return Values :  The pointer to this position
81 +--------------------------------------------------------------------------*/
82 INLINE
83 static char * After_Last_Non_Pad_Position(char *buffer, int len, int pad)
84 {
85   char *end = buffer + len;
86 
87   assert(buffer && len>=0);
88   while ( (buffer < end) && (*(end-1)==pad) )
89     end--;
90 
91   return end;
92 }
93 
94 #define SMALL_BUFFER_SIZE (80)
95 
96 /*---------------------------------------------------------------------------
97 |   Facility      :  libnform
98 |   Function      :  bool data_ahead(const FORM *form)
99 |
100 |   Description   :  Check for off-screen data ahead. This is more difficult
101 |                    because a dynamic field has a variable end.
102 |
103 |   Return Values :  TRUE   - there are off-screen data ahead
104 |                    FALSE  - there are no off-screen data ahead
105 +--------------------------------------------------------------------------*/
106 bool data_ahead(const FORM *form)
107 {
108   bool result = FALSE;
109 
110   if (form && (form->status & _POSTED) && form->current)
111     {
112       static char buffer[SMALL_BUFFER_SIZE + 1];
113       FIELD *field;
114       bool large_buffer;
115       bool cursor_moved = FALSE;
116       char *bp;
117       char *found_content;
118       int pos;
119 
120       field = form->current;
121       assert(form->w);
122 
123       large_buffer = (field->cols > SMALL_BUFFER_SIZE);
124       if (large_buffer)
125 	bp = (char *)malloc((size_t)(field->cols) + 1);
126       else
127 	bp = buffer;
128 
129       assert(bp);
130 
131       if (Single_Line_Field(field))
132 	{
133 	  int check_len;
134 
135 	  pos = form->begincol + field->cols;
136 	  while (pos < field->dcols)
137 	    {
138 	      check_len = field->dcols - pos;
139 	      if ( check_len >= field->cols )
140 		check_len = field->cols;
141 	      cursor_moved = TRUE;
142 	      wmove(form->w,0,pos);
143 	      winnstr(form->w,bp,check_len);
144 	      found_content =
145 		After_Last_Non_Pad_Position(bp,check_len,field->pad);
146 	      if (found_content==bp)
147 		  pos += field->cols;
148 	      else
149 		{
150 		  result = TRUE;
151 		  break;
152 		}
153 	    }
154 	}
155       else
156 	{
157 	  pos = form->toprow + field->rows;
158 	  while (pos < field->drows)
159 	    {
160 	      cursor_moved = TRUE;
161 	      wmove(form->w,pos,0);
162 	      pos++;
163 	      winnstr(form->w,bp,field->cols);
164 	      found_content =
165 		After_Last_Non_Pad_Position(bp,field->cols,field->pad);
166 	      if (found_content!=bp)
167 		{
168 		  result = TRUE;
169 		  break;
170 		}
171 	    }
172 	}
173 
174       if (large_buffer)
175 	free(bp);
176 
177       if (cursor_moved)
178 	wmove(form->w,form->currow,form->curcol);
179     }
180   return(result);
181 }
182 
183 /* frm_data.c ends here */
184