|
View previous topic :: View next topic
|
| Author |
Message |
selvamsrinivasan85
New User

Joined: 09 Aug 2010 Posts: 36 Location: Chennai
|
|
|
|
Hello friends,
I am in a need of creating a String function that has to replace SASC predefined memxlt() and xltable() functions.
Description:
memxlt() -
void *memxlt(void *blk, const char *table, size_t n);
- memxlt translates a block of memory from one character set to another. The first argument ( blk ) is the address of the area of memory to be translated, and the third argument ( n ) is the number of characters to be translated. The second argument ( table ) is a pointer to a 256-byte translate table, which should be defined so that table[c] for any character c is the value to which c should be translated. (The function xltable is frequently used to build such a table.)
Note:
The argument string is translated in place; that is, each character in the string is replaced by a translated character.
Return value:
memxlt returns a pointer to the translated string.
Caution:
The third argument to memxlt is size_t . If a negative number is passed, massive overlaying of memory occurs.
The effect of memxlt is not defined if the source string and the translate table overlap.
e.g:
Code:
#include <lcstring.h>
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
int len, i, j;
char a;
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char table[256];
if (argc < 2) {
puts("Specify the secret word on the command line.");
exit(4);
}
len = strlen(argv[1] );
memupr(argv[1], len); /* Uppercase input message. */
/* Randomize the alphabet. */
for (i = 0; i < 26; i++)
for (j = i + 1; j < 26; j++)
if (rand() % 2) {
a = alphabet[i];
alphabet[i] = alphabet[j] ;
alphabet[j] = a;
}
/* Build a translate table. */
xltable(table,"ABCDEFGHIJKLMNOPQRSTUVWXYZ",alphabet);
/* Translate message. */
memxlt(argv[1],table,len);
/* Print message. */
printf("Today's secret word is: \n%s\n",argv[1]);
return;
}
xltable() -
char *xltable(char table[256], char *source, char *target);
Description:
xltable builds a translation table that you can use later as an argument to the memxlt or strxlt function.
The argument table is a 256-character array, in which the translation table is to be built. The second argument ( source ) is a string of characters that the table is to translate, and the third argument ( target ) is a string containing the characters to which the source characters are to be translated, in the same order. The source and target strings should contain the same number of characters; if they do not, the extra characters of the longer string are ignored.
You can also specify a table address of 0. In this case, xltable builds the table in a static area and returns the address of this area. This area may be overlaid by the next call to xltable , strscntb , or memscntb .
The table built by xltable translates any character not present in the source string to itself, so these characters are not changed when using the table.
Please let me know if you need any more details. |
|
| Back to top |
|
 |
PeterHolland
Global Moderator

Joined: 27 Oct 2009 Posts: 2481 Location: Netherlands, Amstelveen
|
|
|
|
| And the question is ? |
|
| Back to top |
|
 |
selvamsrinivasan85
New User

Joined: 09 Aug 2010 Posts: 36 Location: Chennai
|
|
|
|
Hi,
The question is can anyone help to create a user-defined C function for the above pre-defined SASC functions, as we replacing a SASC compiler with IBM/C compiler. |
|
| Back to top |
|
 |
enrico-sorichetti
Superior Member

Joined: 14 Mar 2007 Posts: 10903 Location: italy
|
|
|
|
what kind of help ??
where are You facing issues...
the coding or the logic?
neither looks overly complicated to my warped mind |
|
| Back to top |
|
 |
selvamsrinivasan85
New User

Joined: 09 Aug 2010 Posts: 36 Location: Chennai
|
|
|
|
Hi enrico,
We have coded functions for replacing those SASC built-in functions. the issue we are facing is only one line is getting printed on the file.
For e.g, SASC compiler run has below format of output
1C2AACD201010010802001 - Header Info.
1C2AACDNYRG0001 - Trailer Info
....
1C2AACDNYRG9999 - Trailer Info
But using IBM/C compiler, we are getting only header info.
Code is given below for your reference,
char_map replaces the SASC function xltable:
*********************************************
char *char_map(char tableÝ256¨ , char *source, char *target) {
char *new_table = table; /* Pointer to the table we are returning *
char *tgt; /* Pointer to where the target data starts */
int i;
/* Initialize the array we are returning with standard ascii */
for (i = 0; i < 256; i++)
new_tableÝi¨ = i % 128;
/* The middle of the Array is where we keep target */
tgt = &new_tableÝ128¨ ;
/* Store the data that was passed in to our new table until one
(or both) of the string(s) end(s) */
while (*source != '\0' && *target != '\0') {
tgtÝ*source¨ = * target;
source++;
target++;
}
return new_table;
}
char_trans replaces the SASC function memxlt:
*********************************************
char *char_trans(void *blk, const char *table, size_t n) {
char *ret_val = blk;
int i;
int tmp=0;
for (i = 0; i < n; i++) {
tmp = ret_valÝi¨ + 128;
ret_valÝi¨ = tableÝtmp¨ ;
}
return ret_val;
} |
|
| Back to top |
|
 |
enrico-sorichetti
Superior Member

Joined: 14 Mar 2007 Posts: 10903 Location: italy
|
|
|
|
the last post was completely obfuscated by the char translation
frankly I do not see how a <wrong> translate might result in a missing record
also the terminology is confusing...
since I was not able to understand anything I found simpler to rewrite everything from scratch
| Code: |
#include <stdio.h>
#include <string.h>
#define XTSIZE 256
char * xtable(unsigned char *tb, unsigned char *in, unsigned char *ou)
{
int i;
if ( strlen(in) != strlen(ou) )
return NULL ;
for ( i = 0; i < XTSIZE; i++)
tb[i] = (unsigned char) i;
for ( i = 0; i < strlen(in); i++)
tb[in[i]] = ou[i] ;
return tb;
}
void * xlate(unsigned char *in, unsigned char *tb, int l)
{
int i;
for ( i = 0; i < l; i++)
in[i] = tb[in[i]] ;
return in ;
}
main(int argc, char **argv)
{
unsigned char tb[XTSIZE] ;
unsigned char in[] = "ABCDEFG" ;
if ( xtable(tb,"ABCDEFG","abcdefg") )
{
printf("table build success\n");
printf("before ==>%s<==\n",in);
if ( xlate(in,tb,strlen(in) ) )
{
printf("xlate success\n");
printf("after ==>%s<==\n",in);
}
else
printf("xlate failure\n");
}
else
printf("table build failure\n");
}
|
TESTED AND WORKING |
|
| Back to top |
|
 |
selvamsrinivasan85
New User

Joined: 09 Aug 2010 Posts: 36 Location: Chennai
|
|
|
|
Hi enrico,
Thanks for the help. But still facing the same issue (Only header records are printing correctly) . |
|
| Back to top |
|
 |
enrico-sorichetti
Superior Member

Joined: 14 Mar 2007 Posts: 10903 Location: italy
|
|
|
|
as I said before it is hard to believe that it could be a translate issue
( if the translate stuff has been reasonably tested ) |
|
| Back to top |
|
 |
|
|
 |
All times are GMT + 6 Hours |
|