insstr.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. insstr
  5. ------
  6. ### Synopsis
  7. int insstr(const char *str);
  8. int insnstr(const char *str, int n);
  9. int winsstr(WINDOW *win, const char *str);
  10. int winsnstr(WINDOW *win, const char *str, int n);
  11. int mvinsstr(int y, int x, const char *str);
  12. int mvinsnstr(int y, int x, const char *str, int n);
  13. int mvwinsstr(WINDOW *win, int y, int x, const char *str);
  14. int mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n);
  15. int ins_wstr(const wchar_t *wstr);
  16. int ins_nwstr(const wchar_t *wstr, int n);
  17. int wins_wstr(WINDOW *win, const wchar_t *wstr);
  18. int wins_nwstr(WINDOW *win, const wchar_t *wstr, int n);
  19. int mvins_wstr(int y, int x, const wchar_t *wstr);
  20. int mvins_nwstr(int y, int x, const wchar_t *wstr, int n);
  21. int mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr);
  22. int mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n);
  23. ### Description
  24. The insstr() functions insert a character string into a window at the
  25. current cursor position, by repeatedly calling winsch(). When
  26. PDCurses is built with wide-character support enabled, the narrow-
  27. character functions treat the string as a multibyte string in the
  28. current locale, and convert it first. All characters to the right of
  29. the cursor are moved to the right, with the possibility of the
  30. rightmost characters on the line being lost. The cursor position
  31. does not change (after moving to y, x, if specified). The routines
  32. with n as the last argument insert at most n characters; if n is
  33. negative, then the entire string is inserted.
  34. ### Return Value
  35. All functions return OK on success and ERR on error.
  36. ### Portability
  37. X/Open ncurses NetBSD
  38. insstr Y Y Y
  39. winsstr Y Y Y
  40. mvinsstr Y Y Y
  41. mvwinsstr Y Y Y
  42. insnstr Y Y Y
  43. winsnstr Y Y Y
  44. mvinsnstr Y Y Y
  45. mvwinsnstr Y Y Y
  46. ins_wstr Y Y Y
  47. wins_wstr Y Y Y
  48. mvins_wstr Y Y Y
  49. mvwins_wstr Y Y Y
  50. ins_nwstr Y Y Y
  51. wins_nwstr Y Y Y
  52. mvins_nwstr Y Y Y
  53. mvwins_nwstr Y Y Y
  54. **man-end****************************************************************/
  55. #include <string.h>
  56. int winsnstr(WINDOW *win, const char *str, int n)
  57. {
  58. #ifdef PDC_WIDE
  59. wchar_t wstr[513], *p;
  60. int i;
  61. #endif
  62. int len;
  63. PDC_LOG(("winsnstr() - called: string=\"%s\" n %d \n", str, n));
  64. if (!win || !str)
  65. return ERR;
  66. len = strlen(str);
  67. if (n < 0 || n > len)
  68. n = len;
  69. #ifdef PDC_WIDE
  70. if (n > 512)
  71. n = 512;
  72. p = wstr;
  73. i = 0;
  74. while (str[i] && i < n)
  75. {
  76. int retval = PDC_mbtowc(p, str + i, n - i);
  77. if (retval <= 0)
  78. break;
  79. p++;
  80. i += retval;
  81. }
  82. while (p > wstr)
  83. if (winsch(win, *--p) == ERR)
  84. #else
  85. while (n)
  86. if (winsch(win, (unsigned char)(str[--n])) == ERR)
  87. #endif
  88. return ERR;
  89. return OK;
  90. }
  91. int insstr(const char *str)
  92. {
  93. PDC_LOG(("insstr() - called: string=\"%s\"\n", str));
  94. return winsnstr(stdscr, str, -1);
  95. }
  96. int winsstr(WINDOW *win, const char *str)
  97. {
  98. PDC_LOG(("winsstr() - called: string=\"%s\"\n", str));
  99. return winsnstr(win, str, -1);
  100. }
  101. int mvinsstr(int y, int x, const char *str)
  102. {
  103. PDC_LOG(("mvinsstr() - called: y %d x %d string=\"%s\"\n", y, x, str));
  104. if (move(y, x) == ERR)
  105. return ERR;
  106. return winsnstr(stdscr, str, -1);
  107. }
  108. int mvwinsstr(WINDOW *win, int y, int x, const char *str)
  109. {
  110. PDC_LOG(("mvwinsstr() - called: string=\"%s\"\n", str));
  111. if (wmove(win, y, x) == ERR)
  112. return ERR;
  113. return winsnstr(win, str, -1);
  114. }
  115. int insnstr(const char *str, int n)
  116. {
  117. PDC_LOG(("insnstr() - called: string=\"%s\" n %d \n", str, n));
  118. return winsnstr(stdscr, str, n);
  119. }
  120. int mvinsnstr(int y, int x, const char *str, int n)
  121. {
  122. PDC_LOG(("mvinsnstr() - called: y %d x %d string=\"%s\" n %d \n",
  123. y, x, str, n));
  124. if (move(y, x) == ERR)
  125. return ERR;
  126. return winsnstr(stdscr, str, n);
  127. }
  128. int mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n)
  129. {
  130. PDC_LOG(("mvwinsnstr() - called: y %d x %d string=\"%s\" n %d \n",
  131. y, x, str, n));
  132. if (wmove(win, y, x) == ERR)
  133. return ERR;
  134. return winsnstr(win, str, n);
  135. }
  136. #ifdef PDC_WIDE
  137. int wins_nwstr(WINDOW *win, const wchar_t *wstr, int n)
  138. {
  139. const wchar_t *p;
  140. int len;
  141. PDC_LOG(("wins_nwstr() - called\n"));
  142. if (!win || !wstr)
  143. return ERR;
  144. for (len = 0, p = wstr; *p; p++)
  145. len++;
  146. if (n < 0 || n > len)
  147. n = len;
  148. while (n)
  149. if (winsch(win, wstr[--n]) == ERR)
  150. return ERR;
  151. return OK;
  152. }
  153. int ins_wstr(const wchar_t *wstr)
  154. {
  155. PDC_LOG(("ins_wstr() - called\n"));
  156. return wins_nwstr(stdscr, wstr, -1);
  157. }
  158. int wins_wstr(WINDOW *win, const wchar_t *wstr)
  159. {
  160. PDC_LOG(("wins_wstr() - called\n"));
  161. return wins_nwstr(win, wstr, -1);
  162. }
  163. int mvins_wstr(int y, int x, const wchar_t *wstr)
  164. {
  165. PDC_LOG(("mvins_wstr() - called\n"));
  166. if (move(y, x) == ERR)
  167. return ERR;
  168. return wins_nwstr(stdscr, wstr, -1);
  169. }
  170. int mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr)
  171. {
  172. PDC_LOG(("mvwinsstr() - called\n"));
  173. if (wmove(win, y, x) == ERR)
  174. return ERR;
  175. return wins_nwstr(win, wstr, -1);
  176. }
  177. int ins_nwstr(const wchar_t *wstr, int n)
  178. {
  179. PDC_LOG(("ins_nwstr() - called\n"));
  180. return wins_nwstr(stdscr, wstr, n);
  181. }
  182. int mvins_nwstr(int y, int x, const wchar_t *wstr, int n)
  183. {
  184. PDC_LOG(("mvinsnstr() - called\n"));
  185. if (move(y, x) == ERR)
  186. return ERR;
  187. return wins_nwstr(stdscr, wstr, n);
  188. }
  189. int mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n)
  190. {
  191. PDC_LOG(("mvwinsnstr() - called\n"));
  192. if (wmove(win, y, x) == ERR)
  193. return ERR;
  194. return wins_nwstr(win, wstr, n);
  195. }
  196. #endif