Back to index

lightning-sunbird  0.9+nobinonly
xpctest_string.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   John Bandhauer <jband@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 /* implement nsIXPCTestString for testing. */
00042 
00043 #include "xpctest_private.h"
00044 
00045 class xpcstringtest : public nsIXPCTestString
00046 {
00047 public:
00048     NS_DECL_ISUPPORTS
00049     NS_DECL_NSIXPCTESTSTRING
00050 
00051     xpcstringtest();
00052     virtual ~xpcstringtest();
00053 };
00054 
00055 xpcstringtest::xpcstringtest()
00056 {
00057     NS_ADDREF_THIS();
00058 }
00059 
00060 xpcstringtest::~xpcstringtest()
00061 {
00062 }
00063 
00064 NS_IMPL_ISUPPORTS1(xpcstringtest, nsIXPCTestString)
00065 
00066 /* string GetStringA (); */
00067 NS_IMETHODIMP
00068 xpcstringtest::GetStringA(char **_retval)
00069 {
00070     const char myResult[] = "result of xpcstringtest::GetStringA";
00071 
00072     if(!_retval)
00073         return NS_ERROR_NULL_POINTER;
00074 
00075     *_retval = (char*) nsMemory::Clone(myResult,
00076                                           sizeof(char)*(strlen(myResult)+1));
00077     return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00078 }
00079 
00080 /* void GetStringB (out string s); */
00081 NS_IMETHODIMP
00082 xpcstringtest::GetStringB(char **s)
00083 {
00084     const char myResult[] = "result of xpcstringtest::GetStringB";
00085 
00086     if(!s)
00087         return NS_ERROR_NULL_POINTER;
00088 
00089     *s = (char*) nsMemory::Clone(myResult,
00090                                     sizeof(char)*(strlen(myResult)+1));
00091 
00092     return *s ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00093 }
00094 
00095 
00096 /* void GetStringC ([shared, retval] out string s); */
00097 NS_IMETHODIMP
00098 xpcstringtest::GetStringC(const char **s)
00099 {
00100     static const char myResult[] = "result of xpcstringtest::GetStringC";
00101     if(!s)
00102         return NS_ERROR_NULL_POINTER;
00103     *s = myResult;
00104     return NS_OK;
00105 }
00106 
00107 // quick and dirty!!!
00108 static PRUnichar* GetTestWString(int* size)
00109 {
00110     static PRUnichar* sWStr;            
00111     static char str[] = "This is part of a long string... ";
00112     static const int slen = (sizeof(str)-1)/sizeof(char);
00113     static const int rep = 1;
00114     static const int space = (slen*rep*sizeof(PRUnichar))+sizeof(PRUnichar);
00115 
00116     if(!sWStr)
00117     {
00118         sWStr = (PRUnichar*) nsMemory::Alloc(space);
00119         if(sWStr)
00120         {
00121             PRUnichar* p = sWStr;
00122             for(int k = 0; k < rep; k++)
00123                 for (int i = 0; i < slen; i++)
00124                     *(p++) = (PRUnichar) str[i];
00125         *p = 0;        
00126         }
00127     }
00128     if(size)
00129         *size = space;
00130     return sWStr;
00131 }        
00132 
00133 /* void GetWStringCopied ([retval] out wstring s); */
00134 NS_IMETHODIMP xpcstringtest::GetWStringCopied(PRUnichar **s)
00135 {
00136     if(!s)
00137         return NS_ERROR_NULL_POINTER;
00138 
00139     int size;
00140     PRUnichar* str = GetTestWString(&size);
00141     if(!str)
00142         return NS_ERROR_OUT_OF_MEMORY;
00143 
00144     *s = (PRUnichar*) nsMemory::Clone(str, size);
00145     return *s ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00146 }        
00147 
00148 /* void GetWStringShared ([shared, retval] out wstring s); */
00149 NS_IMETHODIMP xpcstringtest::GetWStringShared(const PRUnichar **s)
00150 {
00151     if(!s)
00152         return NS_ERROR_NULL_POINTER;
00153     *s = GetTestWString(nsnull);
00154     return NS_OK;
00155 }        
00156 
00157 /***************************************************************************/
00158 
00159 // static
00160 NS_IMETHODIMP
00161 xpctest::ConstructStringTest(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00162 {
00163     nsresult rv;
00164     NS_ASSERTION(aOuter == nsnull, "no aggregation");
00165     xpcstringtest* obj = new xpcstringtest();
00166 
00167     if(obj)
00168     {
00169         rv = obj->QueryInterface(aIID, aResult);
00170         NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
00171         NS_RELEASE(obj);
00172     }
00173     else
00174     {
00175         *aResult = nsnull;
00176         rv = NS_ERROR_OUT_OF_MEMORY;
00177     }
00178 
00179     return rv;
00180 }
00181 /***************************************************************************/
00182 
00183 
00184 
00185