// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %r8, %r13
movl %ecx, -0x54(%rbp)
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, -0x88(%rbp)
leaq -0x40(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x68(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq 0x8(%r8), %rax
cmpq $0x1e, %rax
je 0x6324
movslq %r12d, %rdx
movl $0x1e, %ecx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x621d
movq %r13, %rdi
movq %r15, %rsi
callq 0x2060
jmp 0x6403
movl $0x1e, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %rdx, -0x80(%rbp)
callq 0x2060
leaq -0x208(%rbp), %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0x2380
leaq -0x208(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x2180
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22c0
leaq -0x78(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2170
leaq -0x78(%rbp), %rdi
callq 0x6676
movq -0x48(%rbp), %r14
cmpq -0x70(%rbp), %r14
jne 0x629f
testq %r14, %r14
je 0x62fb
movq -0x78(%rbp), %rsi
movq -0x50(%rbp), %rdi
movq %r14, %rdx
callq 0x2240
testl %eax, %eax
je 0x62fb
movq -0x50(%rbp), %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r14, %rcx
callq 0x23b0
movq 0x8(%r13), %rcx
cmpq %rax, %rcx
jb 0x6455
movq %rax, 0x8(%r13)
movq (%r13), %rcx
movb $0x0, (%rcx,%rax)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %r13, %rdi
callq 0x20d0
movl 0x8(%r13), %ebx
cmpl $0x1d, %ebx
jg 0x62fb
addl $-0x1e, %ebx
leaq 0x1e0c(%rip), %r14      # 0x80f8
movq %r13, %rdi
movq %r14, %rsi
callq 0x23f0
incl %ebx
jne 0x62ec
movq -0x80(%rbp), %rax
addq %rax, %r15
subl %eax, %r12d
movq 0x5cbc(%rip), %rsi      # 0xbfc8
leaq -0x208(%rbp), %rdi
callq 0x22f0
leaq -0x190(%rbp), %rdi
callq 0x20e0
leaq -0x208(%rbp), %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0x2380
leaq -0x208(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x2180
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22c0
movq -0x208(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x1e8(%rbp,%rax)
jne 0x6447
movq -0x50(%rbp), %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x21b0
movl %eax, %ebx
movslq %r12d, %rdx
movl %eax, %edi
movq %r15, %rsi
callq 0x20f0
movl %ebx, %edi
callq 0x2400
cmpl $0x3, -0x54(%rbp)
jne 0x63e4
movl $0x20, %edi
callq 0x2220
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x37b2
movl -0x2c(%rbp), %esi
movq -0x88(%rbp), %rdi
callq 0x49c2
testq %rax, %rax
je 0x63e4
movq %rax, %rdi
movq %r14, %rsi
callq 0x788e
movq 0x5bdd(%rip), %rsi      # 0xbfc8
leaq -0x208(%rbp), %rdi
callq 0x22f0
leaq -0x190(%rbp), %rdi
callq 0x20e0
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x641c
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x6435
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e31(%rip), %rdi      # 0x827f
callq 0x21d0
jmp 0x63e4
leaq 0x1e51(%rip), %rdi      # 0x82ad
leaq 0x1e36(%rip), %rsi      # 0x8299
movq %rax, %rdx
xorl %eax, %eax
callq 0x2340
jmp 0x6483
movq %rax, %rbx
movl $0x20, %esi
movq %r14, %rdi
callq 0x2230
jmp 0x6491
jmp 0x6483
movq %rax, %rbx
jmp 0x64b0
jmp 0x648e
jmp 0x648e
jmp 0x648e
movq %rax, %rbx
movq 0x5b30(%rip), %rsi      # 0xbfc8
leaq -0x208(%rbp), %rdi
callq 0x22f0
leaq -0x190(%rbp), %rdi
callq 0x20e0
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x64c9
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x64e2
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0