vss control



#include <stdio.h>
#include <string.h>
#include <windows.h>


#define DEBUG 1


enum
{
PARAM_ERROR,
FORWARD,
BACKWARD,
FOUND_NO_STR,
STR_FOUND,
FSEEK_ERR,
HEADER_OF_FILE,
FILE_OPERATE_API_ERR,
API_ERROR,
WORD_FOUND,
FOUND_NO_WORD,
LOCATE_OK,
LOCATE_NG,
INSERT_SUCCEFULLY,
};
/*******************************************************************/
/****** Search_Str_In_File  ***************************************/
/******************************************************************/


int SearchStrInFile(FILE *fp,char *str,int method,int *site,int *line_t)
{
char buf[10000];
int len = strlen(str);
int ret;
int line = 0;

if(BACKWARD == method)
{
fseek(fp,-(len+1),SEEK_CUR);
}


fgets(buf,len + 1,fp);/* want to read "len+1" words but the truth is 'len' words */


while(!feof(fp))
{
if(!strcmp(buf,str))/* if found str */
{
fseek(fp,-len,SEEK_CUR);
*site = ftell(fp);
*line_t = line;
return STR_FOUND;
}

if( FORWARD == method)/* pointer go back */
{

ret = fseek(fp,-(len-1),SEEK_CUR);

if(ret != 0)
{
#if DEBUG
printf("[- %d -]SearchStrInFile : fseek() return NG\n",__LINE__);
#endif
return FSEEK_ERR;
}
}
else
{
if(ftell(fp) < len + 1)
{
#if DEBUG
printf("[- %d -]SearchStrInFile : goto start of file\n",__LINE__);
#endif
return HEADER_OF_FILE;
}
ret = fseek(fp,-(len+1),SEEK_CUR);
if(ret != 0)
{
#if DEBUG
printf("[- %d -]SearchStrInFile : fseek() return NG\n",__LINE__);
#endif
return 0;
}
}

fgets(buf,len + 1,fp);
if(strlen(buf) < len)
{
line ++;
if(feof(fp))/* end of file */
{
#if DEBUG
printf("[- %d -]SearchStrInFile : end of file,not found str\n",__LINE__);
#endif
return FOUND_NO_STR;
}
if(FORWARD == method)
{
fseek(fp,len- 1,SEEK_CUR);
}
else
{
fseek(fp, -(len + 2), SEEK_CUR);
}
}


}

if(!feof(fp))
{
#if DEBUG
printf("[- %d -]SearchStrInFile : end of file,not found str\n",__LINE__);
#endif
return FOUND_NO_STR;
}


return STR_FOUND;


}
/*******************************************************************/
/****** GetPathAccordingToLabel  ***********************************/
/******************************************************************/
int GetPathAccordingToLabel(char *db_filename, char *vss_label_name, char *vss_path)
{
FILE *fp;
int site;
int line;
int label_longth;
int i = 0;
char ch;
/*----------------------------------*/
/* params check */


if(NULL == db_filename)
{
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : Invalid file name\n",__LINE__);
#endif
return PARAM_ERROR;
}


if(NULL == vss_label_name)
{
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : Invalid label name\n",__LINE__);
#endif
return PARAM_ERROR;
}

if(NULL == vss_path)
{
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : Invalid vss path\n",__LINE__);
#endif
return PARAM_ERROR;
}
/*----------------------------------*/
/* get label site */
fp = fopen(db_filename, "r");


memset(vss_path, '\0', 500);

/* move point to the start place of label */
if(STR_FOUND == SearchStrInFile(fp, vss_label_name, FORWARD, &site, &line))
{
/* move point to the start place of vss path */
label_longth = strlen(vss_label_name);
if(0 == fseek(fp, label_longth+3, SEEK_CUR))
{
/* copy vss path */
ch = fgetc(fp);
while(ch != '\n')
{
vss_path[i] = ch;
ch = fgetc(fp);
i++;
}

if(NULL == vss_path)
{
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : can't find vss path\n",__LINE__);
return FOUND_NO_STR;
#endif
}
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : vss_path = %s\n",__LINE__,vss_path);
#endif


}
else
{
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : SearchStrInFile() return not STR_FOUND\n",__LINE__);
#endif
}
}
else
{
#if DEBUG
printf("[- %d -]GetPathAccordingToLabel() : SearchStrInFile() return not STR_FOUND\n",__LINE__);
#endif
}


return STR_FOUND;

}


/*******************************************************************/
/****** AddItemsToControlVssDBFiles  ******************************/
/******************************************************************/
int AddItemsToControlVssDBFiles(char *db_filename, char *vss_label_name, char *vss_path)
{
FILE *fp;
char db_item[500] = "\0";
int i = 0;
int dir_count = 0;
signed int cut_str_index = 0xFE;
char cut_site_flag = 0;
/*----------------------------------*/
/* params check */


printf("AddItemsToControlVssDBFiles() : vss_path = %s\n",vss_path);
if(NULL == db_filename)
{
#if DEBUG
printf("AddItemsToControlVssDBFiles() : Invalid file name\n");
#endif
return PARAM_ERROR;
}


if(NULL == vss_label_name)
{
#if DEBUG
printf("AddItemsToControlVssDBFiles() : Invalid vss label name\n");
#endif
return PARAM_ERROR;
}

if(NULL == vss_path)
{


#if DEBUG
printf("AddItemsToControlVssDBFiles() : Invalid vss path\n");
#endif
return PARAM_ERROR;
}

/*----------------------------------*/
/* open file */


fp = fopen(db_filename,"at+");
if(NULL == fp)
{
return API_ERROR;
}

while(vss_path[i] != '\0')
{
/* switch '\' to '/' */
if(vss_path[i] == '\\')
{
if(53 == i)// h5
{

}
else if(54 == i)// zx7
{

}
else
{
vss_path[i] = '/';
}
dir_count++;
}
/* if appered three layers direct */
if(dir_count == 3)
{
/* cut the string */
if(cut_site_flag == 0)
{
cut_str_index = i;
cut_site_flag = 1;
}
}


if(i >= cut_str_index)
{
vss_path[i - cut_str_index] = vss_path[i];
}

i++;
}
vss_path[i - cut_str_index] = '\0';
#if DEBUG
printf("[- %d -]AddItemsToControlVssDBFiles() : vss_label_name = %s\n",__LINE__,vss_path);
printf("[- %d -]AddItemsToControlVssDBFiles() : vss_path[cut_str_index] = %c\n",__LINE__,vss_path[cut_str_index]);
#endif

/*----------------------------------*/
/* make item */
strncat(db_item, vss_label_name, strlen(vss_label_name));
strncat(db_item, " = ", strlen(" = "));
strncat(db_item, "\"", strlen("\""));
strncat(db_item, "-p", strlen("-p"));
strncat(db_item, "$/SY6E091121_DTV2012A", strlen("$/SY6E091121_DTV2012A"));
strncat(db_item, vss_path, strlen(vss_path));
strncat(db_item, "\"", strlen("\""));
strncat(db_item, "\n", strlen("\n"));
#if DEBUG
printf("[- %d -]AddItemsToControlVssDBFiles() : db_item = %s\n",__LINE__,db_item);
#endif
/*----------------------------------*/
/* write in file */


if(-1 == fputs(db_item, fp))
{
#if DEBUG
printf("[- %d -]AddItemsToControlVssDBFiles() : fputs() return error\n",__LINE__);
#endif
}

return INSERT_SUCCEFULLY;

}


int main(int argc, char *argv[])
{
char vss_path[500] = "\0";
HWND ParenthWnd;
char vss_prg[300] = "D:\\Program Files\\Microsoft Visual SourceSafe\\ssexp.exe";
char param[500] = "\0";

if(NULL == argv[1])
{
#if DEBUG
printf("[- %d -]main() : oprato mode error\n",__LINE__);
#endif
return 0;
}

if(!strcmp("add",argv[1]))
{
if(INSERT_SUCCEFULLY != AddItemsToControlVssDBFiles(argv[2], argv[3], argv[4]))
{
#if DEBUG
printf("[- %d -]main() : Fail to add item\n",__LINE__);
#endif
}
}
else if(!strcmp("delete", argv[1]))
{
// dummy need to do 
}
else if(!strcmp("start", argv[1]))
{
if(strcmp("NULL", argv[4]))
{
memset(vss_prg, '\0', sizeof(char)*300);
strncat(vss_prg, argv[4], strlen(argv[4]));
strncat(vss_prg, "\0", strlen("\0"));
}

if(STR_FOUND == GetPathAccordingToLabel(argv[2], argv[3], vss_path))
{
#if DEBUG
printf("[- %d -]main() : vss path = %s\n", __LINE__, vss_path);
#endif
/* Close running vss */
ParenthWnd = FindWindow(NULL,TEXT("\\\\10.1.37.210\\sy6e091118-dtv201 - Visual SourceSafe Explorer"));
if(NULL == ParenthWnd)
{
ParenthWnd = FindWindow(NULL,TEXT("\\\\10.1.37.210\\sy6e091118-dtv201 - Visual SourceSafe Explorer"));
if(NULL == ParenthWnd)
{
ParenthWnd = FindWindow(NULL,TEXT("\\\\10.1.37.210\\sy6e091118-dtv2~1 - Visual SourceSafe Explorer"));
if(NULL == ParenthWnd)
{
ParenthWnd = FindWindow(NULL,TEXT("sy6e091121_dtv201 on 10.1.37.21 - Visual SourceSafe Explorer"));
if(NULL != ParenthWnd)
{
SendMessage(ParenthWnd, WM_CLOSE  , 0, 0);
}


}
}
}



/* Open vss */
strncat(param, vss_path, strlen(vss_path));
strncat(param, " ", strlen(" "));
strncat(param, argv[5], strlen(argv[5]));
strncat(param, " ", strlen(" "));
strncat(param, argv[6], strlen(argv[6]));
#if DEBUG
printf("[- %d -]main() : %s %s\n",__LINE__,vss_prg, param);
#endif
ShellExecute(NULL, TEXT("open"), vss_prg, param, NULL, SW_SHOWNORMAL);
}
}
else if(!strcmp("--help", argv[1]))
{
printf("-------------------------------------\n");
printf(" add \n");
printf("-------------------------------------\n");
printf("param 1: operate mode \n");
printf("param 2: file name \n");
printf("param 3: vss label name \n");
printf("param 4: vss path \n");
printf("-------------------------------------\n");
printf(" start \n");
printf("-------------------------------------\n");
printf("param 1: operate mode \n");
printf("param 2: file name \n");
printf("param 3: vss label name \n");
printf("param 4: vss prg \n");
printf("param 5: vss prg -s option,vss server address\n");
printf("param 6: -y option,user and passed\n");
printf("-------------------------------------\n");
return 0;
}
else
{
#if DEBUG
printf("[- %d -]main() : Pass start mode error\n",__LINE__);
#endif
}

return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值