Back to index

lightning-sunbird  0.9+nobinonly
nsSample.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00046 #include <stdio.h>
00047 
00048 #include "nsSample.h"
00049 #include "nsMemory.h"
00050 
00051 #include "nsEmbedString.h"
00053 
00054 nsSampleImpl::nsSampleImpl() : mValue(nsnull)
00055 {
00056     mValue = (char*)nsMemory::Clone("initial value", 14);
00057 }
00058 
00059 nsSampleImpl::~nsSampleImpl()
00060 {
00061     if (mValue)
00062         nsMemory::Free(mValue);
00063 }
00064 
00079 NS_IMPL_ISUPPORTS1_CI(nsSampleImpl, nsISample)
00080 
00081 
00086 NS_IMETHODIMP
00087 nsSampleImpl::GetValue(char** aValue)
00088 {
00089     NS_PRECONDITION(aValue != nsnull, "null ptr");
00090     if (! aValue)
00091         return NS_ERROR_NULL_POINTER;
00092 
00093     if (mValue) {
00107         *aValue = (char*) nsMemory::Clone(mValue, strlen(mValue) + 1);
00108         if (! *aValue)
00109             return NS_ERROR_NULL_POINTER;
00110     }
00111     else {
00112         *aValue = nsnull;
00113     }
00114     return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP
00118 nsSampleImpl::SetValue(const char* aValue)
00119 {
00120     NS_PRECONDITION(aValue != nsnull, "null ptr");
00121     if (! aValue)
00122         return NS_ERROR_NULL_POINTER;
00123 
00124     if (mValue) {
00125         nsMemory::Free(mValue);
00126     }
00127 
00134     mValue = (char*) nsMemory::Clone(aValue, strlen(aValue) + 1);
00135     return NS_OK;
00136 }
00137 
00138 NS_IMETHODIMP
00139 nsSampleImpl::Poke(const char* aValue)
00140 {
00141     return SetValue((char*) aValue);
00142 }
00143 
00144 
00145 static void GetStringValue(nsACString& aValue)
00146 {
00147     NS_CStringSetData(aValue, "GetValue");
00148 }
00149 
00150 NS_IMETHODIMP
00151 nsSampleImpl::WriteValue(const char* aPrefix)
00152 {
00153     NS_PRECONDITION(aPrefix != nsnull, "null ptr");
00154     if (! aPrefix)
00155         return NS_ERROR_NULL_POINTER;
00156 
00157     printf("%s %s\n", aPrefix, mValue);
00158 
00159     // This next part illustrates the nsEmbedString:
00160     nsEmbedString foopy;
00161     foopy.Append(PRUnichar('f'));
00162     foopy.Append(PRUnichar('o'));
00163     foopy.Append(PRUnichar('o'));
00164     foopy.Append(PRUnichar('p'));
00165     foopy.Append(PRUnichar('y'));
00166     
00167     const PRUnichar* f = foopy.get();
00168     PRUint32 l = foopy.Length();
00169     printf("%c%c%c%c%c %d\n", char(f[0]), char(f[1]), char(f[2]), char(f[3]), char(f[4]), l);
00170     
00171     nsEmbedCString foopy2;
00172     GetStringValue(foopy2);
00173 
00174     //foopy2.AppendLiteral("foopy");
00175     const char* f2 = foopy2.get();
00176     PRUint32 l2 = foopy2.Length();
00177 
00178     printf("%s %d\n", f2, l2);
00179 
00180     return NS_OK;
00181 }