Example #1
0
CString CDateTime::toString(const CString& format) const
{
    if (isNull())
    {
        return CString();
    }

    CString str;
    bool special = false;
    const unsigned int len = format.getSize();

    for (unsigned int i = 0 ; i < len ; ++i)
    {
        if (format[i] == CChar('%'))
        {
            special = true;
        }
        else if (special)
        {
            switch (format[i].toLatin1())
            {
                default:
                    str += format[i];
                    break;

                case 'a':
                    str += (m_hour < 12 ? "am" : "pm");
                    break;

                case 'A':
                    str += (m_hour < 12 ? "AM" : "PM");
                    break;

                case 'd':

                    if (m_day < 10)
                        str += '0';

                    str += CString::fromNumber(m_day);
                    break;

                case 'D':
                    str += shortDayName(dayOfWeek(m_year, m_month, m_day));
                    break;

                case 'F':
                    str += longMonthName(m_month);
                    break;

                case 'g':

                    if (m_hour == 0)
                    {
                        str += "12";
                    }
                    else if (m_hour < 13)
                    {
                        str += CString::fromNumber(m_hour);
                    }
                    else
                    {
                        str += CString::fromNumber(m_hour - 12);
                    }

                    break;

                case 'G':
                    str += CString::fromNumber(m_hour);
                    break;

                case 'h':

                    if (m_hour == 0)
                    {
                        str += "12";
                    }
                    else if (m_hour < 13)
                    {
                        if (m_hour < 10)
                        {
                            str += '0';
                        }

                        str += CString::fromNumber(m_hour);
                    }
                    else
                    {
                        if (m_hour < 22)
                        {
                            str += '0';
                        }

                        str += CString::fromNumber(m_hour - 12);
                    }

                    break;

                case 'H':

                    if (m_hour < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_hour);

                    break;

                case 'i':

                    if (m_minute < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_minute);

                    break;

                case 'j':
                    str += CString::fromNumber(m_day);
                    break;

                case 'l':
                    str += longDayName(dayOfWeek(m_year, m_month, m_day));
                    break;

                case 'L':
                    str += (isLeapYear(m_year) ? '1' : '0');
                    break;

                case 'm':

                    if (m_month < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_month);

                    break;

                case 'M':
                    str += shortMonthName(m_month);
                    break;

                case 'n':
                    str += CString::fromNumber(m_month);
                    break;

                case 'N':
                    str += dayOfWeek(m_year, m_month, m_day);
                    break;

                case 's':

                    if (m_second < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_second);

                    break;

                case 't':
                    str += CString::fromNumber(daysInMonth(m_month, m_year));
                    break;

                case 'U':
                    str += CString::fromNumber(timestamp(m_year, m_month, m_day, m_hour, m_minute, m_second));
                    break;

                case 'W':
                    str += CString::fromNumber(weekOfYear(m_year, m_month, m_day));
                    break;

                case 'y':

                    if (m_year < 0)
                    {
                        str += CString::fromNumber(m_year);
                    }
                    else
                    {
                        unsigned int tmp = m_year % 100;

                        if (tmp < 10)
                        {
                            str += "0";
                        }

                        str += CString::fromNumber(tmp);
                    }

                    break;

                case 'Y':

                    if (m_year < 0)
                    {
                        str += CString::fromNumber(m_year);
                    }
                    else
                    {
                        unsigned int tmp = m_year % 10000;

                        if (tmp < 10)
                            str += "000";
                        if (tmp < 100)
                            str += "00";
                        if (tmp < 1000)
                            str += "000";

                        str += CString::fromNumber(tmp);
                    }

                    break;

                case 'z':
                    str += dayOfYear(m_year, m_month, m_day);
                    break;
            }

            special = false;
        }
        else
        {
            str += format[i];
        }
    }

    return str;
}
void CSheetId::loadSheetId ()
{
	H_AUTO(CSheetIdInit);
	//nldebug("Loading sheet_id.bin");

	// Open the sheet id to sheet file name association
	CIFile file;
	std::string path = CPath::lookup("sheet_id.bin", false, false);
	if(!path.empty() && file.open(path))
	{
		// clear entries
		_FileExtensions.clear ();
		_SheetIdToName.clear ();
		_SheetNameToId.clear ();

		// reserve space for the vector of file extensions
		_FileExtensions.resize(1 << (NL_SHEET_ID_TYPE_BITS));

		// Get the map from the file
		map<uint32,string> tempMap;
		contReset(tempMap);
		file.serialCont(tempMap);
		file.close();

		if (_RemoveUnknownSheet)
		{
			uint32 removednbfiles = 0;
			uint32 nbfiles = (uint32)tempMap.size();

			// now we remove all files that not available
			map<uint32,string>::iterator itStr2;
			for( itStr2 = tempMap.begin(); itStr2 != tempMap.end(); )
			{
				if (CPath::exists ((*itStr2).second))
				{
					++itStr2;
				}
				else
				{
					map<uint32,string>::iterator olditStr = itStr2;
					//nldebug ("Removing file '%s' from CSheetId because the file not exists", (*olditStr).second.c_str ());
					itStr2++;
					tempMap.erase (olditStr);
					removednbfiles++;
				}
			}
			nlinfo ("SHEETID: Removed %d files on %d from CSheetId because these files doesn't exists", removednbfiles, nbfiles);
		}

		// Convert the map to one big string and 1 static map (id to name)
		{
			// Get the number and size of all strings
			vector<CChar> tempVec; // Used to initialise the first map
			uint32 nNb = 0;
			uint32 nSize = 0;
			map<uint32,string>::const_iterator it = tempMap.begin();
			while (it != tempMap.end())
			{
				nSize += (uint32)it->second.size()+1;
				nNb++;
				it++;
			}

			// Make the big string (composed of all strings) and a vector referencing each string
			tempVec.resize(nNb);
			_AllStrings.Ptr = new char[nSize];
			it = tempMap.begin();
			nSize = 0;
			nNb = 0;
			while (it != tempMap.end())
			{
				tempVec[nNb].Ptr = _AllStrings.Ptr+nSize;
				strcpy(_AllStrings.Ptr+nSize, it->second.c_str());
				toLower(_AllStrings.Ptr+nSize);
				nSize += (uint32)it->second.size()+1;
				nNb++;
				it++;
			}

			// Finally build the static map (id to name)
			_SheetIdToName.reserve(tempVec.size());
			it = tempMap.begin();
			nNb = 0;
			while (it != tempMap.end())
			{
				_SheetIdToName.add(pair<uint32, CChar>(it->first, CChar(tempVec[nNb])));

				nNb++;
				it++;
			}

			// The vector of all small string is not needed anymore we have all the info in
			// the static map and with the pointer AllStrings referencing the beginning.
		}

		// Build the invert map (Name to Id) & file extension vector
		{
			uint32 nSize = (uint32)_SheetIdToName.size();
			_SheetNameToId.reserve(nSize);
			CStaticMap<uint32,CChar>::iterator itStr;
			for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
			{
				// add entry to the inverse map
				_SheetNameToId.add( make_pair((*itStr).second, (*itStr).first) );

				// work out the type value for this entry in the map
				TSheetId sheetId;
				sheetId.Id=(*itStr).first;
				uint32 type = sheetId.IdInfos.Type;

				// check whether we need to add an entry to the file extensions vector
				if (_FileExtensions[type].empty())
				{
					// find the file extension part of the given file name
					_FileExtensions[type] = toLower(CFile::getExtension((*itStr).second.Ptr));
				}
				nSize--;
			}
			_SheetNameToId.endAdd();
		}
	}
	else
	{
		nlerror("<CSheetId::init> Can't open the file sheet_id.bin");
	}
	nldebug("Finished loading sheet_id.bin: %u entries read",_SheetIdToName.size());
}