root/_BazaLib/BL_File.cpp

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ReadString
  2. FindPrevEOL
  3. WriteString
  4. WriteString
  5. WriteChar
  6. ReadStringAt
  7. WriteStringAt
  8. CopyAttributeFrom

   1 /*
   2 ===============================================
   3 Project:        BeFar
   4 File:           BL_File.cpp   ( was TextFile.cpp )
   5 Desc:           Provides textfile services.
   6 Author:         Baza, [based on Nexus sources ]
   7 Created:        05 Nov 1999
   8 Modified:       09 Jul 2000
   9 ===============================================
  10 */
  11 #include "stdio.h"
  12 #include <stdlib.h>
  13 #include <fs_attr.h>
  14 #include "BL_File.h"
  15 
  16 BL_File::BL_File(void) : BFile(){
  17 }
  18 
  19 BL_File::BL_File(const BL_File &file) : BFile(file) {
  20 }
  21 
  22 BL_File::BL_File(const entry_ref *ref, uint32 openMode) : BFile(ref, openMode){
  23 }
  24 
  25 BL_File::BL_File(const BEntry *entry, uint32 openMode) : BFile(entry, openMode){
  26 }
  27         
  28 BL_File::BL_File(const char *path, uint32 openMode) : BFile(path, openMode) {
  29 }
  30 
  31 BL_File::BL_File(BDirectory *dir, const char *path, uint32 openMode) : BFile(dir, path, openMode) {
  32 }
  33 
  34 // Destructor
  35 BL_File::~BL_File() {
  36 }
  37 
  38 // Reads strings from file, line by line.
  39 status_t BL_File::ReadString(BString *str)     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41         if(!str)        return B_ERROR;
  42         off_t           pos = BFile::Position();
  43         char            buf[0x100];
  44         size_t          size = BFile::Read(buf, 0x100);
  45         
  46         BString bstr(buf);
  47         int32 line_end = bstr.FindFirst('\n');
  48         if(0 == size) // no data and hit the end of file
  49                 return B_ERROR;
  50         if(line_end > size) { // there was data read, but the end of file reached
  51                 off_t size;
  52                 BFile::GetSize(&size);
  53                 line_end =  size - pos;
  54         }
  55         bstr.CopyInto(*str, 0, line_end);
  56         int32 bytes = str->Length();
  57         line += 1; 
  58         if(BFile::Seek(pos+bytes+1, SEEK_SET)<0) return B_ERROR;
  59         return B_OK;
  60 }
  61 
  62 // Reads strings from file, line by line , back direction
  63 status_t
  64 BL_File::FindPrevEOL(off_t & i_PosResult)     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66         i_PosResult = -1;
  67         
  68         off_t           iPos = BFile::Position(),iOldPos = iPos;
  69         char            cBuf[0x1000+1]; 
  70         
  71         if(iPos<=0){
  72                 i_PosResult = 0;
  73                 return B_OK;            
  74         }
  75         
  76         while(true){
  77                 iPos -= 0x1000;
  78                 if(iPos<0) iPos=0;                              
  79                 
  80                 iPos = BFile::Seek(iPos,SEEK_SET);
  81                 if(iPos<0) return B_ERROR;
  82                 
  83                 size_t size = BFile::Read(cBuf, iOldPos - iPos+1);              
  84                 if(0==size) return B_ERROR;
  85 
  86                 cBuf[size]='\0';
  87                 
  88                 BString bstr(cBuf);
  89                 int32 line_end = bstr.FindLast('\n');
  90                 
  91                 if(line_end<0 && iPos==0){
  92                         i_PosResult = 0;
  93                         return B_OK;
  94                 }else                   
  95                 if(line_end<0){
  96                         line_end = bstr.Length()-1;
  97                 }
  98                 if(line_end>size) return B_ERROR;else   
  99                 if(line_end<=size){
 100                         i_PosResult = iPos+line_end;
 101                         return B_OK;
 102                 }               
 103                 iOldPos = iPos;
 104                 continue;                               
 105         }               
 106 }
 107 
 108 
 109 status_t        
 110 BL_File::WriteString(const BString &s)    /* [previous][next][first][last][top][bottom][index][help] */
 111 {       
 112         BFile::Seek(0, SEEK_END);               
 113         BFile::Write(s.String(), s.Length());
 114         if(s.FindLast('\n') != (s.Length()-1))  BFile::Write("\n",1);
 115         return  B_OK;   
 116 }
 117 
 118 // Appends a string to file, string being forced to end by \n
 119 status_t BL_File::WriteString(const BString *po_Str)       /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         ASSERT(po_Str);         
 122         return WriteString(*po_Str);
 123 }
 124 
 125 status_t 
 126 BL_File::WriteChar(const char *pc_Text)    /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128         ASSERT(pc_Text);
 129         BString s(pc_Text);     
 130         return WriteString(&s);
 131 }
 132 
 133 
 134 // TODO
 135 status_t BL_File::ReadStringAt(off_t location, BString str) {    /* [previous][next][first][last][top][bottom][index][help] */
 136         const off_t pos = BFile::Position();
 137         return B_OK;
 138 }
 139 
 140 // TODO
 141 status_t BL_File::WriteStringAt(off_t location, BString str) {    /* [previous][next][first][last][top][bottom][index][help] */
 142         const off_t pos = BFile::Position();
 143         return B_OK;
 144 }
 145 
 146 status_t        BL_File::CopyAttributeFrom(const char*pc_AttrName,BL_File & o_SrcFile)    /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         // check files 
 149         status_t uRes = InitCheck();    if(B_OK!=uRes) return uRes;
 150         uRes = o_SrcFile.InitCheck();   if(B_OK!=uRes) return uRes;
 151         // check name
 152         if(!pc_AttrName || (0==strlen(pc_AttrName))) return B_ERROR;
 153 
 154         {
 155                 Debug_Info("start");
 156                 char            pcName[B_ATTR_NAME_LENGTH];
 157                 attr_info       uAttrInfo;
 158                 o_SrcFile.RewindAttrs();
 159                 while(B_OK==o_SrcFile.GetNextAttrName(pcName)){
 160                         Debug_Info(pcName);
 161                 }
 162         }
 163 
 164         // try to find req.attribute //
 165         attr_info       uAttrInfo;
 166         uRes = o_SrcFile.GetAttrInfo(pc_AttrName,&uAttrInfo);
 167         if(B_OK!=uRes) return uRes;
 168         // alloc buffer //
 169         char *pcBuf = (char*)malloc(uAttrInfo.size)+1;
 170         // read data //
 171         if(0==o_SrcFile.ReadAttr(pc_AttrName,uAttrInfo.type,0,(void*)pcBuf,uAttrInfo.size)) return B_ERROR;
 172         // write data //
 173         return 0==WriteAttr(pc_AttrName,uAttrInfo.type,0,(void*)pcBuf,uAttrInfo.size)?B_ERROR:B_OK;
 174 }

/* [previous][next][first][last][top][bottom][index][help] */