comparison win/dw.c @ 95:636c37ca7238

Avoid allocating super huge buffers when dealing with the MLEs.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 30 May 2002 07:47:28 +0000
parents 98cce029a611
children 372a7581b312
comparison
equal deleted inserted replaced
94:7c3eef54c98c 95:636c37ca7238
30 DWTID _dwtid = -1; 30 DWTID _dwtid = -1;
31 31
32 #define PACKVERSION(major,minor) MAKELONG(minor,major) 32 #define PACKVERSION(major,minor) MAKELONG(minor,major)
33 33
34 #define IS_IE5PLUS (dwComctlVer >= PACKVERSION(5,80)) 34 #define IS_IE5PLUS (dwComctlVer >= PACKVERSION(5,80))
35
36 #ifndef MIN
37 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
38 #endif
35 39
36 char monthlist[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", 40 char monthlist[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
37 "Sep", "Oct", "Nov", "Dec" }; 41 "Sep", "Oct", "Nov", "Dec" };
38 42
39 int main(int argc, char *argv[]); 43 int main(int argc, char *argv[]);
4900 void dw_listbox_set_top(HWND handle, int top) 4904 void dw_listbox_set_top(HWND handle, int top)
4901 { 4905 {
4902 SendMessage(handle, LB_SETTOPINDEX, (WPARAM)top, 0); 4906 SendMessage(handle, LB_SETTOPINDEX, (WPARAM)top, 0);
4903 } 4907 }
4904 4908
4905 #define MLE_MAX 1000000
4906 /* 4909 /*
4907 * Adds text to an MLE box and returns the current point. 4910 * Adds text to an MLE box and returns the current point.
4908 * Parameters: 4911 * Parameters:
4909 * handle: Handle to the MLE to be queried. 4912 * handle: Handle to the MLE to be queried.
4910 * buffer: Text buffer to be imported. 4913 * buffer: Text buffer to be imported.
4911 * startpoint: Point to start entering text. 4914 * startpoint: Point to start entering text.
4912 */ 4915 */
4913 unsigned int dw_mle_import(HWND handle, char *buffer, int startpoint) 4916 unsigned int dw_mle_import(HWND handle, char *buffer, int startpoint)
4914 { 4917 {
4915 char *tmpbuf = calloc(1, MLE_MAX+1); 4918 int textlen, len = GetWindowTextLength(handle);
4916 int len; 4919 char *tmpbuf;
4917 4920
4918 if(strlen(buffer) < 1) 4921 if((textlen = strlen(buffer)) < 1)
4919 return startpoint; 4922 return startpoint;
4920 4923
4921 startpoint++; 4924 startpoint++;
4925 tmpbuf = calloc(1, len + textlen + startpoint + 2);
4922 4926
4923 if(startpoint < 0) 4927 if(startpoint < 0)
4924 startpoint = 0; 4928 startpoint = 0;
4925 4929
4926 GetWindowText(handle, tmpbuf, MLE_MAX);
4927
4928 len = strlen(tmpbuf);
4929 if(len) 4930 if(len)
4930 { 4931 {
4931 char *dest = &tmpbuf[startpoint+strlen(buffer)-1], *start = &tmpbuf[startpoint]; 4932 char *dest, *start;
4932 int copylen = len - startpoint; 4933 int copylen = len - startpoint;
4934
4935 GetWindowText(handle, tmpbuf, len+1);
4936
4937 dest = &tmpbuf[startpoint+textlen-1];
4938 start = &tmpbuf[startpoint];
4933 4939
4934 if(copylen > 0) 4940 if(copylen > 0)
4935 memcpy(dest, start, copylen); 4941 memcpy(dest, start, copylen);
4936 } 4942 }
4937 memcpy(&tmpbuf[startpoint], buffer, strlen(buffer)); 4943 memcpy(&tmpbuf[startpoint], buffer, textlen);
4938 4944
4939 SetWindowText(handle, tmpbuf); 4945 SetWindowText(handle, tmpbuf);
4940 4946
4941 free(tmpbuf); 4947 free(tmpbuf);
4942 return startpoint+strlen(buffer) - 1; 4948 return (startpoint + textlen - 1);
4943 } 4949 }
4944 4950
4945 /* 4951 /*
4946 * Grabs text from an MLE box. 4952 * Grabs text from an MLE box.
4947 * Parameters: 4953 * Parameters:
4950 * startpoint: Point to start grabbing text. 4956 * startpoint: Point to start grabbing text.
4951 * length: Amount of text to be grabbed. 4957 * length: Amount of text to be grabbed.
4952 */ 4958 */
4953 void dw_mle_export(HWND handle, char *buffer, int startpoint, int length) 4959 void dw_mle_export(HWND handle, char *buffer, int startpoint, int length)
4954 { 4960 {
4955 char *tmpbuf = malloc(MLE_MAX+1); 4961 int max, len = GetWindowTextLength(handle);
4956 4962 char *tmpbuf = calloc(1, len+2);
4957 GetWindowText(handle, tmpbuf, MLE_MAX); 4963
4958 tmpbuf[MLE_MAX] = 0; 4964 if(len)
4959 4965 GetWindowText(handle, tmpbuf, len+1);
4960 memcpy(buffer, &tmpbuf[startpoint], length); 4966
4967 buffer[0] = 0;
4968
4969 if(startpoint < len)
4970 {
4971 max = MIN(length, len - startpoint);
4972
4973 memcpy(buffer, &tmpbuf[startpoint], max);
4974 }
4961 4975
4962 free(tmpbuf); 4976 free(tmpbuf);
4963 } 4977 }
4964 4978
4965 /* 4979 /*
4969 * bytes: A pointer to a variable to return the total bytes. 4983 * bytes: A pointer to a variable to return the total bytes.
4970 * lines: A pointer to a variable to return the number of lines. 4984 * lines: A pointer to a variable to return the number of lines.
4971 */ 4985 */
4972 void dw_mle_query(HWND handle, unsigned long *bytes, unsigned long *lines) 4986 void dw_mle_query(HWND handle, unsigned long *bytes, unsigned long *lines)
4973 { 4987 {
4974 char *tmpbuf = malloc(MLE_MAX+1);
4975
4976 GetWindowText(handle, tmpbuf, MLE_MAX);
4977 tmpbuf[MLE_MAX] = 0;
4978
4979 if(bytes) 4988 if(bytes)
4980 *bytes = strlen(tmpbuf); 4989 *bytes = GetWindowTextLength(handle);
4981 if(lines) 4990 if(lines)
4982 *lines = (unsigned long)SendMessage(handle, EM_GETLINECOUNT, 0, 0); 4991 *lines = (unsigned long)SendMessage(handle, EM_GETLINECOUNT, 0, 0);
4983
4984 free(tmpbuf);
4985 } 4992 }
4986 4993
4987 /* 4994 /*
4988 * Deletes text from an MLE box. 4995 * Deletes text from an MLE box.
4989 * Parameters: 4996 * Parameters:
4991 * startpoint: Point to start deleting text. 4998 * startpoint: Point to start deleting text.
4992 * length: Amount of text to be deleted. 4999 * length: Amount of text to be deleted.
4993 */ 5000 */
4994 void dw_mle_delete(HWND handle, int startpoint, int length) 5001 void dw_mle_delete(HWND handle, int startpoint, int length)
4995 { 5002 {
4996 char *tmpbuf = malloc(MLE_MAX+1); 5003 int len = GetWindowTextLength(handle);
4997 int len; 5004 char *tmpbuf = calloc(1, len+2);
4998 5005
4999 GetWindowText(handle, tmpbuf, MLE_MAX); 5006 GetWindowText(handle, tmpbuf, len+1);
5000 tmpbuf[MLE_MAX] = 0; 5007
5001 5008 if(startpoint + length < len)
5002 len = strlen(tmpbuf); 5009 {
5003 5010 strcpy(&tmpbuf[startpoint], &tmpbuf[startpoint+length]);
5004 strcpy(&tmpbuf[startpoint], &tmpbuf[startpoint+length]); 5011
5005 5012 SetWindowText(handle, tmpbuf);
5006 SetWindowText(handle, tmpbuf); 5013 }
5007 5014
5008 free(tmpbuf); 5015 free(tmpbuf);
5009 } 5016 }
5010 5017
5011 /* 5018 /*
5078 * point: Start point of search. 5085 * point: Start point of search.
5079 * flags: Search specific flags. 5086 * flags: Search specific flags.
5080 */ 5087 */
5081 int dw_mle_search(HWND handle, char *text, int point, unsigned long flags) 5088 int dw_mle_search(HWND handle, char *text, int point, unsigned long flags)
5082 { 5089 {
5083 char *tmpbuf = malloc(MLE_MAX+1); 5090 int len = GetWindowTextLength(handle);
5084 int z, len, textlen, retval = 0; 5091 char *tmpbuf = calloc(1, len+2);
5085 5092 int z, textlen, retval = 0;
5086 GetWindowText(handle, tmpbuf, MLE_MAX); 5093
5087 tmpbuf[MLE_MAX] = 0; 5094 GetWindowText(handle, tmpbuf, len+1);
5088 5095
5089 len = strlen(tmpbuf);
5090 textlen = strlen(text); 5096 textlen = strlen(text);
5091 5097
5092 if(flags & DW_MLE_CASESENSITIVE) 5098 if(flags & DW_MLE_CASESENSITIVE)
5093 { 5099 {
5094 for(z=point;z<(len-textlen) && !retval;z++) 5100 for(z=point;z<(len-textlen) && !retval;z++)