File: ml_model.c

package info (click to toggle)
mlterm 2.9.4-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,728 kB
  • ctags: 5,338
  • sloc: ansic: 74,649; sh: 8,532; cpp: 1,451; makefile: 1,126; perl: 496; sed: 16
file content (319 lines) | stat: -rw-r--r-- 5,080 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 *	$Id: ml_model.c,v 1.11 2005/02/21 14:43:41 h_minami Exp $
 */

#include  "ml_model.h"

#include  <kiklib/kik_mem.h>		/* malloc/free */
#include  <kiklib/kik_debug.h>
#include  <kiklib/kik_util.h>


/* --- global functions --- */

int
ml_model_init(
	ml_model_t *  model ,
	u_int  num_of_cols ,
	u_int  num_of_rows
	)
{
	int  count ;

	if( num_of_rows == 0 || num_of_cols == 0)
	{
		return  0 ;
	}
		
	model->num_of_rows = num_of_rows ;
	model->num_of_cols = num_of_cols ;

	if( ( model->lines = calloc( sizeof( ml_line_t), model->num_of_rows)) == NULL)
	{
	#ifdef  DEBUG
		kik_warn_printf( KIK_DEBUG_TAG "calloc() failed.\n") ;
	#endif
	
		return  0 ;
	}
	
	for( count = 0 ; count < model->num_of_rows ; count ++)
	{
		if( ! ml_line_init( &model->lines[count] , model->num_of_cols))
		{
			return  0 ;
		}
	}

	model->beg_row = 0 ;

	return  1 ;
}

int
ml_model_final(
	ml_model_t *  model
	)
{
	int  count ;
	
	for( count = 0 ; count < model->num_of_rows ; count ++)
	{
		ml_line_final( &model->lines[count]) ;
	}
	
	free( model->lines) ;

	return  1 ;
}

int
ml_model_reset(
	ml_model_t *  model
	)
{
	int  count ;
	
	for( count = 0 ; count < model->num_of_rows ; count ++)
	{
		ml_line_reset( &model->lines[count]) ;
		ml_line_updated( &model->lines[count]) ;
	}

	return  1 ;
}

int
ml_model_resize(
	ml_model_t *  model ,
	u_int *  slide ,
	u_int  num_of_cols ,
	u_int  num_of_rows
	)
{
	int  old_row ;
	int  new_row ;
	int  count ;
	u_int  copy_rows ;
	u_int  copy_len ;
	ml_line_t *  lines_p ;
	u_int  filled_rows ;

	if( num_of_cols == 0 || num_of_rows == 0)
	{
		return  0 ;
	}
	
	if( num_of_cols == model->num_of_cols && num_of_rows == model->num_of_rows)
	{
		/* not resized */

		return  0 ;
	}

	if( ( lines_p = calloc( sizeof( ml_line_t), num_of_rows)) == NULL)
	{
	#ifdef  DEBUG
		kik_warn_printf( KIK_DEBUG_TAG " malloc() failed.\n") ;
	#endif
	
		return  0 ;
	}
	
	copy_len = K_MIN(num_of_cols , model->num_of_cols) ;

	count = model->num_of_rows - 1 ;
	while( 1)
	{
		if( count < 0)
		{
			/* All lines are empty, which is impossible. */
			free( lines_p) ;		
			return  0 ;
		}
	#if  0
		/*
		 * This is problematic, since the value of 'slide' can be incorrect when
		 * cursor is located at the line which contains white spaces alone.
		 */
		else if( ml_get_num_of_filled_chars_except_spaces( ml_model_get_line( model , count)) > 0)
	#else
		else if( ! ml_line_is_empty( ml_model_get_line( model , count)))
	#endif
		{
			filled_rows = count + 1 ;
			
			break ;
		}
		else
		{
			count -- ;
		}
	}

	if( num_of_rows >= filled_rows)
	{
		old_row = 0 ;
		copy_rows = filled_rows ;
	}
	else
	{
		old_row = filled_rows - num_of_rows ;
		copy_rows = num_of_rows ;
	}

	if( slide)
	{
		*slide = old_row ;
	}

	/*
	 * updating each line.
	 */

	for( new_row = 0 ; new_row < copy_rows ; new_row ++)
	{
		ml_line_init( &lines_p[new_row] , num_of_cols) ;

		ml_line_copy_line( &lines_p[new_row] , ml_model_get_line( model , old_row)) ;
		old_row ++ ;
		
		ml_line_set_modified_all( &lines_p[new_row]) ;
	}

	for( ; new_row < num_of_rows ; new_row ++)
	{
		ml_line_init( &lines_p[new_row] , num_of_cols) ;

		ml_line_set_modified_all( &lines_p[new_row]) ;
	}

	/*
	 * freeing old data.
	 */
	 
	for( count = 0 ; count < model->num_of_rows ; count ++)
	{
		ml_line_final( &model->lines[count]) ;
	}

	free( model->lines) ;
	
	model->lines = lines_p ;

	model->num_of_rows = num_of_rows ;
	model->num_of_cols = num_of_cols ;

	model->beg_row = 0 ;

	return  1 ;
}

int
ml_model_end_row(
	ml_model_t *  model
	)
{
	return  model->num_of_rows - 1 ;
}

ml_line_t *
ml_model_get_line(
	ml_model_t *  model ,
	int  row
	)
{
	if( row < 0 || model->num_of_rows <= row)
	{
		return  NULL ;
	}

	if( model->beg_row + row < model->num_of_rows)
	{
		return  &model->lines[model->beg_row + row] ;
	}
	else
	{
		return  &model->lines[model->beg_row + row - model->num_of_rows] ;
	}
}

int
ml_model_scroll_upward(
	ml_model_t *  model ,
	u_int  size
	)
{
	if( size > model->num_of_rows)
	{
		size = model->num_of_rows ;
	}
	
	if( model->beg_row + size >= model->num_of_rows)
	{
		model->beg_row = model->beg_row + size - model->num_of_rows ;
	}
	else
	{
		model->beg_row += size ;
	}
	
	return  1 ;
}

int
ml_model_scroll_downward(
	ml_model_t *  model ,
	u_int  size
	)
{
	if( size > model->num_of_rows)
	{
		size = model->num_of_rows ;
	}
	
	if( model->beg_row < size)
	{
		model->beg_row = model->num_of_rows - (size - model->beg_row) ;
	}
	else
	{
		model->beg_row -= size ;
	}
	
	return  1 ;
}

#ifdef  DEBUG

void
ml_model_dump(
	ml_model_t *  model
	)
{
	int  row ;
	ml_line_t *  line ;
	
	for( row = 0 ; row < model->num_of_rows ; row++)
	{
		line = ml_model_get_line( model , row) ;

		if( ml_line_is_modified( line))
		{
			kik_msg_printf( "!%.2d-%.2d" ,
				ml_line_get_beg_of_modified( line) ,
				ml_line_get_end_of_modified( line)) ;
		}
		else
		{
			kik_msg_printf( "      ") ;
		}
		
		kik_msg_printf( "[%.2d %.2d]" , line->num_of_filled_chars ,
			ml_line_get_num_of_filled_cols( line)) ;

		ml_str_dump( line->chars , line->num_of_filled_chars) ;
	}
}

#endif