File: add_char.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (103 lines) | stat: -rw-r--r-- 2,719 bytes parent folder | download | duplicates (5)
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
/* Copyright (C) 2000, 2001 Damir Zucic */

/*=============================================================================

				add_char.c

Purpose:
	Add one character  to the command string;  copy the string  to the
	history buffer.

Input:
	(1) Pointer to RuntimeS structure.
	(2) Pointer to GUIS structure.
	(2) The new character.

Output:
	(1) One character added to the input string.
	(2) Return value.

Return value:
	(1) Positive on success.
	(2) Negative on failure.

Notes:
	(1) Only insert mode is available: it is not possible to overwrite
	    characters.

	(2) Input overflow is not allowed.

========includes:============================================================*/

#include <stdio.h>

#include <string.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#include "defines.h"
#include "typedefs.h"

/*======add one character to command string:=================================*/

int AddCharToCommand_ (RuntimeS *runtimeSP, GUIS *guiSP, int new_char)
{
int			comm_length, old_comm_length, carriage_pos;
char			stringA[COMMSTRINGSIZE];
char			*currP;

/* Copy the command string length and carriage (keyboard cursor) position: */
comm_length = runtimeSP->command_length;
old_comm_length = comm_length;
carriage_pos = runtimeSP->carriage_position;

/* Check the command string length: */
if (comm_length >= COMMSTRINGSIZE - 2) return -1;

/* Append the character  to the end  of command */
/* string, if carriage is at the end of string: */
if (carriage_pos == comm_length)
	{
	runtimeSP->curr_commandA[comm_length] = new_char;
	runtimeSP->curr_commandA[comm_length + 1] = '\0';
	}

/* Insert character, if carriage is not at the end of command string: */
else
	{
	strncpy (stringA, runtimeSP->curr_commandA, carriage_pos);
	stringA[carriage_pos] = new_char;
	stringA[carriage_pos + 1] = '\0';
	strncat (stringA,
		 runtimeSP->curr_commandA + carriage_pos,
		 comm_length - carriage_pos);
	stringA[comm_length + 1] = '\0';
	strcpy (runtimeSP->curr_commandA, stringA);
	}

/* Update the command length and carriage position: */
comm_length++;
runtimeSP->command_length = comm_length;
carriage_pos++;
runtimeSP->carriage_position = carriage_pos;

/* Copy the command string to the history buffer: */
currP = runtimeSP->commandsP + runtimeSP->next_commandI * COMMSTRINGSIZE;
strcpy (currP, runtimeSP->curr_commandA);

/* Find and store the command string width: */
runtimeSP->left_part_widthA[old_comm_length] =
			XTextWidth (guiSP->main_winS.fontSP,
				    runtimeSP->curr_commandA,
				    runtimeSP->carriage_position);

/* Return positive value (success): */
return 1;
}

/*===========================================================================*/