October 31, 2003 Re: Library naming convention | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hauke Duden | "Hauke Duden" <H.NS.Duden@gmx.net> wrote in message news:bnunf3$e0l$1@digitaldaemon.com... > And yes, I'm a smart ass too ;). Aren't we all? <g> |
October 31, 2003 replace.exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Wilson | I wrote it eons ago, so it's in C. It's a completely trivial program, but I find myself using it all the time. I'll post the source here. I think a D version would be an improvement; anyone want to give it a go? "Matthew Wilson" <matthew-hat@-stlsoft-dot.-org> wrote in message news:bnuo8v$f68$1@digitaldaemon.com... > Is it written in D? > > "Walter" <walter@digitalmars.com> wrote in message news:bnun89$dpa$2@digitaldaemon.com... > > I'll include it (replace.exe) in the next release. ------------------------------------------------------------------- /*_ replace.c Mon Nov 21 1988 Modified by: Walter Bright */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <dos.h> EXPAND_WILDCARDS; #include "filespec.h" #define STRMAX 80 char instr[STRMAX + 1], newstr[STRMAX + 1], oldstr[STRMAX + 1]; char *ptrd, *ptri, *strptr; FILE *fpin, *fpout; unsigned long int strcount; int cc; main(argc, argv) int argc; char *argv[]; { int i; int result = 0; unsigned len; char *sourcename,*backupname; if(argc < 2) { fputs ("Usage:\n\treplace filename(s)", stdout); exit (1); } fputs("String to be replaced (80 characters max)?\n",stdout); fgets(oldstr, STRMAX + 1, stdin); fputs("Replacement string?\n", stdout); fgets(newstr, STRMAX + 1, stdin); /* Kill the CR and LF at the end of the strings. */ for(ptrd = newstr; *ptrd; ptrd++) { if(*ptrd == '\r' || *ptrd == '\n' ) *ptrd = '\0'; } for(ptrd = oldstr; *ptrd; ptrd++) { if(*ptrd == '\r' || *ptrd == '\n') *ptrd = '\0'; } strcount = 0L; for (i = 1; i < argc; i++) { char *tempname; sourcename = argv[i]; fputs("File ",stdout); fputs(sourcename,stdout); fputc('\n',stdout); tempname = filespecforceext(sourcename,"tmp"); backupname = filespecforceext(sourcename,"bak"); fpout = fopen(tempname, "w"); if (!fpout) { fputs("cannot open ",stdout); fputs(sourcename, stdout); fputc('\n',stdout); continue; } fpin = fopen(sourcename, "r"); if (!fpin) { fputs("cannot open ",stdout); fputs(backupname, stdout); fputc('\n',stdout); fclose (fpout); continue; } if (replace()) { unlink (backupname); /* Delete any existing backup. */ if (rename (sourcename, backupname) == -1 || rename (tempname, sourcename) == -1) { fputs ("File rename error with file '", stdout); fputs (sourcename, stdout); fputs ("'\n", stdout); } } else { fputs("No changes\n",stdout); unlink(tempname); } free(backupname); free(tempname); outascii (strcount); fputs (" strings replaced\n", stdout); strcount = 0L; } return result; } outascii (n) unsigned long int n; { char outstr[16], *ptr; outstr[15] = '\0'; ptr = &outstr[14]; do { *ptr-- = (char)('0' + n % 10); n /= 10; } while (n); fputs (ptr + 1, stdout); } /******************************* * Does the actual replacement. * Returns: * 0 error or no changes made */ int replace () { int sflag; if (*oldstr) /* If there is a char, search for strings. */ { for (*instr = '\0', sflag= strcheck(); sflag >= 0; sflag= strcheck() ) { /* Compare the two strings for a match. If it doesn't fully match then shift the input string over and continue (case 0). If it fully matches then replace the string (case 1). If EOF occurs then exit (case -1). */ if (sflag) { *instr = '\0'; if (fputs (newstr, fpout) == EOF) { fputs("Error writing file\n",stdout); goto err; } strcount++; } else { if (putc (*instr, fpout) == EOF) { fputs("Error writing file\n",stdout); goto err; } /* Shift the string. */ for (strptr = instr, ptri = instr + 1; *ptri; *strptr++ = *ptri++); *strptr = '\0'; } } } if (fclose(fpin) == EOF || fclose(fpout) == EOF) { fputs("Error closing files\n",stdout); goto err; } return strcount != 0; err: return 0; } /**************************************** * Inputs characters to the input string, checks the input string for a match * against the dead string. */ strcheck() { ptrd = oldstr; ptri = instr; while( *ptri == *ptrd && *ptrd != '\0' ) { ptrd++; ptri++; } if( *ptri == '\0' && *ptrd != '\0' ) { while( (cc = getc( fpin )) != EOF && cc == *ptrd && *++ptrd != '\0' ) *ptri++ = cc; if( cc == EOF ) { *ptri++ = '\0'; return -1; } *ptri++ = cc; *ptri++ = '\0'; } if( *ptrd != '\0' ) return 0; /* Doesn't fully match the input string. */ else return 1; /* Full match. */ } #define MEM_H #define mem_malloc malloc #define mem_strdup strdup #define mem_free free #define mem_calloc(n) calloc(n,1) #include "filespec.c" |
October 31, 2003 Re: Library naming convention | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hauke Duden | "Hauke Duden" <H.NS.Duden@gmx.net> wrote in message news:bnup38$g4e$1@digitaldaemon.com... > Walter wrote: > >>Since C functions require different handling than D functions I would like to import such modules in a way so that I have to explicitly type std.c.func() instead of func(). Just as a reminder to myself and the people reading my code that one has to take care how to use them. > >> > >>This would be especially important for string functions, I expect, since > >> forgetting to add the null-terminator can cause a crash. > >> > >>Any way to do this? > > > > > > No, there currently isn't a way to do that. BTW, import doesn't exactly add > > a symbol to the current namespace, it just adds a search of the import module if the name isn't already in the current namespace. In other words, > > any names in the current namespace override imported names. > > > > Hmm, that does suggest a way to do it ... define a bunch of dummy symbols > > with the same name, then anyone using the import will be forced to qualify > > it! > > You mean like this? > > import std.c.stdio; > int printf; > int scanf; > int fopen; > int fread; > [snipped about a hundred further definitions] > > Every time I import std.c.stdio? That isn't really practical ;). I assumed Walter was talking about internal to the compiler. It would indeed be worrying if he meant your interpretation. Big W, say it ain't so! |
November 01, 2003 Re: Library naming convention | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hauke Duden | "Hauke Duden" <H.NS.Duden@gmx.net> wrote in message news:bnup38$g4e$1@digitaldaemon.com... > > Hmm, that does suggest a way to do it ... define a bunch of dummy symbols > > with the same name, then anyone using the import will be forced to qualify > > it! > > You mean like this? > > import std.c.stdio; > int printf; > int scanf; > int fopen; > int fread; > [snipped about a hundred further definitions] > > Every time I import std.c.stdio? That isn't really practical ;). I know. It's just a thought. > I know you're probably sick of all these feature requests, but don't you think there should be an EASY way to do such an import? > > Maybe something like > > "use std.c.stdio;" / "using std.c.stdio" > > or > > "reference std.c.stdio;" > > or > > "access std.c.stdio;" > > which would mean that the current module accesses the specified module, but the symbols are not "imported" and must be references using fully qualified names. I understand what you're looking for. I'm not ready to implement that yet, though. Too many other things first! > > Maybe this could also be done by adding a new feature to import. Like the ability to specify an alias for the imported module's namespace. > > For example: > > import std.c.stdio as c; > > If the "as" part is left out the symbols are added to the current namespace (or, as you put it, an automatic search for that namespace is added to the current namespace). Otherwise they are added to a namespace with the specified name. So one would have to write "c.printf" instead of just "printf", which is just what I want: it is obvious that a C function is called, so I am reminded to zero-terminate strings, for example. > > This could also be useful to "flatten" deep module hierarchies. If one uses two modules that define the same symbols, one has to use fully qualified names, right? With such an "import as" you could do the following: > > import foo.bar.flobbox.windows.henry as henry; > import foo_version2.bar.flobbox.windows.henry as henry2; > > Then you could write > > henry.someFunc(); > and > henry2.someFunc(); > > instead of > > foo.bar.flobbox.windows.henry.someFunc(); > foo_version2.bar.flobbox.windows.henry.someFunc(); > > Could come in handy... The alias already works for module names: import foo.bar.abc; alias foo.bar.abc xyz; ... xyz.func(); |
November 01, 2003 Re: replace.exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Sure ill go for it, is their an ETA for .75 ? I ask because of the private imports, and some other changes requested for phobos that we'd like to implement, but if .75 is available soon ill just wait. C "Walter" <walter@digitalmars.com> wrote in message news:bnupcl$gjs$1@digitaldaemon.com... > I wrote it eons ago, so it's in C. It's a completely trivial program, but I > find myself using it all the time. I'll post the source here. I think a D version would be an improvement; anyone want to give it a go? > > "Matthew Wilson" <matthew-hat@-stlsoft-dot.-org> wrote in message news:bnuo8v$f68$1@digitaldaemon.com... > > Is it written in D? > > > > "Walter" <walter@digitalmars.com> wrote in message news:bnun89$dpa$2@digitaldaemon.com... > > > I'll include it (replace.exe) in the next release. > > ------------------------------------------------------------------- > /*_ replace.c Mon Nov 21 1988 Modified by: Walter Bright */ > > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > #include <dos.h> > > EXPAND_WILDCARDS; > > #include "filespec.h" > > #define STRMAX 80 > char instr[STRMAX + 1], newstr[STRMAX + 1], oldstr[STRMAX + 1]; > char *ptrd, *ptri, *strptr; > FILE *fpin, *fpout; > unsigned long int strcount; > int cc; > > main(argc, argv) > int argc; > char *argv[]; > { > int i; > int result = 0; > unsigned len; > char *sourcename,*backupname; > > if(argc < 2) > { > fputs ("Usage:\n\treplace filename(s)", stdout); > exit (1); > } > fputs("String to be replaced (80 characters max)?\n",stdout); > fgets(oldstr, STRMAX + 1, stdin); > fputs("Replacement string?\n", stdout); > fgets(newstr, STRMAX + 1, stdin); > > /* Kill the CR and LF at the end of the strings. */ > for(ptrd = newstr; *ptrd; ptrd++) > { > if(*ptrd == '\r' || *ptrd == '\n' ) > *ptrd = '\0'; > } > for(ptrd = oldstr; *ptrd; ptrd++) > { > if(*ptrd == '\r' || *ptrd == '\n') > *ptrd = '\0'; > } > strcount = 0L; > for (i = 1; i < argc; i++) > { char *tempname; > > sourcename = argv[i]; > fputs("File ",stdout); > fputs(sourcename,stdout); > fputc('\n',stdout); > > tempname = filespecforceext(sourcename,"tmp"); > backupname = filespecforceext(sourcename,"bak"); > > fpout = fopen(tempname, "w"); > if (!fpout) > { fputs("cannot open ",stdout); > fputs(sourcename, stdout); > fputc('\n',stdout); > continue; > } > fpin = fopen(sourcename, "r"); > if (!fpin) > { fputs("cannot open ",stdout); > fputs(backupname, stdout); > fputc('\n',stdout); > fclose (fpout); > continue; > } > if (replace()) > { > unlink (backupname); /* Delete any existing backup. */ > if (rename (sourcename, backupname) == -1 || > rename (tempname, sourcename) == -1) > { > fputs ("File rename error with file '", stdout); > fputs (sourcename, stdout); > fputs ("'\n", stdout); > } > } > else > { fputs("No changes\n",stdout); > unlink(tempname); > } > free(backupname); > free(tempname); > outascii (strcount); > fputs (" strings replaced\n", stdout); > strcount = 0L; > } > return result; > } > > outascii (n) > unsigned long int n; > { > char outstr[16], *ptr; > outstr[15] = '\0'; > ptr = &outstr[14]; > do > { > *ptr-- = (char)('0' + n % 10); > n /= 10; > } while (n); > fputs (ptr + 1, stdout); > } > > /******************************* > * Does the actual replacement. > * Returns: > * 0 error or no changes made > */ > > int replace () > { > int sflag; > > if (*oldstr) /* If there is a char, search for strings. */ > { > for (*instr = '\0', sflag= strcheck(); sflag >= 0; sflag= strcheck() ) > { > /* Compare the two strings for a match. If it doesn't fully match then > shift > the input string over and continue (case 0). If it fully matches then > replace > the string (case 1). If EOF occurs then exit (case -1). */ > > if (sflag) > { > *instr = '\0'; > if (fputs (newstr, fpout) == EOF) > { fputs("Error writing file\n",stdout); > goto err; > } > strcount++; > } > else > { > if (putc (*instr, fpout) == EOF) > { fputs("Error writing file\n",stdout); > goto err; > } > /* Shift the string. */ > for (strptr = instr, ptri = instr + 1; *ptri; *strptr++ = *ptri++); > *strptr = '\0'; > } > } > } > if (fclose(fpin) == EOF || fclose(fpout) == EOF) > { fputs("Error closing files\n",stdout); > goto err; > } > return strcount != 0; > > err: > return 0; > } > > /**************************************** > * Inputs characters to the input string, checks the input string for a > match > * against the dead string. > */ > > strcheck() > { > ptrd = oldstr; > ptri = instr; > while( *ptri == *ptrd && *ptrd != '\0' ) > { > ptrd++; > ptri++; > } > if( *ptri == '\0' && *ptrd != '\0' ) > { > while( (cc = getc( fpin )) != EOF && cc == *ptrd && *++ptrd != '\0' ) > *ptri++ = cc; > if( cc == EOF ) > { > *ptri++ = '\0'; > return -1; > } > *ptri++ = cc; > *ptri++ = '\0'; > } > if( *ptrd != '\0' ) > return 0; /* Doesn't fully match the input string. */ > else > return 1; /* Full match. */ > } > > #define MEM_H > #define mem_malloc malloc > #define mem_strdup strdup > #define mem_free free > #define mem_calloc(n) calloc(n,1) > > #include "filespec.c" > > > > |
November 01, 2003 Re: replace.exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 1988 lol! man that is old. ( puttin on I think we're alone now : Tiffany ) C "Walter" <walter@digitalmars.com> wrote in message news:bnupcl$gjs$1@digitaldaemon.com... > I wrote it eons ago, so it's in C. It's a completely trivial program, but I > find myself using it all the time. I'll post the source here. I think a D version would be an improvement; anyone want to give it a go? > > "Matthew Wilson" <matthew-hat@-stlsoft-dot.-org> wrote in message news:bnuo8v$f68$1@digitaldaemon.com... > > Is it written in D? > > > > "Walter" <walter@digitalmars.com> wrote in message news:bnun89$dpa$2@digitaldaemon.com... > > > I'll include it (replace.exe) in the next release. > > ------------------------------------------------------------------- > /*_ replace.c Mon Nov 21 1988 Modified by: Walter Bright */ > > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > #include <dos.h> > > EXPAND_WILDCARDS; > > #include "filespec.h" > > #define STRMAX 80 > char instr[STRMAX + 1], newstr[STRMAX + 1], oldstr[STRMAX + 1]; > char *ptrd, *ptri, *strptr; > FILE *fpin, *fpout; > unsigned long int strcount; > int cc; > > main(argc, argv) > int argc; > char *argv[]; > { > int i; > int result = 0; > unsigned len; > char *sourcename,*backupname; > > if(argc < 2) > { > fputs ("Usage:\n\treplace filename(s)", stdout); > exit (1); > } > fputs("String to be replaced (80 characters max)?\n",stdout); > fgets(oldstr, STRMAX + 1, stdin); > fputs("Replacement string?\n", stdout); > fgets(newstr, STRMAX + 1, stdin); > > /* Kill the CR and LF at the end of the strings. */ > for(ptrd = newstr; *ptrd; ptrd++) > { > if(*ptrd == '\r' || *ptrd == '\n' ) > *ptrd = '\0'; > } > for(ptrd = oldstr; *ptrd; ptrd++) > { > if(*ptrd == '\r' || *ptrd == '\n') > *ptrd = '\0'; > } > strcount = 0L; > for (i = 1; i < argc; i++) > { char *tempname; > > sourcename = argv[i]; > fputs("File ",stdout); > fputs(sourcename,stdout); > fputc('\n',stdout); > > tempname = filespecforceext(sourcename,"tmp"); > backupname = filespecforceext(sourcename,"bak"); > > fpout = fopen(tempname, "w"); > if (!fpout) > { fputs("cannot open ",stdout); > fputs(sourcename, stdout); > fputc('\n',stdout); > continue; > } > fpin = fopen(sourcename, "r"); > if (!fpin) > { fputs("cannot open ",stdout); > fputs(backupname, stdout); > fputc('\n',stdout); > fclose (fpout); > continue; > } > if (replace()) > { > unlink (backupname); /* Delete any existing backup. */ > if (rename (sourcename, backupname) == -1 || > rename (tempname, sourcename) == -1) > { > fputs ("File rename error with file '", stdout); > fputs (sourcename, stdout); > fputs ("'\n", stdout); > } > } > else > { fputs("No changes\n",stdout); > unlink(tempname); > } > free(backupname); > free(tempname); > outascii (strcount); > fputs (" strings replaced\n", stdout); > strcount = 0L; > } > return result; > } > > outascii (n) > unsigned long int n; > { > char outstr[16], *ptr; > outstr[15] = '\0'; > ptr = &outstr[14]; > do > { > *ptr-- = (char)('0' + n % 10); > n /= 10; > } while (n); > fputs (ptr + 1, stdout); > } > > /******************************* > * Does the actual replacement. > * Returns: > * 0 error or no changes made > */ > > int replace () > { > int sflag; > > if (*oldstr) /* If there is a char, search for strings. */ > { > for (*instr = '\0', sflag= strcheck(); sflag >= 0; sflag= strcheck() ) > { > /* Compare the two strings for a match. If it doesn't fully match then > shift > the input string over and continue (case 0). If it fully matches then > replace > the string (case 1). If EOF occurs then exit (case -1). */ > > if (sflag) > { > *instr = '\0'; > if (fputs (newstr, fpout) == EOF) > { fputs("Error writing file\n",stdout); > goto err; > } > strcount++; > } > else > { > if (putc (*instr, fpout) == EOF) > { fputs("Error writing file\n",stdout); > goto err; > } > /* Shift the string. */ > for (strptr = instr, ptri = instr + 1; *ptri; *strptr++ = *ptri++); > *strptr = '\0'; > } > } > } > if (fclose(fpin) == EOF || fclose(fpout) == EOF) > { fputs("Error closing files\n",stdout); > goto err; > } > return strcount != 0; > > err: > return 0; > } > > /**************************************** > * Inputs characters to the input string, checks the input string for a > match > * against the dead string. > */ > > strcheck() > { > ptrd = oldstr; > ptri = instr; > while( *ptri == *ptrd && *ptrd != '\0' ) > { > ptrd++; > ptri++; > } > if( *ptri == '\0' && *ptrd != '\0' ) > { > while( (cc = getc( fpin )) != EOF && cc == *ptrd && *++ptrd != '\0' ) > *ptri++ = cc; > if( cc == EOF ) > { > *ptri++ = '\0'; > return -1; > } > *ptri++ = cc; > *ptri++ = '\0'; > } > if( *ptrd != '\0' ) > return 0; /* Doesn't fully match the input string. */ > else > return 1; /* Full match. */ > } > > #define MEM_H > #define mem_malloc malloc > #define mem_strdup strdup > #define mem_free free > #define mem_calloc(n) calloc(n,1) > > #include "filespec.c" > > > > |
November 01, 2003 Re: replace.exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | "Charles Sanders" <sanders-consulting@comcast.net> wrote in message news:bnv2su$ths$1@digitaldaemon.com... > 1988 lol! man that is old. ( puttin on I think we're alone now : Tiffany ) > > C > lol...now that is classic. |
November 01, 2003 Re: replace.exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | "Charles Sanders" <sanders-consulting@comcast.net> wrote in message news:bnv2il$t3j$1@digitaldaemon.com... > Sure ill go for it, is their an ETA for .75 ? I ask because of the private > imports, and some other changes requested for phobos that we'd like to implement, but if .75 is available soon ill just wait. I hope to get it done within a week. |
November 01, 2003 Re: replace.exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | "Charles Sanders" <sanders-consulting@comcast.net> wrote in message news:bnv2su$ths$1@digitaldaemon.com... > 1988 lol! man that is old. ( puttin on I think we're alone now : Tiffany ) If you want to see some really old stuff I wrote, check out www.classicempire.com <g>. I think some of my code has wound up in the PDP-10 museum. |
November 01, 2003 Re: Library naming convention | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > > "Benji Smith" <dlanguage@xxagg.com> wrote in message news:3qf5qv066p5295hqfbo0isgh1c8aeg101f@4ax.com... > > On Thu, 30 Oct 2003 20:49:12 -0800, "Walter" <walter@digitalmars.com> wrote: > > >Here's what I did. First, I did a simple module with declarations to make the C zlib functions accessible from D. It makes no attempt to hide > zlib's C > > >conventions. This will not be an official D package, but is still useful, > so > > >I stuffed it in etc.c.zlib. Then I build a D version that implements a conventional D interface to the zlib functionality. I called this > std.zlib. > > >Now, it works out that std.zlib is implemented by calling the functions > in > > >etc.c.zlib, but that is quite irrelevant to (and hidden from) the user of > > >std.zlib. > > This is a big red flag to me. The standard library (std.zlib) is dependant upon something outside of the standard library (etc.c.zlib). As far as I'm concerned, the standard library should be entirely self-contained. > > In general, you're right. But in some cases the implementation should be hidden from the user. The user of std.zlib is not dependent on it. > > > It seems to me that if a standard library module needs to depend on a c module, it should be something in std.c rather than in etc.c (maybe we need both std.c and etc.c). > > I don't think it's necessary for the implementation of it to be in std. But then you talk and build a bigger entity than std. This I called dlib, that which incloses all modules that are downloaded with the standard distribution. -- Helmut Leitner leitner@hls.via.at Graz, Austria www.hls-software.com |
Copyright © 1999-2021 by the D Language Foundation