Skip to content

Commit 041a6fd

Browse files
committed
[Vtrutil]: adding getline with comment igorance (compatible with both linux and windows)
Signed-off-by: Seyed Alireza Damghani <[email protected]>
1 parent c0de7ce commit 041a6fd

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

libs/libvtrutil/src/vtr_util.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,67 @@ char* fgets(char* buf, int max_size, FILE* fp) {
374374
return nullptr;
375375
}
376376

377+
/**
378+
* @brief to get an arbitrary long input line and cut off any
379+
* comment part
380+
*
381+
* the getline function is exaly like the __get_delim function
382+
* in GNU with '\n' delimiter. As a result, to make the function
383+
* behaviour identical for Windows (\r\n) and Linux (\n) compiler
384+
* macros for checking operating systems have been used.
385+
*
386+
* @note user need to take care of the given pointer,
387+
* which will be dynamically allocated by getdelim
388+
*/
389+
char* getline(char*& _lineptr, FILE* _stream) {
390+
int i;
391+
int ch;
392+
size_t _n = 0;
393+
ssize_t nread;
394+
395+
#if defined(__unix__)
396+
nread = getdelim(&_lineptr, &_n, '\n', _stream);
397+
#elif defined(_WIN32)
398+
# define __WIN_NLTK "\r\n"
399+
nread = getdelim(&_lineptr, &_n, __WIN_NLTK, _stream);
400+
#endif
401+
402+
if (nread == -1) {
403+
int errsv = errno;
404+
std::string error_msg;
405+
406+
if (errsv == EINVAL)
407+
error_msg = string_fmt("[%s] Bad arguments (_lineptr is NULL, or _stream is not valid).", strerror(errsv));
408+
else if (errsv == ENOMEM)
409+
error_msg = string_fmt("[%s] Allocation or reallocation of the line buffer failed.", strerror(errsv));
410+
else
411+
/* end of file so it will return null */
412+
return nullptr;
413+
414+
/* getline was unsuccessful, so error */
415+
throw VtrError(string_fmt("Error -- %s\n",
416+
error_msg),
417+
__FILE__, __LINE__);
418+
return nullptr;
419+
}
420+
421+
cont = 0; /* line continued? */
422+
file_line_number++; /* global variable */
423+
424+
for (i = 0; i < nread; i++) { /* Keep going until the line finishes */
425+
426+
ch = _lineptr[i];
427+
428+
if (ch == '#') { /* comment */
429+
_lineptr[i] = '\0';
430+
/* skip the rest of the line */
431+
break;
432+
}
433+
}
434+
435+
return (_lineptr);
436+
}
437+
377438
///@brief Returns line number of last opened and read file
378439
int get_file_line_number_of_last_opened_file() {
379440
return file_line_number;

libs/libvtrutil/src/vtr_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ char* strtok(char* ptr, const char* tokens, FILE* fp, char* buf);
5757
FILE* fopen(const char* fname, const char* flag);
5858
int fclose(FILE* f);
5959
char* fgets(char* buf, int max_size, FILE* fp);
60+
char* getline(char*& _lineptr, FILE* _stream);
6061

6162
int atoi(const std::string& value);
6263
unsigned atou(const std::string& value);

0 commit comments

Comments
 (0)