[Dev-C++] About passing text files from UNIX to DOS in a text editor.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <ten...@ne...> - 2003-07-31 16:54:57
|
Just now I came back to my pad trial to make it recognize UNIX text files and DOS text files, so it displays UNIX files correctly in lines, not one hugh file with black rectangles, an so I substitute notepad with it as the default text file program. It works perfectly . But I found one unconsistency: I wrote two functions, addret and remret, both receive LPTSTR and int&. addret work perfect, I do, in pesudocode: Count \n in the string. Hold the new sizeof the string in variable resize. Allocate space for the extended string. Cycle through the string from beginning to end searching for \n. When I found one I move from there ot end one byte right, from end to the place. Put a \r there. size = resize. Put a '\0' in [resize], although there is yet a '\0'. The result is the desired: I get the UNIX file displayed in lines, with all \n tranfromed in \r\n. The "problem" arises in remret: I count the number of \n (or \r, they're the same). I get the new size in resize. I search for \r, from start to end. When I find it I move all bytes from there to end one byte left. I GlobalRealloc to resize + 1. I make [resize] '\0'. I make size = resize. Now the string is converted to UNIX, where there was a \r\n now there is a \n: but the text has being cut in size - resize characters. Just as if \r\n were only one byte. If I comment the realloc and the [resize] \0, then it works perfect: all the text is converted to UNIX and there is the complete text. Another thing I noticed (I did it as it must, but shouldn't, be done not after seeing that the way it should doesn't work right): GetWindowTextLength retireves the control text length, without the \0 (or even if it includes it). Then GetWindowText retrieves the text of the control until reach the limit set by the third parameter. Nothing is said about the null end character. But consistency demands that if you pass the value retruned by the first function to the second, this will give you the full text (both include or not the end null character). Well, it doesn't: it places a \0 at the last character or doesn't read it, so it cuts the last character, if you really want retrieve the full text you shall add 1 to that value: length = GetWindowTextLength(GetDlgItem(hwnd, ID_EDIT)); text = (LPTSTR)GlobalAlloc(GPTR, length + 1); text[length] = '\0'; GetWindowText(GetDlgItem(hwnd, ID_EDIT), text, length + 1); |