用c++写的小工具,用来写文件恢复企业通讯录,员工名录的详细信息。

jackxiang 2008-3-17 14:12 | |
//该程序通过mysql数据库得到path,然后通过函数GetUserDetailFilename得到user_info?.conf ? 里面的数字,然后配合mysql数据库里面取得的内容写入到文本中,修复rpc造成的影响让webmail的企业通讯录下员工名录得以正常查看任何一个用户的详细信息!xiangdong2@staff.sina.com.cn
//测试enterpriseid=100334;
//CC -o user_info_mysql.cgi user_info_mysql.cpp -L/usr/local/lib/mysql -lmysqlclient -I/usr/local/include
#include <fstream>
#include <iostream>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <math.h>
#include "/usr/local/mysql/include/mysql/mysql.h"
const char mysqlServer[20] = "10.88.15.114";
const char user[20]="web";
const char password[20]="sinatest";
const char database[20]="enterprise";
unsigned  int port=3306;
#define DETAILFILE_NUM  10
static int GetUserDetailFilename(const char *email, char *filename, const unsigned int size=1);
char path[1024];
char ent_address[21]="/ent_address/";
char filename[100];
char  file_path_del[1024];
char buffer[1024],buffer1[1024],buffer2[1024],buffer3[1024],buffer4[1024],buffer5[1024],buffer6[1024],buffer7[1024],buffer8[1024],buffer9[1024];
using namespace std;
int main(int argc,char* argv[])
{
  
  MYSQL myData;
  MYSQL_RES *res;
  MYSQL_FIELD *fd;
  MYSQL_ROW row;
  int i,j,rowCount = 0,colCount = 0;
  string query="select path from enterprise where enterpriseid=";
  string query2 = "select email,realname,tel,mobile,address,office,enter_time,num,dept,flag from enterprisemail_info where enterpriseid=";
  if (argc < 2) {
    printf("usage:  请输入enterprise库enterprise表里面企业id(enterpriseid? Such As:./user_info_mysql.cgi 100334)\n", argv[0]);
    exit(1);
          }
    query+=argv[1];
  query2+=argv[1];

  mysql_init( &myData );
  if(!mysql_real_connect( &myData, mysqlServer, user, password, database,port,NULL,0))
  {
    printf("connect mysql error!\n");
        
  }
  
  
  if( mysql_query(&myData, query.c_str()) != 0 )
  {
  printf("query error!\n");
  return 0;
  }else{
    
     cout << "mysql query run ok!\n";
    
     }
  
  res = mysql_store_result( &myData );
  row = mysql_fetch_row( res );
  strcpy(path,row[0]);
  //cout << row[0]<<path;取得path然后加入ent_address/
  strcat(path,ent_address);
  //cout << path<<"\n";
  if( mysql_query(&myData, query2.c_str()) != 0 )
  {
  printf("query error!\n");
  return 0;
  }else{
    
     cout << "mysql query run ok!\n";
    
     }
  
  
  res = mysql_store_result( &myData );
  rowCount = (int) mysql_num_rows( res );
  colCount = (int) mysql_num_fields( res );
  //cout << "\t"<<colCount<<"\t"<<rowCount<<"\n";
  
  char num[2];
  for(i=0;i<=10;i++){//构造从user_info0.conf to user_info10.conf 路径然后del掉他们,为下面从数据库取写做准备

  strcpy(file_path_del,path);
  strcat(file_path_del,"user_info");
  sprintf(num,"%d",i);
  strcat(file_path_del,num);
  strcat(file_path_del,".conf");
  if( remove(file_path_del) != 0 )//删除文件
    cout << "Error deleting "<< file_path_del <<" file, \t Because it is not exit! But it is still Ok...\n";
    else
    cout << "File" <<file_path_del<<" successfully deleted\n";
  
    }
  
  for(i = 0; i < rowCount; i++)
  {      
    row = mysql_fetch_row( res );
    for(j=0;j<colCount;j++){
    strcpy(buffer,row[0]);
    strcpy(buffer1,row[1]);
    strcpy(buffer2,row[2]);
    strcpy(buffer3,row[3]);
    strcpy(buffer4,row[4]);
    strcpy(buffer5,row[5]);
    strcpy(buffer6,row[6]);
    strcpy(buffer7,row[7]);
    strcpy(buffer8,row[8]);
    strcpy(buffer9,row[9]);
        
    char* p = strstr(buffer,"@");
    *p=0;
    //取@前面的
    const char *uid;
    uid=buffer;
    
    if(GetUserDetailFilename(uid, filename, 100)<0)//获取目录位置
    {
      cout<<"GetUserDetailFilename() erro...";  
  
    }
      
                 }//字段数循环
    char file_path[1024];
    strcpy(file_path,path);
    strcat(file_path,filename);//得到文件的完整的path路径
    //cout<<"file_path="<<file_path<<"\n\n";

    //write file
    ofstream fout(file_path,ios_base::app);
    fout <<"[USER:"<<buffer<<"]\n"<<"EMAIL="<<buffer<<"\n"<<"REALNAME="<<buffer1<<"\n"<<"TEL="<<buffer2<<"\n"<<"MOBILE="<<buffer3<<"\n"<<"ADDRESS="<<buffer4<<"\n"<<"OFFICE="<<buffer5<<"\n"<<"ENTER_TIME="<<buffer6<<"\n"<<"NUM="<<buffer7<<"\n"<<"DEPT="<<buffer8<<"\n"<<"FLAG="<<buffer9<<"\n";

    fout.close();
    


    memset(file_path, 0, 1024);//清空该变量 清空其它字段缓存
    memset(buffer, 0, 1024);
    memset(buffer1, 0, 1024);
    memset(buffer2, 0, 1024);
    memset(buffer3, 0, 1024);
    memset(buffer4, 0, 1024);
    memset(buffer5, 0, 1024);
    memset(buffer6, 0, 1024);
    memset(buffer7, 0, 1024);
    memset(buffer8, 0, 1024);
    memset(buffer9, 0, 1024);
    
      
  }//条目数
  
  cout <<"\n\n\nVery good ,The task  finished all right!\t goodbye...\n\n\n";

}




int GetUserDetailFilename(const char *email, char *filename, const unsigned
int size)
{
  if(!email || !filename)return -1;
  int t=0, n;
  for(int i=0; i<strlen(email); i++)
    t+=email[i];
  
  n = (int)fmod(t, DETAILFILE_NUM)+1;
  if(snprintf(filename, size, "user_info%d.conf", n)>=size)return -1;
  return 0;
}

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/948/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]