[Ttssh2-commit] [3985] strreplace コマンドを追加した。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 8月 11日 (水) 23:08:41 JST


Revision: 3985
          http://sourceforge.jp/projects/ttssh2/svn/view?view=rev&revision=3985
Author:   yutakapon
Date:     2010-08-11 23:08:41 +0900 (Wed, 11 Aug 2010)

Log Message:
-----------
strreplace コマンドを追加した。

Modified Paths:
--------------
    trunk/teraterm/common/helpid.h
    trunk/teraterm/ttpmacro/ttl.c
    trunk/teraterm/ttpmacro/ttmparse.c
    trunk/teraterm/ttpmacro/ttmparse.h


-------------- next part --------------
Modified: trunk/teraterm/common/helpid.h
===================================================================
--- trunk/teraterm/common/helpid.h	2010-08-11 11:22:32 UTC (rev 3984)
+++ trunk/teraterm/common/helpid.h	2010-08-11 14:08:41 UTC (rev 3985)
@@ -286,6 +286,7 @@
 #define HlpMacroCommandStrlen           92095
 #define HlpMacroCommandStrmatch         92135
 #define HlpMacroCommandStrremove        92181
+#define HlpMacroCommandStrreplcae       92182
 #define HlpMacroCommandStrscan          92096
 #define HlpMacroCommandTestlink         92097
 #define HlpMacroCommandTolower          92118

Modified: trunk/teraterm/ttpmacro/ttl.c
===================================================================
--- trunk/teraterm/ttpmacro/ttl.c	2010-08-11 11:22:32 UTC (rev 3984)
+++ trunk/teraterm/ttpmacro/ttl.c	2010-08-11 14:08:41 UTC (rev 3985)
@@ -3381,13 +3381,33 @@
 	return Err;
 }
 
+static void insert_string(char *str, int index, char *addstr)
+{
+	char *np;
+	int srclen;
+	int addlen;
+
+	srclen = strlen(str);
+	addlen = strlen(addstr);
+
+	// ‚Ü‚¸‚Í‘}“ü‚³‚ê‚é‰ÓŠˆÈ~‚̃f[ƒ^‚ðAŒã‚ë‚Ɉړ®‚·‚éB
+	np = str + (index - 1);
+	memmove_s(np + addlen, MaxStrLen, np, srclen - (index - 1));
+
+	// •¶Žš—ñ‚ð‘}“ü‚·‚é
+	memcpy(np, addstr, addlen);
+
+	// null-terminate
+	str[srclen + addlen] = '\0';
+}
+
 WORD TTLStrInsert()
 {
 	WORD Err, VarId;
 	int Index;
 	TStrVal Str;
 	int srclen, addlen;
-	char *srcptr, *np;
+	char *srcptr;
 
 	Err = 0;
 	GetStrVar(&VarId,&Err);
@@ -3408,17 +3428,28 @@
 	}
 	if (Err!=0) return Err;
 
-	// ‚Ü‚¸‚Í‘}“ü‚³‚ê‚é‰ÓŠˆÈ~‚̃f[ƒ^‚ðAŒã‚ë‚Ɉړ®‚·‚éB
-	np = srcptr + (Index - 1);
-	memmove_s(np + addlen, MaxStrLen, np, srclen - (Index - 1));
+	insert_string(srcptr, Index, Str);
 
-	// •¶Žš—ñ‚ð‘}“ü‚·‚é
-	memcpy(np, Str, addlen);
+	return Err;
+}
 
+static void remove_string(char *str, int index, int len)
+{
+	char *np;
+	int srclen;
+
+	srclen = strlen(str);
+
+	if (len <=0 || index <= 0 || (index-1 + len) > srclen) {
+		return;
+	}
+
+	// •¶Žš—ñ str ‚©‚ç index •¶Žš–Ú‚©‚ç len •¶Žšíœ‚·‚é
+	np = str + (index - 1);
+	memmove_s(np, MaxStrLen, np + len, srclen - len);
+
 	// null-terminate
-	srcptr[srclen + addlen] = '\0';
-
-	return Err;
+	str[srclen - len] = '\0';
 }
 
 WORD TTLStrRemove()
@@ -3426,7 +3457,7 @@
 	WORD Err, VarId;
 	int Index, Len;
 	int srclen;
-	char *srcptr, *np;
+	char *srcptr;
 
 	Err = 0;
 	GetStrVar(&VarId,&Err);
@@ -3443,13 +3474,61 @@
 	}
 	if (Err!=0) return Err;
 
-	// •¶Žš—ñ‚ðíœ‚·‚é
-	np = srcptr + (Index - 1);
-	memmove_s(np, MaxStrLen, np + Len, srclen - Len);
+	remove_string(srcptr, Index, Len);
 
-	// null-terminate
-	srcptr[srclen - Len] = '\0';
+	return Err;
+}
 
+WORD TTLStrReplace()
+{
+	WORD Err, VarId;
+	TStrVal oldstr;
+	TStrVal newstr;
+	char *srcptr;
+	char *p;
+	int srclen, oldlen;
+	int pos;
+	int result = 0;
+
+	memset(oldstr, 0, MaxStrLen);
+	memset(newstr, 0, MaxStrLen);
+
+	Err = 0;
+	GetStrVar(&VarId,&Err);
+	GetIntVal(&pos,&Err);
+	GetStrVal(oldstr,&Err);
+	GetStrVal(newstr,&Err);
+	if ((Err==0) && (GetFirstChar()!=0))
+		Err = ErrSyntax;
+	if (Err!=0) return Err;
+
+	srcptr = StrVarPtr(VarId);
+	srclen = strlen(srcptr);
+
+	if (pos > srclen) {
+		result = 0;
+		goto error;
+	}
+
+	// strptr•¶Žš—ñ‚Ì pos •¶Žš–ڈȍ~‚É‚¨‚¢‚āAoldstr ‚ð’T‚·B
+	p = strstr(srcptr + (pos - 1), oldstr);
+	if (p == NULL) {
+		// Œ©‚‚©‚ç‚È‚©‚Á‚½ê‡‚́A"0"‚Å–ß‚éB
+		result = 0;
+		goto error;
+	}
+
+	// ‚Ü‚¸‚Í oldstr ‚ðíœ‚·‚é
+	oldlen = strlen(oldstr);
+	remove_string(srcptr, p - srcptr + 1, oldlen);
+
+	// newstr ‚ð‘}“ü‚·‚é
+	insert_string(srcptr, p - srcptr + 1, newstr);
+
+	result = 1;
+
+error:
+	SetResult(result);
 	return Err;
 }
 
@@ -4299,6 +4378,8 @@
 			Err = TTLStrMatch(); break;
 		case RsvStrRemove:
 			Err = TTLStrRemove(); break;
+		case RsvStrReplace:
+			Err = TTLStrReplace(); break;
 		case RsvStrScan:
 			Err = TTLStrScan(); break;
 		case RsvTestLink:

Modified: trunk/teraterm/ttpmacro/ttmparse.c
===================================================================
--- trunk/teraterm/ttpmacro/ttmparse.c	2010-08-11 11:22:32 UTC (rev 3984)
+++ trunk/teraterm/ttpmacro/ttmparse.c	2010-08-11 14:08:41 UTC (rev 3985)
@@ -292,6 +292,7 @@
 		else if (_stricmp(Str,"strlen")==0) *WordId = RsvStrLen;
 		else if (_stricmp(Str,"strmatch")==0) *WordId = RsvStrMatch;
 		else if (_stricmp(Str,"strremove")==0) *WordId = RsvStrRemove;
+		else if (_stricmp(Str,"strreplace")==0) *WordId = RsvStrReplace;
 		else if (_stricmp(Str,"strscan")==0) *WordId = RsvStrScan;
 		break;
 	case 't':

Modified: trunk/teraterm/ttpmacro/ttmparse.h
===================================================================
--- trunk/teraterm/ttpmacro/ttmparse.h	2010-08-11 11:22:32 UTC (rev 3984)
+++ trunk/teraterm/ttpmacro/ttmparse.h	2010-08-11 14:08:41 UTC (rev 3985)
@@ -197,6 +197,7 @@
 #define RsvFileTruncate 179
 #define RsvStrInsert    180
 #define RsvStrRemove    181
+#define RsvStrReplace   182
 
 #define RsvOperator     1000
 #define RsvBNot         1001



Ttssh2-commit メーリングリストの案内
Back to archive index