ReactOS  0.4.14-dev-323-g6fe6a88
pager.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Console Utilities Library
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Console/terminal paging functionality.
5  * COPYRIGHT: Copyright 2017-2018 ReactOS Team
6  * Copyright 2017-2018 Hermes Belusca-Maito
7  */
8 
16 /* FIXME: Temporary HACK before we cleanly support UNICODE functions */
17 #define UNICODE
18 #define _UNICODE
19 
20 #include <windef.h>
21 #include <winbase.h>
22 // #include <winnls.h>
23 #include <wincon.h> // Console APIs (only if kernel32 support included)
24 #include <strsafe.h>
25 
26 #include "conutils.h"
27 #include "stream.h"
28 #include "screen.h"
29 #include "pager.h"
30 
31 // Temporary HACK
32 #define CON_STREAM_WRITE ConStreamWrite
33 
34 
35 
36 /* Returns TRUE when all the text is displayed, and FALSE if display is stopped */
37 BOOL
39  IN PCON_PAGER Pager,
41  IN BOOL StartPaging,
42  IN PTCHAR szStr,
43  IN DWORD len)
44 {
46 
47  /* Used to see how big the screen is */
48  DWORD ScreenLines = 0;
49 
50  /* Chars since start of line */
51  DWORD CharSL;
52 
53  DWORD from = 0, i = 0;
54 
55  /* Parameters validation */
56  if (!Pager)
57  return FALSE;
58 
59  /* Reset LineCount and return if no string has been given */
60  if (StartPaging == TRUE)
61  Pager->LineCount = 0;
62  if (szStr == NULL)
63  return TRUE;
64 
65  /* Get the size of the visual screen that can be printed to */
66  if (!ConGetScreenInfo(Pager->Screen, &csbi))
67  {
68  /* We assume it's a file handle */
69  CON_STREAM_WRITE(Pager->Screen->Stream, szStr, len);
70  return TRUE;
71  }
72 
73  /*
74  * Get the number of lines currently displayed on screen, minus 1
75  * to account for the "press any key..." prompt from PagePrompt().
76  */
77  ScreenLines = (csbi.srWindow.Bottom - csbi.srWindow.Top);
78  CharSL = csbi.dwCursorPosition.X;
79 
80  /* Make sure the user doesn't have the screen too small */
81  if (ScreenLines < 4)
82  {
83  CON_STREAM_WRITE(Pager->Screen->Stream, szStr, len);
84  return TRUE;
85  }
86 
87  while (i < len)
88  {
89  /* Search until the end of a line is reached */
90  if (szStr[i++] != TEXT('\n') && ++CharSL < csbi.dwSize.X)
91  continue;
92 
93  Pager->LineCount++;
94  CharSL = 0;
95 
96  if (Pager->LineCount >= ScreenLines)
97  {
98  CON_STREAM_WRITE(Pager->Screen->Stream, &szStr[from], i-from);
99  from = i;
100 
101  /* Prompt the user; give him some values for statistics */
102  // FIXME TODO: The prompt proc can also take ScreenLines ??
103  if (!PagePrompt(Pager, from, len))
104  return FALSE;
105 
106  // TODO: Recalculate 'ScreenLines' in case the user redimensions
107  // the window during the prompt.
108 
109  /* Reset the number of lines being printed */
110  Pager->LineCount = 0;
111  }
112  }
113  if (i > from)
114  CON_STREAM_WRITE(Pager->Screen->Stream, &szStr[from], i-from);
115 
116  return TRUE;
117 }
118 
119 BOOL
121  IN PCON_PAGER Pager,
123  IN BOOL StartPaging,
124  IN LPTSTR szStr)
125 {
126  DWORD len;
127 
128  /* Return if no string has been given */
129  if (szStr == NULL)
130  return TRUE;
131 
132  len = wcslen(szStr);
133  return ConWritePaging(Pager, PagePrompt, StartPaging, szStr, len);
134 }
135 
136 BOOL
138  IN PCON_PAGER Pager,
140  IN BOOL StartPaging,
142  IN UINT uID)
143 {
144  INT Len;
145  PWCHAR szStr = NULL;
146 
147  Len = K32LoadStringW(hInstance, uID, (PWSTR)&szStr, 0);
148  if (szStr && Len)
149  return ConWritePaging(Pager, PagePrompt, StartPaging, szStr, Len);
150  else
151  return TRUE;
152 }
153 
154 BOOL
156  IN PCON_PAGER Pager,
158  IN BOOL StartPaging,
159  IN UINT uID)
160 {
161  return ConResPagingEx(Pager, PagePrompt, StartPaging,
162  NULL /*GetModuleHandleW(NULL)*/, uID);
163 }
164 
165 /* EOF */
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
static BOOL __stdcall PagePrompt(PCON_PAGER Pager, DWORD Done, DWORD Total)
Definition: more.c:51
int ScreenLines
Definition: ctm.c:78
#define CON_STREAM_WRITE
Definition: pager.c:32
BOOL ConGetScreenInfo(IN PCON_SCREEN Screen, OUT PCONSOLE_SCREEN_BUFFER_INFO pcsbi)
Definition: screen.c:73
uint16_t * PWSTR
Definition: typedefs.h:54
BOOL ConResPaging(IN PCON_PAGER Pager, IN PAGE_PROMPT PagePrompt, IN BOOL StartPaging, IN UINT uID)
Definition: pager.c:155
INT WINAPI K32LoadStringW(IN HINSTANCE hInstance OPTIONAL, IN UINT uID, OUT LPWSTR lpBuffer, IN INT nBufferMax)
Definition: utils.c:173
uint16_t * PWCHAR
Definition: typedefs.h:54
Console/terminal screen management.
int32_t INT
Definition: typedefs.h:56
CHAR * LPTSTR
Definition: xmlstorage.h:192
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HINSTANCE hInstance
Definition: charmap.c:20
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
ULONG X
Definition: bl.h:1340
SHORT Bottom
Definition: blue.h:28
BOOL ConPutsPaging(IN PCON_PAGER Pager, IN PAGE_PROMPT PagePrompt, IN BOOL StartPaging, IN LPTSTR szStr)
Definition: pager.c:120
BOOL(__stdcall * PAGE_PROMPT)(IN PCON_PAGER, IN DWORD, IN DWORD)
Definition: pager.h:54
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
SHORT Top
Definition: blue.h:26
GLenum GLsizei len
Definition: glext.h:6722
Console/terminal paging functionality.
#define TEXT(s)
Definition: k32.h:26
BOOL ConResPagingEx(IN PCON_PAGER Pager, IN PAGE_PROMPT PagePrompt, IN BOOL StartPaging, IN HINSTANCE hInstance OPTIONAL, IN UINT uID)
Definition: pager.c:137
unsigned int UINT
Definition: ndis.h:50
BOOL ConWritePaging(IN PCON_PAGER Pager, IN PAGE_PROMPT PagePrompt, IN BOOL StartPaging, IN PTCHAR szStr, IN DWORD len)
Definition: pager.c:38
CardRegion * from
Definition: spigame.cpp:19
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
char * PTCHAR
Definition: ntbasedef.h:483
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68