inline static void _catPath(UT_String& st, const char* st2)
{
	if (st.size() > 0)
	{
		if (st[st.size() - 1] != '/')
			st += '/';
	}
	else
		st += '/';

	st += st2;
}
Exemplo n.º 2
0
/*!
 * Assuming a string of standard abiword properties eg. "fred:nerk; table-width:1.0in; table-height:10.in"
 * Add aother propety string, updating previously defined properties with
 * values in the new string.
 */
void UT_String_addPropertyString(UT_String & sPropertyString, const UT_String & sNewProp)
{
	UT_sint32 iSize = static_cast<UT_sint32>(sNewProp.size());
	UT_sint32 iBase  =0;
	UT_String sProp;
	UT_String sVal;
	UT_String sSubStr;
	const char * szWork = NULL;
	const char * szLoc = NULL;
	while(iBase < iSize)
	{
		bool bBreakAtEnd = false;
		sSubStr = sNewProp.substr(iBase, iSize-iBase);
		szWork = sSubStr.c_str();
		szLoc = strstr(szWork,":");
		if(szLoc)
		{
			sProp = sNewProp.substr(iBase,szLoc - szWork);
		}
		else
		{
			break;
		}
		iBase += szLoc-szWork+1;
		sSubStr = sNewProp.substr(iBase, iSize-iBase);
		szWork = sSubStr.c_str();
		szLoc = strstr(szWork,";");
		if(szLoc)
		{
			sVal = sNewProp.substr(iBase,szLoc - szWork);
			iBase += szLoc-szWork+1;
		}
		else
		{
			sVal = sNewProp.substr(iBase,iSize-iBase);
			bBreakAtEnd = true;
		}
		if((sProp.size()>0) && (sVal.size() >0))
		{
			UT_String_setProperty(sPropertyString,sProp,sVal);
		}
		else
		{
			break;
		}
		if(bBreakAtEnd)
		{
			break;
		}
	}
}
Exemplo n.º 3
0
size_t UT_String_findRCh(const UT_String &st, char ch)
{
  for (size_t i = st.size() ; i > 0; i--)
    if (st[i] == ch)
      return i;
  return (size_t)-1;
}
Exemplo n.º 4
0
size_t UT_String_findCh(const UT_String &st, char ch)
{
  for (size_t i = 0 ; i < st.size(); i++)
    if (st[i] == ch)
      return i;
  return (size_t)-1;
}
Exemplo n.º 5
0
bool fp_FieldTOCListLabelRun::calculateValue(void)
{
    UT_UCSChar sz_ucs_FieldValue[FPFIELD_MAX_LENGTH + 1];
//
// First get owning TOC.
//
    UT_ASSERT(getLength() == 0);
    fl_TOCLayout * pTOCL = static_cast<fl_TOCLayout *>(getBlock()->myContainingLayout());
    UT_ASSERT(pTOCL->getContainerType() == FL_CONTAINER_TOC);
    UT_String str = pTOCL->getTOCListLabel(getBlock()).utf8_str();
    if(str.size() == 0)
    {
        sz_ucs_FieldValue[0] = 0;
        return _setValue(sz_ucs_FieldValue);
    }
    UT_sint32 i = 0;
    bool bStop = false;
    for(i=0; (i<FPFIELD_MAX_LENGTH) && !bStop; i++)
    {
        sz_ucs_FieldValue[i] = static_cast<UT_UCSChar>(str[i]);
        if(str[i] == 0)
        {
            bStop = true;
        }
    }
    return _setValue(sz_ucs_FieldValue);
}
Exemplo n.º 6
0
void UT_LocaleInfo::init (const UT_String & locale)
{
  if (locale.size () == 0)
    return;

  size_t dot    = 0;
  size_t hyphen = 0;

  // take both hyphen types into account
  hyphen = UT_String_findCh (locale, '_');
  if (hyphen == (size_t)-1)
    hyphen = UT_String_findCh (locale, '-');

  dot = UT_String_findCh (locale, '.');

  if (hyphen == (size_t)-1 && dot == (size_t)-1)
    {
      mLanguage = locale.c_str();
      return;
    }

  if (hyphen != (size_t)-1 && dot != (size_t)-1)
    {
      if (hyphen < dot)
	{
	  mLanguage  = locale.substr (0, hyphen).c_str();
	  mTerritory = locale.substr (hyphen+1, dot-(hyphen+1)).c_str();
	  mEncoding  = locale.substr (dot+1, locale.size ()-(dot+1)).c_str();
	}
      else
	{
	  mLanguage = locale.substr (0, dot).c_str();
	  mEncoding = locale.substr (dot+1, locale.size ()-(dot+1)).c_str();
	}
    }
  else if (dot != (size_t)-1)
    {
      mLanguage = locale.substr (0, dot).c_str();
      mEncoding = locale.substr (dot+1, locale.size ()-(dot+1)).c_str();
    }
  else if (hyphen != (size_t)-1)
    {
      mLanguage = locale.substr (0, hyphen).c_str();
      mEncoding = locale.substr (hyphen+1, locale.size ()-(hyphen+1)).c_str();
    }
}
Exemplo n.º 7
0
const char * AP_UnixPrefs::_getPrefsPathname(void) const
{
	/* return a pointer to a static buffer */
	static UT_String buf;

	if(!buf.empty())
	  return buf.c_str();

	const char * szDirectory = XAP_App::getApp()->getUserPrivateDirectory();
	const char * szFile = "AbiWord.Profile";

	buf = szDirectory;
	if (!buf.size() || szDirectory[buf.size()-1] != '/')
	  buf += "/";
	buf += szFile;

	return buf.c_str();
}
Exemplo n.º 8
0
/*!
 * Assuming a string of standard abiword properties eg. "fred:nerk; table-width:1.0in; table-height:10.in"
 * Add the property sProp with value sVal to the string of properties. If the property is already present, replace the 
 * old value with the new value.
 */
void UT_String_setProperty(UT_String & sPropertyString, const UT_String & sProp, const UT_String & sVal)
{
//
// Remove the old value if it exists and tack the new property on the end.
//
	UT_String_removeProperty(sPropertyString, sProp);
	if(sPropertyString.size() > 0)
	{
		sPropertyString += "; ";
	}
	sPropertyString += sProp;
	sPropertyString += ":";
	sPropertyString += sVal;
}
Exemplo n.º 9
0
void XAP_Dictionary::_outputUTF8(const UT_UCSChar * data, UT_uint32 length)
{
	UT_String buf;
	const UT_UCSChar * pData;

	for (pData = data; (pData<data+length); /**/)
	{
		if (*pData > 0x007f)
		{
			gchar outbuf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
			g_unichar_to_utf8(*pData++, outbuf);
			buf += outbuf;
		}
		else
		{
			buf += (char)*pData++;
		}
	}

	_writeBytes(reinterpret_cast<const UT_Byte *>(buf.c_str()),buf.size());
}
bool XAP_Win32AppImpl::openURL(const char * szURL)
{
	// NOTE: could get finer control over browser window via DDE 
	// NOTE: may need to fallback to WinExec for old NSCP versions

	UT_String sURL = szURL;

	// If this is a file:// URL, strip off file:// and make it backslashed
	if (sURL.substr(0, 7) == "file://")
	{
		sURL = sURL.substr(7, sURL.size() - 7);

		// View as WebPage likes to throw in an extra /\ just for fun, strip it off
		if (sURL.substr(0, 2) == "/\\")
			sURL = sURL.substr(2, sURL.size() - 2);

		if (sURL.substr(0, 1) == "/")
			sURL = sURL.substr(1, sURL.size() - 1);
		
		// Convert all forwardslashes to backslashes
		for (unsigned int i=0; i<sURL.length();i++)	
			if (sURL[i]=='/')	
                sURL[i]='\\';

		// Convert from longpath to 8.3 shortpath, in case of spaces in the path
		char* longpath = NULL;
		char* shortpath = NULL;
		longpath = new char[PATH_MAX];
		shortpath = new char[PATH_MAX];
		strcpy(longpath, sURL.c_str());
		DWORD retval = GetShortPathName(longpath, shortpath, PATH_MAX);
		if((retval == 0) || (retval > PATH_MAX))
		{
			UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
			DELETEP(longpath);
			DELETEP(shortpath);
			return false;
		}
		sURL = shortpath;
		DELETEP(longpath);
		DELETEP(shortpath);
	}

	// Query the registry for the default browser so we can directly invoke it
	UT_String sBrowser;
	HKEY hKey;
	unsigned long lType;
	DWORD dwSize;
	unsigned char* szValue = NULL;

	if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\command", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
	{
		if(RegQueryValueEx(hKey, NULL, NULL, &lType, NULL, &dwSize) == ERROR_SUCCESS)
		{
			szValue = new unsigned char[dwSize + 1];
			RegQueryValueEx(hKey, NULL, NULL, &lType, szValue, &dwSize);
			sBrowser = (char*) szValue;
			DELETEP(szValue);
		}
		RegCloseKey(hKey);
	}

	/* Now that we have sBrowser from the registry, we need to parse it out.
	 * If the first character is a double-quote, everything up to and including
	 * the next double-quote is the sBrowser command. Everything after the
	 * double-quote is appended to the parameters.
	 * If the first character is NOT a double-quote, we assume
	 * everything up to the first whitespace is the command and anything after
	 * is appended to the parameters.
	 */

	int iDelimiter;
	if (sBrowser.substr(0, 1) == "\"")
		iDelimiter = UT_String_findCh(sBrowser.substr(1, sBrowser.length()-1), '"')+2;
	else
		iDelimiter = UT_String_findCh(sBrowser.substr(0, sBrowser.length()), ' ');

	// Store params into a separate UT_String before we butcher sBrowser
	UT_String sParams = sBrowser.substr(iDelimiter+1, sBrowser.length()-iDelimiter+1);
	// Cut params off of sBrowser so all we're left with is the broweser path & executable
	sBrowser = sBrowser.substr(0, iDelimiter);

	// Check for a %1 passed in from the registry.  If we find it,
	// substitute our URL for %1.  Otherwise, just append sURL to params.
	const char *pdest = strstr(sParams.c_str(), "%1");
	if (pdest != NULL)
	{
		int i = pdest - sParams.c_str() + 1;
		sParams = sParams.substr(0, i-1) + sURL + sParams.substr(i+1, sParams.length()-i+1);
	}
	else
	{
		sParams = sParams + " " + sURL;
	}

	// Win95 doesn't like the Browser command to be quoted, so strip em off.
	if (sBrowser.substr(0, 1) == "\"")
		sBrowser = sBrowser.substr(1, sBrowser.length() - 1);
	if (sBrowser.substr(sBrowser.length()-1, 1) == "\"")
		sBrowser = sBrowser.substr(0, sBrowser.length() - 1);

	XAP_Frame * pFrame = XAP_App::getApp()->getLastFocussedFrame();
	UT_return_val_if_fail(pFrame, false);
	XAP_Win32FrameImpl *pFImp =  (XAP_Win32FrameImpl *) pFrame->getFrameImpl();
	UT_return_val_if_fail(pFImp, false);

	intptr_t res = (intptr_t) ShellExecuteA(pFImp->getTopLevelWindow() /*(HWND)*/,
								 "open", sBrowser.c_str(), sParams.c_str(), NULL, SW_SHOW );

	// TODO: localized error messages
	// added more specific error messages as documented in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp
	if (res <= 32)
	{
		UT_String errMsg;
		switch (res)
		{
			case ERROR_FILE_NOT_FOUND:
				{
					errMsg = "Error ("; 
					errMsg += UT_String_sprintf("%d", res);
					errMsg += ") displaying URL: The system cannot find the file specified.\n";
					errMsg += " [ ";  errMsg += sURL;  errMsg += " ] ";
					MessageBoxA(pFImp->getTopLevelWindow(), errMsg.c_str(), "Error displaying URL", MB_OK|MB_ICONEXCLAMATION);
				}
				break;

			case ERROR_PATH_NOT_FOUND:
				{
					errMsg = "Error ("; 
					errMsg += UT_String_sprintf("%d", res);
					errMsg += ") displaying URL: The system cannot find the path specified.\n";
					errMsg += " [ ";  errMsg += sURL;  errMsg += " ] ";
					MessageBoxA(pFImp->getTopLevelWindow(), errMsg.c_str(), "Error displaying URL", MB_OK|MB_ICONEXCLAMATION);
				}
				break;

			case SE_ERR_ACCESSDENIED:
				{
					errMsg = "Error ("; 
					errMsg += UT_String_sprintf("%d", res);
					errMsg += ") displaying URL: Access is denied.\n";
					errMsg += " [ ";  errMsg += sURL;  errMsg += " ] ";
					MessageBoxA(pFImp->getTopLevelWindow(), errMsg.c_str(), "Error displaying URL", MB_OK|MB_ICONEXCLAMATION);
				}
				break;

			default:
				{
					errMsg = "Error ("; 
					errMsg += UT_String_sprintf("%d", res);
					errMsg += ") displaying URL: \n";
					errMsg += " [ ";  errMsg += sURL;  errMsg += " ] ";
					MessageBoxA(pFImp->getTopLevelWindow(), errMsg.c_str(), "Error displaying URL", MB_OK|MB_ICONEXCLAMATION);
				}
				break;
		} /* switch (res) */
	} /* if (res <= 32) */

	return (res>32);
}
Exemplo n.º 11
0
#include "tf_test.h"
#include "ut_string_class.h"

#define TFSUITE "core.af.util.stringclass"

TFTEST_MAIN("UT_String")
{
	UT_String s("foobar");

	TFPASS(s.size() == 6);
	TFPASS(s.length() == 6);

	TFFAIL(s.empty());

	TFPASS(s == "foobar");
	TFPASS(s == UT_String("foobar"));

	s += "baz";
	TFPASS(s.size() == 9);
	TFPASS(s == "foobarbaz");

	s += UT_String("42");
	TFPASS(s.size() == 11);
	TFPASS(s == "foobarbaz42");

	s += '!';
	TFPASS(s.size() == 12);
	TFPASS(s == "foobarbaz42!");

	TFPASS(s[3] == 'b');
Exemplo n.º 12
0
/*!
* Try loading a string-set.
* \param szStringSet Language id, e.g. de_AT
* \param pDefaultStringSet String set to be used for untranslated strings.
* \return AP_DiskStringSet * on success, NULL if not found
*/
AP_DiskStringSet * 
AP_UnixApp::loadStringsFromDisk(const char 			* szStringSet, 
								AP_BuiltinStringSet * pDefaultStringSet)
{
	UT_ASSERT(pDefaultStringSet);

	const char * szDirectory = NULL;
	getPrefsValueDirectory(true,
			       static_cast<const gchar*>(AP_PREF_KEY_StringSetDirectory),
			       static_cast<const gchar**>(&szDirectory));
	UT_return_val_if_fail((szDirectory) && (*szDirectory), NULL);

	UT_String szPathVariant[4];
	char * p_strbuf = strdup("");
	char * p_modifier = NULL;
	int  cur_id = 0;
	bool three_letters = false; // some have 3!

	if (szStringSet) {
		FREEP(p_strbuf);
		p_strbuf = strdup(szStringSet);
		p_modifier = strrchr(p_strbuf,'@');
		
		char t = szStringSet[2];
		if (t && t!='-' && t!='@' && t!='_') three_letters = true;
	}

	if (p_modifier) {
		// [email protected]
		szPathVariant[cur_id] = szDirectory;
		if (szDirectory[strlen(szDirectory)-1]!='/')
			szPathVariant[cur_id] += "/";
		szPathVariant[cur_id] += p_strbuf;
		szPathVariant[cur_id] += ".strings";

		cur_id++;

		// [email protected]
		if (szStringSet && strlen(szStringSet) > 2) {
			szPathVariant[cur_id] = szDirectory;
			if (szDirectory[strlen(szDirectory)-1]!='/')
				szPathVariant[cur_id] += "/";
			szPathVariant[cur_id] += p_strbuf[0];
			szPathVariant[cur_id] += p_strbuf[1];
			if (three_letters)
				szPathVariant[cur_id] += p_strbuf[2];
			szPathVariant[cur_id] += p_modifier;
			szPathVariant[cur_id] += ".strings";
		}

		cur_id++;

		// trim modifier part
		*p_modifier = 0;
	}
	
	// fo_BA.strings
	UT_String szPath = szDirectory;
	if (szDirectory[szPath.size()-1]!='/')
		szPath += "/";
	szPath += p_strbuf;
	szPath += ".strings";

	// fo.strings
	UT_String szFallbackPath;
	if (szStringSet && strlen(szStringSet) > 2) {
		szFallbackPath = szDirectory;
		if (szDirectory[szFallbackPath.size()-1]!='/')
			szFallbackPath += "/";
		szFallbackPath += p_strbuf[0];
		szFallbackPath += p_strbuf[1];
		if (three_letters)
			szFallbackPath += p_strbuf[2];
		szFallbackPath += ".strings";
	}

	AP_DiskStringSet * pDiskStringSet = new AP_DiskStringSet(this);

	FREEP(p_strbuf);

	// trying to load specific strings first
	for (int i=0; i<cur_id; i++) {
		if (pDiskStringSet->loadStringsFromDisk(szPathVariant[i].c_str())) {
			pDiskStringSet->setFallbackStringSet(pDefaultStringSet);
			UT_DEBUGMSG(("Using [v] StringSet [%s]\n",szPathVariant[i].c_str()));
			return pDiskStringSet;
		}
	}

	// then generic ones
	if (pDiskStringSet->loadStringsFromDisk(szPath.c_str()))
	{
		pDiskStringSet->setFallbackStringSet(pDefaultStringSet);
		UT_DEBUGMSG(("Using StringSet [%s]\n",szPath.c_str()));
		return pDiskStringSet;
	}
	else if (szFallbackPath.size() && pDiskStringSet->loadStringsFromDisk(szFallbackPath.c_str())) 
	{
		pDiskStringSet->setFallbackStringSet(pDefaultStringSet);
		UT_DEBUGMSG(("Using StringSet [%s]\n",szFallbackPath.c_str()));
		return pDiskStringSet;
	}
	else
	{
		DELETEP(pDiskStringSet);
		UT_DEBUGMSG(("Unable to load StringSet [%s] -- using builtin strings instead.\n",szPath.c_str()));
		return NULL;
	}
}
Exemplo n.º 13
0
bool ev_EditMethod_invoke (const EV_EditMethod * pEM, const UT_String & data)
{
  EV_EditMethodCallData callData ( data.c_str(), static_cast<UT_uint32>(data.size()) ) ;
  return ev_EditMethod_invoke ( pEM, &callData ) ;
}
Exemplo n.º 14
0
bool operator==(const UT_String& s1, const UT_String& s2)
{
        if (s1.size() != s2.size()) return false;
	return strcmp(s1.c_str(), s2.c_str()) == 0;
}
Exemplo n.º 15
0
/*!
 * Assuming a string of standard abiword properties eg. "fred:nerk; table-width:1.0in; table-height:10.in"
 * Remove the property sProp and it's value from the string of properties. 
 */
void UT_String_removeProperty(UT_String & sPropertyString, const UT_String & sProp)
{
	UT_String sWork ( sProp );
	sWork += ":";
	const char * szWork = sWork.c_str();
	const char * szProps = sPropertyString.c_str();
	const char * szLoc = strstr(szProps,szWork);
	if(szLoc == NULL)
	{
//
// Not here, do nothing
		return ;
	}
//
// Found it, Get left part.
//
	UT_sint32 locLeft = static_cast<UT_sint32>(reinterpret_cast<size_t>(szLoc) - reinterpret_cast<size_t>(szProps));
	UT_String sLeft;
	if(locLeft == 0)
	{
		sLeft.clear();
	}
	else
	{
		sLeft = sPropertyString.substr(0,locLeft);
	}
	locLeft = static_cast<UT_sint32>(sLeft.size());
	if(locLeft > 0)
	{
//
// If this element is the last item in the properties there is no "; ".
//
// Remove trailing ';' and ' '
//
		locLeft--;
		while(locLeft >= 0 && (sLeft[locLeft] == ';' || sLeft[locLeft] == ' '))
		{
			locLeft--;
		}
	}
	UT_String sNew;
	if(locLeft > 0)
	{
		sNew = sLeft.substr(0,locLeft+1);
	}
	else
	{
		sNew.clear();
	}
//
// Look for ";" to get right part
//
	const char * szDelim = strchr(szLoc,';');
	if(szDelim == NULL)
	{
//
// No properties after this, just assign and return
//
		sPropertyString = sNew;
	}
	else
	{
//
// Just slice off the properties and tack them onto the pre-existing sNew
//
		while(*szDelim == ';' || *szDelim == ' ')
		{
			szDelim++;
		}
		UT_sint32 offset = static_cast<UT_sint32>(reinterpret_cast<size_t>(szDelim) - reinterpret_cast<size_t>(szProps));
		UT_sint32 iLen = sPropertyString.size() - offset;
		if(sNew.size() > 0)
		{
			sNew += "; ";
		}
		sNew += sPropertyString.substr(offset,iLen);
		sPropertyString = sNew;
	}
}
Exemplo n.º 16
0
static bool _getTranslationCode (FV_View * pView, UT_String & langCode)
{
  XAP_Frame * pFrame = static_cast<XAP_Frame *> (pView->getParentData());
  UT_return_val_if_fail(pFrame,false);
  
  bool bRet = false;

  pFrame->raise();
  
  XAP_Dialog_Id id = XAP_DIALOG_ID_LANGUAGE;
  
  XAP_DialogFactory * pDialogFactory
    = static_cast<XAP_DialogFactory *>(pFrame->getDialogFactory());

  XAP_Dialog_Language * pDialog
    = static_cast<XAP_Dialog_Language *>(pDialogFactory->requestDialog(id));
  UT_return_val_if_fail(pDialog,false);

  UT_String code ("en-US");
  
  const gchar ** props_in = NULL;
  if (pView->getCharFormat(&props_in))
    {
      const gchar * xml_code = UT_getAttribute("lang", props_in);
      if ( xml_code )
	{
	  code = xml_code ;
	  if ( code.size() >= 2 )
	    {
	      code = code.substr (0, 2);
	      code += '_';
	    }
	}
      
      pDialog->setLanguageProperty(UT_getAttribute("lang", props_in));
      FREEP(props_in);
    }
  
  // run the dialog  
  pDialog->runModal(pFrame);
  
  // extract what they did  
  bool bOK = (pDialog->getAnswer() == XAP_Dialog_Language::a_OK);
  
  if (bOK)
    {
      const gchar * s;
      if (pDialog->getChangedLangProperty(&s))
	{
	  UT_String changedLang = s;
	  if (changedLang.size() >= 2)
	    {
	      code += changedLang.substr(0, 2);
	      langCode = code;
	      bRet = true;
	    }
	}
    }

  pDialogFactory->releaseDialog(pDialog);

  return bRet;
}