From b845629c3e14a944aded257aabff6a49cc58e48e Mon Sep 17 00:00:00 2001 From: sorgelig Date: Wed, 4 Dec 2019 20:50:03 +0800 Subject: [PATCH] megasd: improve sgets(). --- support/megacd/cdd.cpp | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/support/megacd/cdd.cpp b/support/megacd/cdd.cpp index 3bebc9a..b970762 100644 --- a/support/megacd/cdd.cpp +++ b/support/megacd/cdd.cpp @@ -38,30 +38,35 @@ cdd_t::cdd_t() { static int sgets(char *out, int sz, char **in) { - char *instr = *in; - int cnt = 0; - - while(*instr && *instr != 10) + *out = 0; + do { - if (*instr == 13) + char *instr = *in; + int cnt = 0; + + while (*instr && *instr != 10) { + if (*instr == 13) + { + instr++; + continue; + } + + if (cnt < sz - 1) + { + out[cnt++] = *instr; + out[cnt] = 0; + } + instr++; - continue; } - if (cnt < sz - 1) - { - out[cnt++] = *instr; - out[cnt] = 0; - } - - instr++; + if(*instr == 10) instr++; + *in = instr; } + while (!*out && **in); - while (*instr && *instr == 10) instr++; - *in = instr; - - return cnt || *instr; + return *out; } int cdd_t::LoadCUE(const char* filename) {