Show
Ignore:
Timestamp:
05/01/08 01:43:10 (7 months ago)
Author:
flynd
Message:

Create history dir if missing before writing history file in it. Combined the Save and Append functions as they were almost identical.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/licq/src/history.cpp

    r4526 r6177  
    1111#endif 
    1212 
     13#include <cstdio> 
    1314#include <string.h> 
    1415#include <unistd.h> 
     
    397398} 
    398399 
    399 //---Save---------------------------------------------------------------------- 
    400 /*! \brief Writes history to disk 
    401  * 
    402  * Writes history to disk. If file does not exists it is created. 
    403  */ 
    404 void CUserHistory::Save(const char *buf) 
     400void CUserHistory::Write(const char* buf, bool append) 
    405401{ 
    406402  if (m_szFileName == NULL || buf == NULL) return; 
    407   int fd = open(m_szFileName, O_WRONLY | O_CREAT | O_TRUNC, 00600); 
     403 
     404  // Make sure history dir exists before trying to write a file in it 
     405  char historydir[MAX_FILENAME_LEN]; 
     406  snprintf(historydir, sizeof(historydir) - 1, "%s/%s", BASE_DIR, HISTORY_DIR); 
     407  if (mkdir(historydir, 0700) == -1 && errno != EEXIST) 
     408  { 
     409    fprintf(stderr, "Couldn't mkdir %s: %s\n", historydir, strerror(errno)); 
     410    return; 
     411  } 
     412 
     413  int fd = open(m_szFileName, O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC), 00600); 
    408414  if (fd == -1) 
    409415  { 
     
    413419  } 
    414420  write(fd, buf, strlen(buf)); 
     421  if (append) 
     422    write(fd, "\n", 1); 
    415423  close(fd); 
    416424} 
     
    429437  hist.erase(hist.begin(), hist.end()); 
    430438} 
    431  
    432  
    433 //---Append-------------------------------------------------------------------- 
    434 /*! \brief Appends a message to the history 
    435  * 
    436  * Appends message _sz to the history. If the history file does not exist it  
    437  * will be created. 
    438  */ 
    439 void CUserHistory::Append(const char *_sz) 
    440 { 
    441   if (m_szFileName == NULL || _sz == NULL) return; 
    442   int fd = open(m_szFileName, O_WRONLY | O_CREAT | O_APPEND, 00600); 
    443   if (fd == -1) 
    444   { 
    445     gLog.Error("%sUnable to open history file (%s):\n%s%s.\n", L_ERRORxSTR, 
    446                m_szFileName, L_BLANKxSTR, strerror(errno)); 
    447     return; 
    448   } 
    449   write(fd, _sz, strlen(_sz)); 
    450   write(fd, "\n", 1); 
    451   close(fd); 
    452 } 
    453  
    454