// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1d8, %rsp            # imm = 0x1D8
movq %r8, %r13
movl %ecx, -0x30(%rbp)
movl %edx, %r12d
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, %r15
movq %rdi, -0x60(%rbp)
xorl %ecx, %ecx
leaq -0x70(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
movq 0x8(%r8), %rax
cmpq $0x1e, %rax
je 0x5167
movslq %r12d, %rdx
pushq $0x1e
popq %rcx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x509e
movq %r13, %rdi
movq %r15, %rsi
callq 0x2050
jmp 0x5211
pushq $0x1e
popq %rcx
subl %eax, %ecx
movslq %ecx, %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %rdx, -0x58(%rbp)
callq 0x2050
leaq -0x200(%rbp), %rdi
pushq $0x8
popq %rdx
movq %r13, %rsi
callq 0x2360
leaq -0x200(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x21a0
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22b0
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2190
leaq -0x80(%rbp), %rdi
callq 0x54a9
leaq -0x50(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x52d5
testb %al, %al
je 0x5151
leaq -0x50(%rbp), %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x2410
pushq $-0x1
popq %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x2090
leaq -0x80(%rbp), %rsi
movq %r13, %rdi
callq 0x23e0
movl 0x8(%r13), %ebx
leaq 0x1fb7(%rip), %r14      # 0x70f4
cmpl $0x1e, %ebx
jge 0x5151
movq %r13, %rdi
movq %r14, %rsi
callq 0x23c0
incl %ebx
jmp 0x513d
movq -0x58(%rbp), %rax
addq %rax, %r15
subl %eax, %r12d
leaq -0x200(%rbp), %rdi
callq 0x2150
leaq -0x200(%rbp), %rdi
pushq $0x8
popq %rdx
movq %r13, %rsi
callq 0x2360
leaq -0x200(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x21a0
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22b0
movq -0x200(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x1e0(%rbp,%rax)
jne 0x5235
movq -0x50(%rbp), %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x21c0
movl %eax, %ebx
movslq %r12d, %rdx
movl %eax, %edi
movq %r15, %rsi
callq 0x20e0
movl %ebx, %edi
callq 0x23f0
cmpl $0x3, -0x30(%rbp)
jne 0x5205
pushq $0x20
popq %rdi
callq 0x2240
movq %rax, %r14
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22e0
movl -0x2c(%rbp), %esi
movq -0x60(%rbp), %rdi
movq %r14, %rdx
callq 0x3dea
leaq -0x200(%rbp), %rdi
callq 0x2150
leaq -0x80(%rbp), %rdi
callq 0x2130
leaq -0x50(%rbp), %rdi
callq 0x2130
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x203f(%rip), %rdi      # 0x727b
callq 0x21e0
jmp 0x5205
jmp 0x5259
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x2250
jmp 0x5265
jmp 0x5259
jmp 0x5262
movq %rax, %rbx
jmp 0x5271
jmp 0x5262
jmp 0x5262
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
callq 0x2150
leaq -0x80(%rbp), %rdi
callq 0x2130
leaq -0x50(%rbp), %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x2390