随笔 - 171  文章 - 257  trackbacks - 0
<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(33)

随笔分类(225)

随笔档案(171)

相册

技术

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 441312
  • 排名 - 48

最新随笔

最新评论

阅读排行榜

评论排行榜

//2007.12.22 修正


//CFile.h

#ifndef __C_FILE_H__
#define __C_FILE_H__

#include <f32file.h>
#include <badesca.h>



class CFile:public CBase
{

public:
  static CFile *NewL(const TDesC &p_fileName);
  static CFile *NewLC(const TDesC &p_fileName);
  CFile(const TDesC& p_fileName);
  ~CFile();

public:
  void ConstructL(TUint p_shareMode);
  TInt FPuts(const TDesC8 &aDes);
  TInt FGets(TDes8 &aDes) const;
  TInt FSeek(TSeek aMode, TInt &aPos) const;
  TInt FSetSize(TInt aSize);
  TInt FileSize() const;
  void DostroyL();
 
public:
  static void GetDirFiles(const TDesC &path, CDesCArray& files);
  static void LogToFile(const TDesC& filename,TDesC8&  pdu);
  //static void LogToFile(const TDesC& filename,TDesC&  pdu);
  static void GetLinesFromFile(const TDesC& filename,CDesCArray&  lines);
  static void GetLinesFromFile(const TDesC& filename,CDesC8Array&  lines);
  static bool MakeCbmDir(const TDesC& aPath);
protected:
  TBuf<128> mFileName;
  RFs fs;
  RFile file;
  TInt mFileSize;
 
};


#endif //__C_FILE_H__




//CFile.cpp

#include "CFile.h"

#include <bautils.h>
#include <utf.h>

_LIT8(KCRLF, "\r\n");

CFile *CFile::NewL(const TDesC &p_fileName)
{
  CFile *self = CFile::NewLC(p_fileName);
  CleanupStack::Pop(self);
  return self;
}


CFile *CFile::NewLC(const TDesC &p_fileName)
{
  CFile *self = new(ELeave) CFile(p_fileName);
  CleanupStack::PushL(self);
  return self;
}


CFile::CFile(const TDesC &p_fileName)
{
  mFileName.Copy(p_fileName);
  mFileSize = 0;
}

CFile::~CFile()
{
   
}

void CFile::ConstructL(TUint p_shareMode)
{
  User::LeaveIfError(fs.Connect() );
  TInt err = file.Open( fs, mFileName, p_shareMode);
 
  if(err==KErrNotFound) // file does not exist - create it
    err=file.Create(fs, mFileName, p_shareMode);
 
  file.Size(mFileSize);
}



TInt CFile::FileSize() const
{
    return mFileSize;
}



TInt CFile::FPuts(const TDesC8 &aDes)
{
  return file.Write(aDes);
}




TInt CFile::FGets(TDes8 &aDes) const
{
  return file.Read(aDes);
}


TInt CFile::FSeek(TSeek aMode, TInt &aPos) const
{
  return file.Seek(aMode, aPos);
}

TInt CFile::FSetSize(TInt aSize)
{
  return file.SetSize(aSize);
}

void CFile::DostroyL()
{
  file.Close();
  fs.Close();
}



bool  CFile::MakeCbmDir(const TDesC& aPath){
    RFs fs;
    User::LeaveIfError(fs.Connect() );
 
    if( BaflUtils::FolderExists(fs,aPath)  ){
        fs.Close();
        return true;
    }
    if(KErrNone==fs.MkDirAll(aPath) ){
        fs.Close();
        return  true;
    }
   
    fs.Close();
    return false;
}


void CFile::GetDirFiles(const TDesC& path,CDesCArray&  files)
{
  RFs fs;
  User::LeaveIfError(fs.Connect() );

  if( BaflUtils::FolderExists(fs,path)  ){
    CDir* dir=NULL;
    User::LeaveIfError( fs.GetDir(path, KEntryAttNormal|KEntryAttMatchMask, ESortByName, dir) );
    TInt ii=dir->Count();
    for(TInt i=0;i<ii;i++){
      TEntry aEntry=(*dir)[i];
      TBuf<256> filePath(path);
      filePath.Append(aEntry.iName);
      files.AppendL(filePath);
    }
  }
  fs.Close();
}


void CFile::LogToFile(const TDesC& filename,TDesC8&  pdu)
{
  //_LIT(name, "C:\\log.txt");
  //TBuf<32> filename(name);
  RFs fs;
  RFile file;

  User::LeaveIfError(fs.Connect() );
  //CleanupClosePushL(fs); 
  //CleanupClosePushL(file); 

  TUint shareMode=EFileRead | EFileWrite ;
  TInt err= file.Open( fs, filename,shareMode ) ;

  if(err==KErrNotFound) // file does not exist - create it
    err=file.Create(fs,filename,shareMode);
  TInt pos;
  file.Seek(ESeekEnd,pos);

  if(err!=KErrNotFound)
    file.Write(pos,_L8("\r\n"));

  file.Seek(ESeekEnd,pos);
  file.Write(pos,pdu);
  //CleanupStack::PopAndDestroy(2,&fs);

  file.Close();
  fs.Close();
}



void CFile::GetLinesFromFile(const TDesC& filename,CDesCArray&  lines){
  RFs fs;
  User::LeaveIfError(fs.Connect() );
  if( BaflUtils::FileExists(fs,filename)  ){
    //open file
    RFile file;
    TUint shareMode=EFileRead | EFileWrite ;
    file.Open( fs, filename,shareMode ) ;

    TInt fileSize=0;
    file.Size(fileSize);  //取得文件大小

    CnvUtfConverter *cutf=new (ELeave) CnvUtfConverter;

    HBufC8 *bufc = HBufC8::NewL(fileSize+1);

    file.Read( (TDes8&) (bufc->Des()) ) ;

    //LogToFile(_L("c:\\cbm\\rsadebug.txt"), bufc->Des());//

    TInt res;
    TPtrC8 iCursor(bufc->Des());
    TBuf<256>  buf16;
       
    //TBuf8<2> bufPage;
    //bufPage.Format(_L8("%02d"), res);
    //LogToFile(_L("c:\\cbm\\rsadebug.txt"), bufPage);

    while( ( res = iCursor.FindF(KCRLF) ) >= 0 ) {
      res = iCursor.FindF(KCRLF);
      TPtrC8 result = iCursor.Left(res);
      iCursor.Set(iCursor.Right(iCursor.Length() - (res +2)));
           
      buf16.Zero();
      buf16=cutf->ConvertToUnicodeFromUtf8L(result)->Des();
           
      lines.AppendL(buf16);
    }

    if(iCursor.Size()>0) {
      buf16.Zero();
      buf16=cutf->ConvertToUnicodeFromUtf8L(iCursor)->Des();
      lines.AppendL(buf16);
    }

    delete cutf;
    delete  bufc;
    file.Close();
  }
  fs.Close();
}


void CFile::GetLinesFromFile(const TDesC& filename,CDesC8Array&  lines){
  RFs fs;
  User::LeaveIfError(fs.Connect() );
  if( BaflUtils::FileExists(fs,filename)  ){
    //open file
    RFile file;
    TUint shareMode=EFileRead | EFileWrite ;
    file.Open( fs, filename,shareMode ) ;   
    TInt fileSize=0;
    file.Size(fileSize);

    CnvUtfConverter *cutf=new (ELeave) CnvUtfConverter;

    HBufC8 *bufc = HBufC8::NewL(fileSize+1);
    file.Read( (TDes8&) (bufc->Des()) );

    //LogToFile(_L("c:\\cbm\\rsadebug.txt"), bufc->Des());//

    TInt res;
    TPtrC8 iCursor(bufc->Des());

    while( ( res = iCursor.FindF(KCRLF) ) >= 0 ){  //有可能位置是从0开始的 如果有问题.再改成 >0
      TPtrC8 result = iCursor.Left(res);
      iCursor.Set(iCursor.Right(iCursor.Length() - (res +2)));

      lines.AppendL(result);
    }
       
    if(iCursor.Size()>0) {
      lines.AppendL(iCursor);
    }

    delete cutf;

    delete  bufc;
    file.Close();
  }
  fs.Close();
}

//file end




//CIniFile.h

#ifndef __C_INI_FILE_H__
#define __C_INI_FILE_H__

#include <e32base.h>  //CArrayPtrFlat

#include "util/CFile.h"


typedef struct tagINIElement
{
    TBuf8<40>   _Section;
    TBuf8<40>   _Key;
    TBuf8<512>  _Value;
}IniElement;



class CIniFile :public  CFile
{
public:
  static CIniFile* NewL(const TDesC &p_fileName);
  static CIniFile* NewLC(const TDesC &p_fileName);
  CIniFile(const TDesC &p_fileName);
  ~CIniFile();
 
 
  TInt OpenIni();
  TInt GetValue(const TDesC8 &p_Section, const TDesC8 &p_Key, TDes8 &p_Value );
  TInt PutValue(const TDesC8 &p_Section, const TDesC8 &p_Key, const TDesC8 &p_Value ); 
  void WriteIni();
  void CloseIni();

  void DostroyL();
private:
  CArrayPtrFlat<IniElement> *m_pIniElements;
};

#endif //__C_INI_FILE_H__



//CIniFile.cpp

#include "CIniFile.h"


CIniFile *CIniFile::NewL(const TDesC &p_fileName)
{
  CIniFile *self = CIniFile::NewLC(p_fileName);
  CleanupStack::Pop(self);
  return self;
}



CIniFile *CIniFile::NewLC(const TDesC &p_fileName)
{
  CIniFile *self = new(ELeave) CIniFile(p_fileName);
  CleanupStack::PushL(self);
  return self;
}



CIniFile::CIniFile(const TDesC &p_fileName):CFile(p_fileName)
{
  m_pIniElements = NULL;
}



void CIniFile::DostroyL()
{
   for(int i = 0; i<m_pIniElements->Count(); i++)
     delete m_pIniElements->At(i);
    
   delete m_pIniElements;
}



CIniFile::~CIniFile()
{
  DostroyL();
}




TInt CIniFile::OpenIni()
{
  ConstructL(EFileRead | EFileWrite) ;
  TInt pos=0;
  FSeek(ESeekStart,pos);
 
  HBufC8 *bufc = HBufC8::NewL(mFileSize + 3); //包含最末尾添加的\r\n
 
  FGets( (TDes8&)(bufc->Des()) );       //读出所有文件内容
  bufc->Des().Append(_L8("\r\n"));
 
  TPtrC8 iCursor(bufc->Des()); //指向文件内容的指针
 
  TInt iPos = 0;
 
  m_pIniElements = new(ELeave) CArrayPtrFlat<IniElement>(20);
 
  TBuf8<40>   szCurSection(_L8(""));
  TBuf8<40>   szSection(_L8(""));

  while( ( iPos = iCursor.FindF(_L8("\r\n")) ) >= 0 ) {
    iPos = iCursor.FindF(_L8("\r\n"));
    TPtrC8 result = iCursor.Left(iPos); //取出一行内容
   
    HBufC8 *pTmp = HBufC8::NewL(result.Length());
    pTmp->Des().Copy(result);
    pTmp->Des().Trim();

   
    if((pTmp->Des().Length() > 0) && pTmp->Des()[0] == '[') //查找section
    {
       
      pTmp->Des().Delete(0, 1);

      if( pTmp->Des()[pTmp->Des().Length() -1 ] == ']')
        pTmp->Des().Delete(pTmp->Des().Length()-1, 1);
   
      szSection.Copy(pTmp->Des());
   
      if (szCurSection != szSection)
        szCurSection = szSection;
    } else if( KErrNotFound != pTmp->Des().Find(_L8("=")) )  //key=value
    {
      if (szCurSection.Compare(_L8("")) != 0)
      {
        IniElement *element = new(ELeave) IniElement;
        element->_Section.Copy(szCurSection) ;
       
        TPtrC8 ptrKey(pTmp->Des());
        TInt iKeyPos=0;
        iKeyPos = ptrKey.FindF(_L8("="));
        TPtrC8 ptrTmpKey = ptrKey.Left(iKeyPos);
       
        element->_Key.Copy(ptrTmpKey);
        element->_Key.Trim();
       
        ptrKey.Set( ptrKey.Right(ptrKey.Length() - iKeyPos -1) );
        element->_Value.Copy(ptrKey);
        element->_Value.Trim();
       
        m_pIniElements->AppendL(element);
      }
    } else   //注释,,空行等无用数据
    {
      if (szCurSection.Compare(_L8("")) ==0 ) //没有section的注释空行..在文件开头
      {
        IniElement *element = new(ELeave) IniElement;
        element->_Section.Copy(_L8("###"));
        element->_Key.Copy(_L8("##"));
        element->_Value.Copy(pTmp->Des());
        m_pIniElements->AppendL(element);
      } else   //section中的注释空行
      {
          IniElement *element = new(ELeave) IniElement;
          element->_Section.Copy(szCurSection);
          element->_Key.Copy(_L8("##"));
        element->_Value.Copy(pTmp->Des());
        m_pIniElements->AppendL(element);
      }
    }
   
    delete pTmp;
    iCursor.Set( iCursor.Right(iCursor.Length() - (iPos +2)) );//
  }
  delete bufc;
  CloseIni();  //关闭文件句柄
  return 1;
}


TInt CIniFile::GetValue(const TDesC8 &p_Section, const TDesC8 &p_Key, TDes8 &p_Value )
{
  for(int i=0; i<m_pIniElements->Count(); i++)
  {
      if(  (m_pIniElements->At(i)->_Section.Compare( (TDesC8&)p_Section ) ==0 ) &&
          (m_pIniElements->At(i)->_Key.Compare( (TDesC8&)p_Key ) ==0 ) )
     
      {
        p_Value.Copy(m_pIniElements->At(i)->_Value);
      return 1;
      }
  }
  return 0;
}
 
 
 
 
TInt CIniFile::PutValue(const TDesC8 &p_Section, const TDesC8 &p_Key, const TDesC8 &p_Value )
{
  IniElement *element = new(ELeave) IniElement;
  element->_Section.Copy(p_Section);
  element->_Key.Copy(p_Key);
  element->_Value.Copy(p_Value);
 
 
  TInt b_KeyFind = 0;
  for(int i = 0; i<m_pIniElements->Count(); i++)
  { 
    //IniElement *tmp = m_pIniElements->At(i);
    if( (m_pIniElements->At(i)->_Section.Compare( (TDesC8&)p_Section ) ==0 ) &&
          (m_pIniElements->At(i)->_Key.Compare( (TDesC8&)p_Key ) ==0 ))
    {
      m_pIniElements->At(i)->_Value.Copy(p_Value);
      b_KeyFind = 1;
      break;
    }
  }
 
  if(0 == b_KeyFind)
  {
    TInt b_secFind = 0;
    for(int i=0; i<m_pIniElements->Count(); i++)
    {
      if( (m_pIniElements->At(i)->_Section.Compare( (TDesC8&)p_Section ) ==0) &&
            (m_pIniElements->At(i)->_Key.Compare( _L8("##")) != 0) ) 
        {
          m_pIniElements->InsertL(i+1, element);
          b_secFind  = 1;
        break;
        }
    }
   
    if(b_secFind  != 1) //如果是新增的section
      m_pIniElements->AppendL(element);
  }
//输出调试
  /*for(int i=0; i<m_pIniElements->Count(); i++)
  {
    TBuf8<512> dd;
    dd.Format(_L8("%S %S %S\r\n"), &(m_pIniElements->At(i)->_Section), &(m_pIniElements->At(i)->_Key), &(m_pIniElements->At(i)->_Value));
    CFile::LogToFile(_L("c:\\stock\\debugini.txt"), dd);
  }
  */

 
  //将内存中的ini覆写回文件
  ConstructL(EFileRead | EFileWrite);
  TInt pos;
  FSeek(ESeekStart,pos);
  FSetSize(0);
 
  WriteIni();
 
  CloseIni();  //关闭文件句柄
  return 1;
}


void CIniFile::WriteIni()
{
  if(m_pIniElements->Count()>0)
  {
    TBuf8<40>   szCurSection(_L8(""));
   
    HBufC8 *strm = HBufC8::NewL(255);
    strm->Des().Zero();
 
    szCurSection.Copy(m_pIniElements->At(0)->_Section);
    if(szCurSection.Compare( _L8("###")) != 0)//文件开头的注释和空行
    {
      strm->Des().Format(_L8("[%S]\r\n"), &szCurSection);
      FPuts(strm->Des());
    }

    for(int i=0; i<m_pIniElements->Count(); i++)
    {
      IniElement *etmp = m_pIniElements->At(i);
      if(etmp->_Section.Compare(szCurSection) == 0) //如果查到当前块的子项
      {
        if( (etmp->_Key.Compare(_L8("##")) == 0 ) && (etmp->_Value.Compare(_L8("")) == 0 ))
          {
            FPuts( _L8("\r\n"));
          } else if( (etmp->_Key.Compare(_L8("##")) == 0 ) && (etmp->_Value.Compare(_L8("")) != 0 ))
          {
            strm->Des().Format(_L8("%S\r\n"), &(etmp->_Value));
          FPuts(strm->Des());
          } else
          {
            strm->Des().Format(_L8("%S = %S\r\n"), &(etmp->_Key), &(etmp->_Value));
          FPuts(strm->Des());
          }
      } else
      {
        szCurSection.Copy(etmp->_Section);
        if(szCurSection.Compare( _L8("###")) != 0)
        {
          strm->Des().Format(_L8("[%S]\r\n"), &szCurSection);
          FPuts(strm->Des());
        }
       
          if( (etmp->_Key.Compare(_L8("##")) == 0 ) && (etmp->_Value.Compare(_L8("")) == 0 ))
          {
            FPuts( _L8("\r\n"));
          } else if( (etmp->_Key.Compare(_L8("##")) == 0 ) && (etmp->_Value.Compare(_L8("")) != 0 ))
          {
            strm->Des().Format(_L8("%S\r\n"), &(etmp->_Value));
          FPuts(strm->Des());
          } else
          {
            strm->Des().Format(_L8("%S = %S\r\n"), &(etmp->_Key), &(etmp->_Value));
          FPuts(strm->Des());
          }
       
      }
     
    }
    delete strm;
  }

}



void CIniFile::CloseIni()
{
  CFile::DostroyL();
}





posted on 2007-08-22 12:29 Khan 阅读(3605) 评论(28)  编辑 收藏 引用 所属分类: GCC/G++跨平台开发

FeedBack:
# re: symbian 的ini文件类. 2007-08-22 13:00 Khan's Notebook
调用方式:
TBuf<128> path;
path.Format(_L("c:\\cbm\\cbm.ini"));
CIniFile *ini = CIniFile::NewL(path);
ini->OpenIni();

TBuf8<256> sChannels;
ini->GetValue(_L8("cbm"), _L8("channels"), sChannels);

TBuf8<4> flag = _L8("1");
ini->PutValue(sChannel, _L8("flag"), flag);

ini->CloseIni();
delete ini;
  回复  更多评论
  
# re: symbian 的ini文件类. 2007-09-03 11:04 Mido
为什么在最后退出时会“严重错误”?  回复  更多评论
  
# re: symbian 的ini文件类. 2007-09-07 09:53 Khan's Notebook
我不太清楚你工程中如何调用
我在6670 n70手机中测试没有出现异常  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-21 17:39 新手
m_pIniElement好像没有释放,请教用destroyL隐藏父类的destroyL是为什么?另外,CIniFile中的的destroyL为什么没有调用?  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-21 18:12 新手
另外,在调用的时候,ini->CloseIni();好像是多于的吧?  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-22 10:27 Khan.Lau
恩.确实忘记调用了CIniFile::destroyL, 马上改过来. 应该是在析构器中调用  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-22 10:30 Khan's Notebook
@新手
ini->CloseIni();主要是为了释放文件句柄  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-24 18:27 新手
ini->CloseIni();是释放文件句柄没有错,但是我看你每一次操作后都有一个CloaseIni()的,如果还调用ini->CloseIni();来释放句柄,是不是就重复了?
  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-26 11:08 Khan's Notebook
嗯. 这样是为了安全. 因为我的项目中需要在一个或者多个进程中写ini文件. 也需要读ini文件. 为了避免文件读写冲突, 我习惯用完马上关掉.. 我对文件读写冲突是怕了  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-27 15:12 新手
是啊,但是中间好像有内存泄露,IniElement *element = new(ELeave) IniElement;NEW出来了,不删掉可以吗?
  回复  更多评论
  
# re: symbian 的ini文件类. 2007-12-27 15:16 Khan's Notebook
void CIniFile::DostroyL()
{
for(int i = 0; i<m_pIniElements->Count(); i++)
delete m_pIniElements->At(i);

delete m_pIniElements;
}



CIniFile::~CIniFile()
{
DostroyL();
}


析构里面不是删除了么  回复  更多评论
  
# re: symbian 的ini文件类. 2008-08-12 00:06 Coastline
自己模拟效率太低了,还是通过CDictionaryFileStore搞吧,
http://coastline.freepgs.com/?p=13  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 11:11 李浩
TBuf<128> sPath;
sPath.Format(_L("c:\\account.ini"));
CIniFile* ifile=CIniFile::NewL(sPath);

ifile->OpenIni();
TBuf8<255> sTemp;
//ifile->GetValue(_L8("account"),_L8("server"),sTemp);
//CEikEdwin* editor = (CEikEdwin*)(dlg->ControlOrNull(ESERVEREDTId));
//editor->GetText(iServer);
CEikEdwin* eds=static_cast< CEikEdwin *>(Control(ESERVEREDTId));
if (eds)
eds->GetText(iServer );
User::InfoPrint(iServer);
CEikEdwin* edu=static_cast< CEikEdwin *>(Control(EUSEREDTId));
if (edu)
edu->GetText(iUser );
User::InfoPrint(iUser);
CEikEdwin* edp=static_cast< CEikEdwin *>(Control(EPASSWORDEDTId));
if (edp)
edp->GetText(iPassword );
//User::InfoPrint(iPassword);
sTemp.Copy(iServer);
ifile->PutValue(_L8("account"),_L8("server"),sTemp);
sTemp.Copy(iUser);
ifile->PutValue(_L8("account"),_L8("user"),sTemp);
sTemp.Copy(iPassword);
ifile->PutValue(_L8("account"),_L8("password"),sTemp);
//static_cast< CAknQueryControl*>(Control())->GetText(iUser );
//static_cast< CAknQueryControl*>(Control())->GetText(iPassword );


ifile->CloseIni();
delete ifile;
ifile=NULL;
我这样调用有错吗?我这边模拟器中会因为这个而出错  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 11:50 Khan's Notebook
我在项目中某处的调用代码 n70 6670真机执行无问题

void CMobileStockAppUi:: InitChannelData(CArrayFixFlat<STItemStock>* _list_data)
{
TBuf8<20> title8;
TBuf<20> title16;
TBuf8<128> stocks;

STItemStock item;

TBuf<128> path;
// path.Format(_L("%c:\\MOBILESTOCK\\Stock.ini"), dir);
path.Format(_L("%c:\\SYSTEM\\APPS\\MOBILESTOCK\\Stock.ini"), dir);
CIniFile *ini = CIniFile::NewL(path);
ini->OpenIni();
ini->GetValue(_L8("Stock"), _L8("stocks"), stocks);
ini->GetValue(_L8("Stock"), _L8("channel"), title8);
TLex8 tlex(title8);
TInt channeltmp;
tlex.Val(channeltmp);
iChannel = channeltmp;

CDesC8Array* pLines = new(ELeave) CDesC8ArrayFlat(20);
ByteCode::SplitBuf((const TDesC8&)stocks, _L8(","), *pLines );


for(TInt i = 0; i < pLines->Count(); i++ )
{
TPtrC8 stock_sec = (*pLines)[i];

ini->GetValue(stock_sec, _L8("flag"), title8);
TInt ib = 0;

tlex.Assign(title8);
tlex.Val(ib);
if( ib == 1 ) //先判断flag是否有效
{
TBuf8<20> stock_name =_L8("");;
ini->GetValue(stock_sec, _L8("name"), stock_name);
title8.Format(_L8("%S"), &stock_name );
ByteCode::ConvGbk2Uni(title8, title16) ;
item._name = title16;

tlex.Assign(stock_sec);
tlex.Val(ib);
item._id = ib;

ini->GetValue(stock_sec, _L8("postion"), title8);
CDesC8Array* pposes = new(ELeave) CDesC8ArrayFlat(20);
ByteCode::SplitBuf(title8, _L8("/"), *pposes );
if(pposes->Count() == 3)
{
tlex.Assign((*pposes)[0]);
tlex.Val(ib);
item._pos = ib;

tlex.Assign((*pposes)[1]);
tlex.Val(ib);
item._pos_page = ib;

tlex.Assign((*pposes)[2]);
tlex.Val(ib);
item._pos_offset = ib;
}

delete pposes;

ini->GetValue(stock_sec, _L8("begin_price"), title8);
tlex.Assign(title8);
tlex.Val(ib);
item._begin_price = ib;

item._curr_price = 0;

_list_data->AppendL(item);
}
}

delete pLines;


ini->CloseIni();
delete ini;
}  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 13:00 李浩
不是GetValue有问题,是PutValue有问题,我的代码中监测下来  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 14:20 Khan's Notebook
put部分代码

void CCbmReciver::RunL()
{
TInt reqCode = this->iStatus.Int();

if (reqCode == KErrNone)
{
TPageMsg page;

CMobileStockAppUi *ui = (CMobileStockAppUi*)(CEikonEnv::Static()->AppUi());
//00 01 序列号
//02 03 频道号
//04 Data Coding Scheme
//05 Page parameter
//06-87 广播体


page._channel=(mBuf[2]<<8) + mBuf[3];

//channel = page._channel;

if(page._channel == ui->iChannel)//如果属于频道列表之内
{

//DebugPrint(mBuf);//debug

page._serial =(mBuf[0]<<8) + mBuf[1]; //cbm序列号
page._cur_page=(mBuf[5] >> 4) & 0x0F; //页码
page._total_page=(mBuf[5]) & 0x0F; //总页数

if(mBuf[6]==0x7A && mBuf[7]==0x34)
mBuf.Delete(0,8); //广州需要去掉头8个字节
else
mBuf.Delete(0,6); //深圳去掉6字节

TBuf8<40> base64tmp;
for(TInt j = 0; j<mBuf.Length(); j++)
{
if(mBuf[j]>0)
base64tmp.Append(mBuf[j]);
}

base64tmp.Delete(0,2);//去掉2字节多余的填充,
TUint8 asms = base64tmp[0] &0x0f; //第几条广播
TUint8 aflag = base64tmp[1] &0x0f;
base64tmp.Delete(0,2);//去掉数据包头,

for (TInt i = 0; i<ui->_list_data->Count(); i++)
{
if((*(ui->_list_data))[i]._pos_page == page._cur_page - 1)//如果包含需要的页
{
if((*(ui->_list_data))[i]._pos == asms) //如果包含当前的消息
{

HBufC8 * buffer = HBufC8::NewLC(mBuf.Length());
TImCodecB64 b64;
TPtr8 buffPtr = buffer->Des();
b64.Initialise();
b64.Decode(base64tmp, buffPtr);

if (aflag == 1)//开盘价
{
//存配置文件
TBuf8<10> aid;
aid.Format(_L8("%06d"), (*(ui->_list_data))[i]._id);

TBuf8<10> abegin_price;
TInt aoffset= 1 + (*(ui->_list_data))[i]._pos_offset * 2;
abegin_price.Format(_L8("%d"), (buffPtr[aoffset]<<8) + buffPtr[aoffset+1]);
TBuf<128> path;
path.Format(_L("%c:\\SYSTEM\\APPS\\MOBILESTOCK\\Stock.ini"), ui->dir);
CIniFile *ini = CIniFile::NewL(path);
ini->OpenIni();
ini->PutValue(aid, _L8("begin_price"), abegin_price);
ini->CloseIni();
delete ini;
}
else //当前价
{
//显示在屏幕
TInt aoffset= 1 + (*(ui->_list_data))[i]._pos_offset * 2;

(*(ui->_list_data))[i]._curr_price = (buffPtr[aoffset]<<8) + buffPtr[aoffset+1];//取得当前价
HBufC* buf = HBufC::NewLC(80);
buf->Des().Format(_L("offset=%d index=%d price=%d"), aoffset, i, (*(ui->_list_data))[i]._curr_price);
CAknInformationNote* note = new (ELeave) CAknInformationNote;
note->ExecuteLD(*buf);
CleanupStack::PopAndDestroy(buf);
/*
TBuf<32> filename;
filename.Format(_L("c:\\ddc%d-%d.txt"), aoffset, i);
CFile::LogToFile(filename, base64tmp);*/
ui->DrawStockAppview();//重绘界面
}


CleanupStack::PopAndDestroy(buffer);
}
}
}
}

StartL();
}
}  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 15:26 李浩
要郁闷死了,我只是
打开文件
ifile->PutValue(_L8("account"),_L8("server"),_L8("lihao"));
关闭,这样可以操作,但是退出程序时提示有内存泄露,却找不着  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 16:04 Khan's Notebook
你比对一下回车符之类的文本格式看看, unix和dos方式都试试  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 16:54 李浩
对比了,它是能读,没有问题,只有写的时候,写进去了,只是在程序退出时才会报alloc错误  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 17:36 Khan's Notebook
留email:我给你一份最新的版本, 或许我后来改了什么东西, 最终项目测试是在07年12月的时候, 可能跟这里的版本有一些变化  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-02 17:39 李浩
lihao_nx@yahoo.com.cn  回复  更多评论
  
# 类是有个bug 2008-09-04 09:33 李浩
TInt CIniFile::PutValue(const TDesC8 &p_Section, const TDesC8 &p_Key, const TDesC8 &p_Value )
{
IniElement *element = new(ELeave) IniElement;
.....
}
在整个函数中,element 没有释放,我在我的模拟器释放了就可以了  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-04 10:19 Khan's Notebook
释放是在这里的.
void CIniFile::DostroyL()
{
for(int i = 0; i<m_pIniElements->Count(); i++)
delete m_pIniElements->At(i);

delete m_pIniElements;
}
m_pIniElements只是存放指针, 你如果早早的把指针指向的内存块都释放了. 这个东西就变成一堆垃圾数据了..., 你说你那边正常我就有点不懂了...  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-04 14:03 李浩
我也搞不懂了,呵呵,因为它在PutValue中是将指针压入到m_pIniElements.并不能释放,但是在我的模拟器上却是只有这样才能正常,非常搞不懂  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-04 15:02 Khan's Notebook
不确定你写ini之后最终是否执行了. iniclose那个释放的操作...  回复  更多评论
  
# re: symbian 的ini文件类. 2008-09-05 09:25 李浩
是执行了,我是按你的调用方法调用的  回复  更多评论
  
# re: symbian 的ini文件类. 2009-03-31 17:47 scribbler
以上的问题出现在PutValue函数里面, 假如element指针没有压入到m_pIniElements里面,那么就最后析构的时候不能释放, 因此造成内存泄露.  回复  更多评论
  
# re: symbian 的ini文件类. 2011-02-25 03:04 zorro0799
CIniFile::~CIniFile()
{
DostroyL();
}
这少一句,而且好象这个ini只能结操作gbk的  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理