Backend/Database/GTable.cpp
/*!
 * @brief GTable file import
 * @details imports GTable data from a file
 * @param fname the file path to the desired data
 */
@brief GTable file import
@details imports GTable data from a file
@param fname the file path to the desired data
@brief GTable file import @details imports GTable data from a file @param fname the file path to the desired data
void GTable::importFromFile(const GString& fname)
{
	if (fname.length() == 0)
		return;
	FILE* fd = fopen(fname.c_str(), "r");
	printf("[CSV] %c%s\n", (fd != NULL) ? '+' : '-', fname.c_str());
	if (!fd)
		return;
	fseek(fd, 0, SEEK_END);
	int64_t fSize = ftell(fd);
	fseek(fd, 0, SEEK_SET);
	int rowCounter = 0;
	int MAX_LINE_SIZE = 256;
	int linesRead = 0; 
	char buffer[MAX_LINE_SIZE];
	do
	{
		shmea::GList newRow;
		bzero(buffer, MAX_LINE_SIZE);
		char readBuffer[MAX_LINE_SIZE];
		if (fgets(readBuffer, sizeof(readBuffer), fd) != 0)
			linesRead = sscanf(readBuffer, "%[^\n]s", buffer);
		else
			linesRead = 0;
		if (linesRead <= 0)
			break;
		int colCounter = 0;
		bool lastCol = false;
		GString line(buffer);
		int breakPoint = line.cfind(delimiter);
		while (breakPoint != -1)
		{
			GString word = line.substr(0, breakPoint);
			if (!lastCol)
				line = line.substr(breakPoint + 1);
			if (rowCounter == 0)
				header.push_back(word);
			else
			{
				GType newCell = GString::Typify(word.c_str(), word.length());
				newRow.addGType(newCell);
			}
			breakPoint = line.cfind(delimiter);
			++colCounter;
			if ((breakPoint == -1) && (line.length() > 0) && (!lastCol))
			{
				breakPoint = line.length();
				lastCol = true;
			}
		}
		if (rowCounter > 0)
			addRow(newRow);
		++rowCounter;
	} while ((linesRead > 0) && (ftell(fd) < fSize));
	fclose(fd);
}
void GTable :: importFromFile ( const GString & fname ) { if ( fname . length ( ) == 0 ) return ; FILE * fd = fopen ( fname . c_str ( ) , " " ) ; printf ( " \n " , ( fd != NULL ) ? ' ' : ' ' , fname . c_str ( ) ) ; if ( ! fd ) return ; fseek ( fd , 0 , SEEK_END ) ; int64_t fSize = ftell ( fd ) ; fseek ( fd , 0 , SEEK_SET ) ; int rowCounter = 0 ; int MAX_LINE_SIZE = 256 ; int linesRead = 0 ; char buffer [ MAX_LINE_SIZE ] ; do { shmea :: GList newRow ; bzero ( buffer , MAX_LINE_SIZE ) ; char readBuffer [ MAX_LINE_SIZE ] ; if ( fgets ( readBuffer , sizeof ( readBuffer ) , fd ) != 0 ) linesRead = sscanf ( readBuffer , " \n " , buffer ) ; else linesRead = 0 ; if ( linesRead <= 0 ) break ; int colCounter = 0 ; bool lastCol = false ; GString line ( buffer ) ; int breakPoint = line . cfind ( delimiter ) ; while ( breakPoint != -1 ) { GString word = line . substr ( 0 , breakPoint ) ; if ( ! lastCol ) line = line . substr ( breakPoint + 1 ) ; if ( rowCounter == 0 ) header . push_back ( word ) ; else { GType newCell = GString :: Typify ( word . c_str ( ) , word . length ( ) ) ; newRow . addGType ( newCell ) ; } breakPoint = line . cfind ( delimiter ) ; ++ colCounter ; if ( ( breakPoint == -1 ) && ( line . length ( ) > 0 ) && ( ! lastCol ) ) { breakPoint = line . length ( ) ; lastCol = true ; } } if ( rowCounter > 0 ) addRow ( newRow ) ; ++ rowCounter ; } while ( ( linesRead > 0 ) && ( ftell ( fd ) < fSize ) ) ; fclose ( fd ) ; }
@brief GTable file import
 @details imports GTable data from a file
 @param fname the file path to the desired data
@brief GTable file import @details imports GTable data from a file @param fname the file path to the desired data
// get the file size // Allocate a buffer // Are the lines read, not how many lines read // get the current line // EOF or error // Read each column // get the cell // add the col to the row // for the next column // last column? // add the row to the object // EOF