// File: billDrett[P]Bulletin-Boards/boardServer.cpp
// Function: BoardServer::parseGetFiles(char*, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void BoardServer::parseGetFiles(char* buffer, int nread, int state, std::string& previousRead)
{
    string name, tmpName;
    char* tmp = buffer;
    int len, fd, id;

    if(previousRead.size() != 30) //ta prwta 30 bytes exoun to chanel id kai to onoma tou file
    {
        if(nread < (30 - previousRead.size())) //an dn arkoun tha diavasw ta ypoloipa se epomeno read
        {
            previousRead.append(buffer, nread);
            return;
        }
        len = 30 - previousRead.size(); // grafw osa apomenoun
        previousRead.append(buffer, len);
        tmp = &buffer[len];
        nread -= len;

        istringstream iss(previousRead); //ama yparxei hdh to arxeio ston server dn theloume na to xalasei
        iss>>id >>name;
        tmpName = name;
        fileNameExists(tmpName);
        if(name != tmpName) //ama ypaxei hdh to onoma antikatastash me to kainourgio
        {
            previousRead.erase(previousRead.find(name));
            previousRead.append(tmpName);
            for(int i = previousRead.size(); i < 30; i++)
            {
                previousRead.append(" ");
            }
        }
    }

    istringstream iss(previousRead);
    iss>>id >>name;
    if(iss.fail())
    {
        perror("cant get id and file name");
        return;
    }
    fd = open(name.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0644);
    write(fd, tmp, nread); //grafoume to arxeio
    close(fd);


    if(state == waitingEndCommand) //afou to pirame olo to vazoume kai sto chanel
    {
        string* stringName = new string (name);
        this->addFileToChanel(id, stringName);
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3d0, %rsp            # imm = 0x3D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3a0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x33e0
leaq -0x60(%rbp), %rdi
callq 0x33e0
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3210
cmpq $0x1e, %rax
je 0x86d4
movslq -0x14(%rbp), %rax
movq %rax, -0x3a8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3210
movq %rax, %rdx
movq -0x3a8(%rbp), %rax
movl $0x1e, %ecx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x8576
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x3070
jmp 0x8553
movl $0x1, -0x88(%rbp)
jmp 0x8848
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x886f
movq -0x20(%rbp), %rdi
callq 0x3210
movl %eax, %ecx
movl $0x1e, %eax
subl %ecx, %eax
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x6c(%rbp), %rdx
callq 0x3070
jmp 0x859e
movq -0x10(%rbp), %rax
movslq -0x6c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x6c(%rbp), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x208(%rbp), %rdi
movl $0x8, %edx
callq 0x3480
jmp 0x85cf
leaq -0x208(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x31e0
movq %rax, -0x3b0(%rbp)
jmp 0x85e8
movq -0x3b0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x33a0
jmp 0x85fa
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3040
jmp 0x8609
leaq -0x60(%rbp), %rdi
callq 0x8ee0
jmp 0x8614
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8950
testb $0x1, %al
jne 0x862a
jmp 0x86c8
movq -0x20(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
xorl %eax, %eax
movl %eax, %edx
leaq -0x40(%rbp), %rsi
callq 0x3550
movq -0x3b8(%rbp), %rdi
movq %rax, %rsi
movq $-0x1, %rdx
callq 0x30c0
jmp 0x865a
movq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3510
jmp 0x8669
movq -0x20(%rbp), %rdi
callq 0x3210
movl %eax, -0x20c(%rbp)
cmpl $0x1e, -0x20c(%rbp)
jge 0x86c6
movq -0x20(%rbp), %rdi
leaq 0x2a42(%rip), %rsi      # 0xb0ce
callq 0x3500
jmp 0x8693
jmp 0x8695
movl -0x20c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20c(%rbp)
jmp 0x8678
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x31a0
jmp 0x886f
jmp 0x86c8
leaq -0x208(%rbp), %rdi
callq 0x31a0
movq -0x20(%rbp), %rsi
leaq -0x390(%rbp), %rdi
movl $0x8, %edx
callq 0x3480
jmp 0x86eb
leaq -0x390(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x31e0
movq %rax, -0x3c0(%rbp)
jmp 0x8704
movq -0x3c0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x33a0
jmp 0x8716
movq -0x390(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x390(%rbp,%rax), %rdi
callq 0x3030
movb %al, -0x3c1(%rbp)
jmp 0x8736
movb -0x3c1(%rbp), %al
testb $0x1, %al
jne 0x8742
jmp 0x8773
leaq 0x2b0c(%rip), %rdi      # 0xb255
callq 0x3260
jmp 0x8750
movl $0x1, -0x88(%rbp)
jmp 0x883c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8863
leaq -0x40(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x441, %esi            # imm = 0x441
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3230
movl %eax, -0x3c8(%rbp)
jmp 0x8798
movl -0x3c8(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %edi
movq -0x68(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x3110
jmp 0x87b3
movl -0x70(%rbp), %edi
callq 0x3520
jmp 0x87bd
cmpl $0x3, -0x18(%rbp)
jne 0x8832
movl $0x20, %edi
callq 0x32d0
movq %rax, -0x3d0(%rbp)
jmp 0x87d6
movq -0x3d0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3150
jmp 0x87e8
movq -0x3a0(%rbp), %rdi
movq -0x3d0(%rbp), %rax
movq %rax, -0x398(%rbp)
movl -0x74(%rbp), %esi
movq -0x398(%rbp), %rdx
callq 0x5f40
jmp 0x880e
jmp 0x8832
movq -0x3d0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x20, %esi
callq 0x32e0
jmp 0x8863
movl $0x0, -0x88(%rbp)
leaq -0x390(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x31f0
leaq -0x40(%rbp), %rdi
callq 0x31f0
addq $0x3d0, %rsp            # imm = 0x3D0
popq %rbp
retq
leaq -0x390(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x31f0
leaq -0x40(%rbp), %rdi
callq 0x31f0
movq -0x80(%rbp), %rdi
callq 0x34b0
nopw (%rax,%rax)