Are there any simple standardized way of reading a full line from a socket that mallocs the memory for you? If so, what are they? If not, is there a way to do it in approximately O(1) time (obviously not including the time for the memory allocation).
The better way is to read data into a buffer, and after there's no more data (at this moment), read all complete lines from the buffer. Implementation is left as an exercise for the reader.
malloc it yourself, use dynamic expansion (double, triple, whatever) on the buffer.
If not, is there a way to do it in approximately O(1) time
Is it possible to read 1 byte and 1,000,000 bytes in the same amount of time? That was a rhetorical question.
Name:
Anonymous2008-08-20 0:15
I guess ill just send integers over the socket to the server, that way I don't have to do any parsing!
Name:
Anonymous2008-08-20 0:44
First choice: Use Erlang.
Second choice: Use JAVA.
else: kill yourself.
Name:
Anonymous2008-08-20 7:35
FILE* f;
char buffa[LINE_MAX];
f = fdopen(socket, "r+");
fgets(buffa, sizeof(buffa), f);
Name:
Anonymous2008-08-20 8:14
O(1)? I highly doubt it because socket memory is claimed by the kernel. I think you have to copy it unless you work in kernel mode.
Dynamic languages usually have a function to read an entire line into dynamically allocated memory. Tons of people use these functions every day, so why is it any different to want to do this in C? By the way, using fdopen is a total fucking hack and you know it.
An example of why I want a function like this: Say you're building a web server, and you want to read in the request headers. You could read them line by line, parsing each line as you go, or you could read the whole request at once until the client disconnects or something. Either way theres the possibility that the request is malformed and it could totally fuck up your application.
Obviously you could set the maximum amount of characters per line, because I believe there is an HTTP code specifically for that, but why?
>>23 By the way, using fdopen is a total fucking hack and you know it.
0/10
Name:
Anonymous2008-08-20 15:03
Tons of people use these functions every day, so why is it any different to want to do this in C?
Because C has no infrastructure in which to do anything automatic. Enjoy your portable assembler.
I agree with >>25 . You can use C to make programs that do all sorts of cool shit, but you can't do cool shit inside the programming language itself unless you resort to code generation or other such non-C solutions.
Name:
Anonymous2008-08-20 15:30
>>25
I will, along with my job in the embedded market.
Name:
Anonymous2008-08-20 15:49
>>28 I will, along with fighting against constraints and manual re-implementations of everything.
You can't have your cake and eat it, too, with flexibility and embedded limitations. So the answer to >>1 is:
if (embedded || low-level)
deal with the shittiness of C
else
use anything else, fuckhead! C is a waste of time!