0

This pset has been a truly humbling experience. I was able to get lookup working (I think, based on my testing), and parse I could think of a general idea and then had to work through the specifics with a lot of gdb debugging and a question on the forum.

For this part of the problem, I'm not even sure I'm on the right track. None of the check50 feedback has changed at all, and I'm not sure how else to test this one (don't know how to write a test program for it). Is there a way to curl test just this part, or is that not what curl does?

Here's my code:

bool load(FILE* file, BYTE** content, size_t* length)
{
   char* buffer = calloc(1, BYTES);      //1

   int sizeCounter = BYTES;      //2

    do 
    {
        fread(&buffer[sizeCounter - BYTES], BYTES, 1, file);      //3
        if(feof(file))      //4
        {
            break;
        }
        sizeCounter += BYTES;       //5
        buffer = realloc(buffer, sizeCounter);       //6
    }
    while(true);       //7

    *content = buffer;       //8
    *length = sizeCounter;       //9

    return true;
}

And here's my thinking, in pseudocode:

  1. Set up a buffer with some memory the size of Bytes (512)
  2. Set up a counter that is initially the same size as the buffer.

Start looping:

  1. Read information from file into the buffer, starting back at the beginning of the last allocated block of memory
  2. Check to see if we're at the end of the file, and if so, break.
  3. Since we're not at the end of the file, increase the file size counter
  4. realloc more memory so the buffer is ready to keep reading
  5. Keep looping

When the loop is finally over:

  1. Set the content to the buffer
  2. Set the length to the sizeCounter.

Am I on the right track here? Is the check50 just not working because I haven't written the indexes method yet? Or is it something else?

Thanks again for any help,

-Brian

1

This looks like a good track. I think you can find the problem fairly quickly with gdb, curl and ls -al. *length needs to be the exact length of the loaded file. ls -al will tell you what to expect. ls -al hello.html produces this result -rw-rw-r-- 1 ubuntu ubuntu 316 Oct 24 2015 hello.html which tells you that hello.html is 316 bytes. Try gdb ./server with a break in load at *length = sizeCounter; and in another terminal tab send the request curl http://localhost:8080/hello.html. (IMO curl is easier to use with gdb because it does not automagically load favicion and hello.html is a "manageable" size.).

It is possible to use fread to a better advantage. From the man page:

RETURN VALUE
On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

If the second argument to fread (size) is 1, it will return the number of bytes read. That piece of data could be very useful in determining the "size" of the file.

| improve this answer | |
  • ...wow. I was checking for a "\\" in a different part of the program instead of a "/", and that was causing my whole parse and lookup to be wrong. Thanks for the tip about curl and especially the return value on fread(). That made it really easy to fix this one. I'm down to two frowny faces on check50 and they're both for the part I haven't written yet. Can I send you a bottle of wine or something? You're a lifesaver. – Brian Smith Oct 30 '16 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .