MyString MultiLogFiles::readFileToString(const MyString &strFilename) { dprintf( D_FULLDEBUG, "MultiLogFiles::readFileToString(%s)\n", strFilename.Value() ); FILE *pFile = safe_fopen_wrapper_follow(strFilename.Value(), "r"); if (!pFile) { dprintf( D_ALWAYS, "MultiLogFiles::readFileToString: " "safe_fopen_wrapper_follow(%s) failed with errno %d (%s)\n", strFilename.Value(), errno, strerror(errno) ); return ""; } if ( fseek(pFile, 0, SEEK_END) != 0 ) { dprintf( D_ALWAYS, "MultiLogFiles::readFileToString: " "fseek(%s) failed with errno %d (%s)\n", strFilename.Value(), errno, strerror(errno) ); fclose(pFile); return ""; } int iLength = ftell(pFile); if ( iLength == -1 ) { dprintf( D_ALWAYS, "MultiLogFiles::readFileToString: " "ftell(%s) failed with errno %d (%s)\n", strFilename.Value(), errno, strerror(errno) ); fclose(pFile); return ""; } MyString strToReturn; strToReturn.reserve_at_least(iLength); fseek(pFile, 0, SEEK_SET); char *psBuf = new char[iLength+1]; /* We now clear the buffer to ensure there will be a NULL at the end of our buffer after the fread(). Why no just do psBuf[iLength] = 0 ? Because on Win32, iLength may not point to the end of the buffer because \r\n are converted into \n because the file is opened in text mode. */ memset(psBuf,0,iLength+1); int ret = fread(psBuf, 1, iLength, pFile); if (ret == 0) { dprintf( D_ALWAYS, "MultiLogFiles::readFileToString: " "fread failed with errno %d (%s)\n", errno, strerror(errno) ); fclose(pFile); delete [] psBuf; return ""; } fclose(pFile); strToReturn = psBuf; delete [] psBuf; return strToReturn; }
static bool docker_add_env_walker (void*pv, const MyString &var, const MyString &val) { ArgList* runArgs = (ArgList*)pv; MyString arg; arg.reserve_at_least(var.length() + val.length() + 2); arg = var; arg += "="; arg += val; runArgs->AppendArg("-e"); runArgs->AppendArg(arg); return true; // true to keep iterating }