MCPcopy Create free account
hub / github.com/ashkulz/NppFTP / ReadLine

Method ReadLine

UTCP/src/UTDataSource.cpp:259–308  ·  view source on GitHub ↗

ReadLine Reads next line from the message file. PARAM: buffer - buffer to read to maxsize - size of the buffer RETURN: 0 - if no more data ************************************************/

Source from the content-addressed store, hash-verified

257 0 - if no more data
258************************************************/
259int CUT_FileDataSource::ReadLine(LPSTR buffer, size_t maxsize) {
260 int rt;
261 LPSTR pos = NULL;
262
263 // Wrong parameter
264 if(buffer == NULL) return -1;
265
266 // If there is no more data or we can't find \r\n - try to read more
267 m_lpszBuffer[m_nLoadedDataSize] = 0;
268 if(m_nLoadedDataSize == 0 || (pos = strstr(m_lpszBuffer, "\r\n")) == NULL) {
269 if((rt = m_file.Read((m_lpszBuffer + m_nLoadedDataSize), m_nBufferSize - m_nLoadedDataSize - 1)) > 0)
270 // Increase the number of bytes read
271 m_nLoadedDataSize += rt;
272 }
273
274 // If we can find \r\n in the buffer - return one line
275 if(pos == NULL && m_nLoadedDataSize > 0)
276 pos = strstr(m_lpszBuffer, "\r\n");
277
278 if(pos != NULL && (unsigned int)(pos - m_lpszBuffer) < (maxsize-3)) {
279 // Copy line to buffer
280 pos[0] = 0;
281 strcpy(buffer, m_lpszBuffer);
282 strcat(buffer, "\r\n");
283
284 // Decrease the nuber of bytes read and
285 // remove the first line from the buffer
286 m_nLoadedDataSize -= (int)strlen(m_lpszBuffer) + 2;
287 memmove(m_lpszBuffer, pos+2, m_nLoadedDataSize);
288 m_lpszBuffer[m_nLoadedDataSize] = 0;
289 return (int)strlen(buffer);
290 }
291
292 // If \r\n not found - return what we have
293 else if(m_nLoadedDataSize > 0) {
294 strncpy(buffer, m_lpszBuffer, maxsize-1);
295 buffer[maxsize-1] = 0;
296
297 // Decrease the nuber of bytes read and
298 // remove the first line from the buffer
299 m_nLoadedDataSize -= (int)strlen(buffer);
300 memmove(m_lpszBuffer, m_lpszBuffer + strlen(buffer), m_nLoadedDataSize);
301 m_lpszBuffer[m_nLoadedDataSize] = 0;
302 return (int)strlen(buffer);
303 }
304
305 buffer[0] = 0;
306
307 return 0; // no more data to read
308}
309
310/***********************************************
311WriteLine

Callers

nothing calls this directly

Calls 1

ReadMethod · 0.45

Tested by

no test coverage detected