Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #45607 > unrolled thread

Challenge: tightest code to find-replace a string

Started byDFS <nospam@dfs.com>
First post2014-06-06 00:08 -0400
Last post2014-06-14 13:12 -0700
Articles 10 on this page of 70 — 18 participants

Back to article view | Back to comp.lang.c


Contents

  Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-06 00:08 -0400
    Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-06 04:34 -0400
      Re: Challenge: tightest code to find-replace a string Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-06-06 10:04 +0000
      Re: Challenge: tightest code to find-replace a string Mark Storkamp <mstorkamp@yahoo.com> - 2014-06-06 07:27 -0500
      Re: Challenge: tightest code to find-replace a string Robert Wessel <robertwessel2@yahoo.com> - 2014-06-06 11:55 -0500
        Re: Challenge: tightest code to find-replace a string Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-06-06 20:47 +0200
    Re: Challenge: tightest code to find-replace a string Ike Naar <ike@iceland.freeshell.org> - 2014-06-06 06:05 +0000
      Re: Challenge: tightest code to find-replace a string Noob <root@127.0.0.1> - 2014-06-06 10:17 +0200
        Re: Challenge: tightest code to find-replace a string Ian Collins <ian-news@hotmail.com> - 2014-06-08 21:20 +1200
    Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 14:41 +0100
      Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-06 16:29 +0100
        Re: Challenge: tightest code to find-replace a string Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-06-06 17:27 +0000
        Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 22:16 +0100
          Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-06 23:26 +0100
            Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 23:38 +0100
      Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-06 08:31 -0700
        Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 22:12 +0100
      Re: Challenge: tightest code to find-replace a string Chad <cdalten@gmail.com> - 2014-06-07 12:19 -0700
        Re: Challenge: tightest code to find-replace a string Chad <cdalten@gmail.com> - 2014-06-07 13:38 -0700
          Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-08 10:34 +0000
      Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-07 18:30 -0400
        Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-08 13:09 +0100
          Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-08 12:42 -0400
            Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 01:44 +0100
              Re: Challenge: tightest code to find-replace a string Siri Crews <chine.bleu@yahoo.com> - 2014-06-08 22:15 -0700
                Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 12:58 +0100
              Re: Challenge: tightest code to find-replace a string Ike Naar <ike@iceland.freeshell.org> - 2014-06-09 06:40 +0000
                Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 13:04 +0100
                  Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 15:27 +0100
                    Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 16:22 +0100
                      Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 18:06 +0100
                      Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 22:38 +0100
        Re: Challenge: tightest code to find-replace a string Tim Rentsch <txr@alumni.caltech.edu> - 2014-06-10 00:57 -0700
    Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-07 17:36 -0400
      Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-08 09:57 +0100
        Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-08 12:43 -0400
          Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-08 15:27 -0700
          Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 10:02 +0100
            Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 08:03 -0700
      Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-08 04:43 -0700
        Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-08 13:26 -0400
        Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-08 15:24 -0700
          Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 08:39 -0700
            Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 08:57 -0700
              Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 09:52 -0700
                Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 11:25 -0700
                  Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 13:42 -0700
                    Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 15:02 -0700
                    Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 16:04 -0700
                      Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-10 10:23 +0000
            Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-09 16:57 +0000
              Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-10 10:06 +0000
    Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-09 12:43 -0700
      Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-09 14:32 -0700
        Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-09 16:09 -0700
        Re: Challenge: tightest code to find-replace a string James Kuyper <jameskuyper@verizon.net> - 2014-06-09 21:35 -0400
      Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-10 09:39 -0400
        Re: Challenge: tightest code to find-replace a string James Kuyper <jameskuyper@verizon.net> - 2014-06-10 11:26 -0400
        Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-11 16:16 +0000
        Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-11 09:25 -0700
          Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-11 13:48 -0700
            Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-11 22:04 +0100
              Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-11 14:59 -0700
            Re: Challenge: tightest code to find-replace a string Ike Naar <ike@iceland.freeshell.org> - 2014-06-11 21:35 +0000
            Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-11 14:56 -0700
              Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-12 02:24 -0700
                Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-12 09:09 -0700
                  Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-13 00:40 -0700
                    Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-13 09:17 +0100
    Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-14 13:12 -0700

Page 4 of 4 — ← Prev page 1 2 3 [4]


#45809

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2014-06-11 13:48 -0700
Message-ID<b675676e-a941-4dc2-8b80-1c2facd63a67@googlegroups.com>
In reply to#45797
On Wednesday, June 11, 2014 5:25:44 PM UTC+1, Keith Thompson wrote:
> 
> My common sense as a developer tells me that I need an unambiguous
> specification.  If I can't get one, I'll probably write one myself
> and verify that it's consistent with what's actually needed.
> 
If someone instructed you to go through  a text, and replace every
instance of "her" with "him", how would you respond?

[toc] | [prev] | [next] | [standalone]


#45813

From"BartC" <bc@freeuk.com>
Date2014-06-11 22:04 +0100
Message-ID<fw3mv.331405$B%3.18532@fx23.am4>
In reply to#45809

"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message 
news:b675676e-a941-4dc2-8b80-1c2facd63a67@googlegroups.com...
> On Wednesday, June 11, 2014 5:25:44 PM UTC+1, Keith Thompson wrote:
>>
>> My common sense as a developer tells me that I need an unambiguous
>> specification.  If I can't get one, I'll probably write one myself
>> and verify that it's consistent with what's actually needed.
>>
> If someone instructed you to go through  a text, and replace every
> instance of "her" with "him", how would you respond?

I would probably change "I told her so" to "I told him so".

I wouldn't change "There are several herds" to "Thime are several himds".

I would have to think a little about changing "Here was her coat" to "Here 
was him coat" (but would not be tempted to write "Hime was him coat".

(I would probably also change whole words of "her", "Her" and "HER" to 
"him", "Him" and "HIM" respectively.)

So yes, when presenting a challenge, you do have to spell these things out. 
(And also why when I re-stated this problem, I talked about byte-sequences 
rather than text.)

-- 
bartc 

[toc] | [prev] | [next] | [standalone]


#45818

FromKeith Thompson <kst-u@mib.org>
Date2014-06-11 14:59 -0700
Message-ID<lnbntzkszs.fsf@nuthaus.mib.org>
In reply to#45813
"BartC" <bc@freeuk.com> writes:
> "Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message 
> news:b675676e-a941-4dc2-8b80-1c2facd63a67@googlegroups.com...
>> On Wednesday, June 11, 2014 5:25:44 PM UTC+1, Keith Thompson wrote:
>>>
>>> My common sense as a developer tells me that I need an unambiguous
>>> specification.  If I can't get one, I'll probably write one myself
>>> and verify that it's consistent with what's actually needed.
>>>
>> If someone instructed you to go through  a text, and replace every
>> instance of "her" with "him", how would you respond?
>
> I would probably change "I told her so" to "I told him so".
>
> I wouldn't change "There are several herds" to "Thime are several himds".
>
> I would have to think a little about changing "Here was her coat" to "Here 
> was him coat" (but would not be tempted to write "Hime was him coat".
>
> (I would probably also change whole words of "her", "Her" and "HER" to 
> "him", "Him" and "HIM" respectively.)
>
> So yes, when presenting a challenge, you do have to spell these things out. 
> (And also why when I re-stated this problem, I talked about byte-sequences 
> rather than text.)

And if the actual intent is to implement C code that performs the
equivalent of

    sed 's/him/her/g'

then most of these assumptions would be wrong.  This is why we need
context.  Even with perfectly precise requirements, knowing the actual
goal can help both in understanding the requirements and in judging
whether they're written correctly.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#45815

FromIke Naar <ike@iceland.freeshell.org>
Date2014-06-11 21:35 +0000
Message-ID<slrn3vfslphis0.9fr.ike@iceland.freeshell.org>
In reply to#45809
On 2014-06-11, Malcolm McLean <malcolm.mclean5@btinternet.com> wrote:
> On Wednesday, June 11, 2014 5:25:44 PM UTC+1, Keith Thompson wrote:
>> 
>> My common sense as a developer tells me that I need an unambiguous
>> specification.  If I can't get one, I'll probably write one myself
>> and verify that it's consistent with what's actually needed.
>> 
> If someone instructed you to go through  a text, and replace every
> instance of "her" with "him", how would you respond?

"Whime you crazy?"

[toc] | [prev] | [next] | [standalone]


#45817

FromKeith Thompson <kst-u@mib.org>
Date2014-06-11 14:56 -0700
Message-ID<lnfvjbkt51.fsf@nuthaus.mib.org>
In reply to#45809
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Wednesday, June 11, 2014 5:25:44 PM UTC+1, Keith Thompson wrote:
>> My common sense as a developer tells me that I need an unambiguous
>> specification.  If I can't get one, I'll probably write one myself
>> and verify that it's consistent with what's actually needed.
>> 
> If someone instructed you to go through  a text, and replace every
> instance of "her" with "him", how would you respond?

I'd ask whethim thime are any bothimsome or othimwise incohiment special
cases to consider.

Possibly the context of the request would clarify whether
occurrences of "her" within a word (and what exactly is a
"word"?) should or shouldn't be replaced, and whether "Her" and
"HER" should be replaced by "Him" and "HIM", respectively.  If the
intent is to replace feminine pronouns by masculine, I'd have to do
more analysis to determine whether "her" is possesive (and should
be replaced by "his") or not (and should be replaced by "him").
Given the complexity of English grammar, I woudln't be surprised
if there are cases that are inherently ambiguous.

Your point?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#45843

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2014-06-12 02:24 -0700
Message-ID<af1aa7ec-f4a1-41c8-9063-449852eacae8@googlegroups.com>
In reply to#45817
On Wednesday, June 11, 2014 10:56:10 PM UTC+1, Keith Thompson wrote:
> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>
> 
> > If someone instructed you to go through  a text, and replace every
> > instance of "her" with "him", how would you respond?
>  
> I'd ask whethim thime are any bothimsome or othimwise incohiment special
> cases to consider.
> 
You can't do that and keep a secretarial job. Well actually you maybe can,
it's such an unusual response that a secretary who said that would probably
be treated as a company treasure. But usually, a businessman expects a 
reasonable instruction like that to be executed.
> 
> Possibly the context of the request would clarify whether
> occurrences of "her" within a word (and what exactly is a
> "word"?) should or shouldn't be replaced, and whether "Her" and
> "HER" should be replaced by "Him" and "HIM", respectively.  If the
> intent is to replace feminine pronouns by masculine, I'd have to do
> more analysis to determine whether "her" is possesive (and should
> be replaced by "his") or not (and should be replaced by "him").
> 
> Given the complexity of English grammar, I woudln't be surprised
> if there are cases that are inherently ambiguous.
> 
> Your point?
> 
You need to read the text. It's entirely possible that a blind search/replace
of the sequence "her" by "him" will produce an output everyone would 
agree is the one desired. It's quite likely that a word replacement will
be right.
But it's also likely that no simple program can do the job, but any human
can, for example, "himself" and "herself", or "her" and "his". It's also
likely that there are other gendered words, there comes a point where
you've got to query if, say, "Freda" should be replaced by "Fred". That's
pretty clearly going beyond the instruction. It's hard to say where that
point is.

User requirements are often inherently ambiguous in computer terms.
There is no algorithm that can fulfil the businessman's request.

[toc] | [prev] | [next] | [standalone]


#45862

FromKeith Thompson <kst-u@mib.org>
Date2014-06-12 09:09 -0700
Message-ID<lnr42ujei2.fsf@nuthaus.mib.org>
In reply to#45843
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Wednesday, June 11, 2014 10:56:10 PM UTC+1, Keith Thompson wrote:
>> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>> > If someone instructed you to go through  a text, and replace every
>> > instance of "her" with "him", how would you respond?
>>  
>> I'd ask whethim thime are any bothimsome or othimwise incohiment special
>> cases to consider.
>> 
> You can't do that and keep a secretarial job. Well actually you maybe can,
> it's such an unusual response that a secretary who said that would probably
> be treated as a company treasure. But usually, a businessman expects a 
> reasonable instruction like that to be executed.

If I were given such an instruction in the context of a secretarial
job, that would provide a great deal of information.  You gave no
such context.  I answered as a software developer, which you already
know is what I am.

>> Possibly the context of the request would clarify whether
>> occurrences of "her" within a word (and what exactly is a
>> "word"?) should or shouldn't be replaced, and whether "Her" and
>> "HER" should be replaced by "Him" and "HIM", respectively.  If the
>> intent is to replace feminine pronouns by masculine, I'd have to do
>> more analysis to determine whether "her" is possesive (and should
>> be replaced by "his") or not (and should be replaced by "him").
>> 
>> Given the complexity of English grammar, I woudln't be surprised
>> if there are cases that are inherently ambiguous.
>> 
>> Your point?
>> 
> You need to read the text. It's entirely possible that a blind search/replace
> of the sequence "her" by "him" will produce an output everyone would 
> agree is the one desired. It's quite likely that a word replacement will
> be right.
> But it's also likely that no simple program can do the job, but any human
> can, for example, "himself" and "herself", or "her" and "his". It's also
> likely that there are other gendered words, there comes a point where
> you've got to query if, say, "Freda" should be replaced by "Fred". That's
> pretty clearly going beyond the instruction. It's hard to say where that
> point is.
>
> User requirements are often inherently ambiguous in computer terms.
> There is no algorithm that can fulfil the businessman's request.

Ok, so we seem to be in agreement, now that you've provided some
more context.

But if we're talking about doing search-and-replace within a string
*as a programming problem*, the requirements can and should be
defined unambiguously.  If I'm writing an implementation of sed,
for example, I'll be working from a specification that leaves no
doubt what should happen if there's an overlap.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#45901

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2014-06-13 00:40 -0700
Message-ID<e6207fac-d74d-4985-ae66-b88533acb04c@googlegroups.com>
In reply to#45862
On Thursday, June 12, 2014 5:09:57 PM UTC+1, Keith Thompson wrote:
>
> But if we're talking about doing search-and-replace within a string 
> *as a programming problem*, the requirements can and should be
> defined unambiguously.  If I'm writing an implementation of sed,
> for example, I'll be working from a specification that leaves no
> doubt what should happen if there's an overlap.
> 
Often there's someone who stands between the client and the programmer,
turning business requirements into some sort of formal specification.
You can argue about the wisdom of that paradigm of software development.
But often there isn't.

If you're writing an implementation of sed, you have to be compatible.
Not so for the first sed author.

[toc] | [prev] | [next] | [standalone]


#45902

From"BartC" <bc@freeuk.com>
Date2014-06-13 09:17 +0100
Message-ID<%symv.541574$Sr2.277434@fx08.am4>
In reply to#45901

"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message 
news:e6207fac-d74d-4985-ae66-b88533acb04c@googlegroups.com...
> On Thursday, June 12, 2014 5:09:57 PM UTC+1, Keith Thompson wrote:
>>
>> But if we're talking about doing search-and-replace within a string
>> *as a programming problem*, the requirements can and should be
>> defined unambiguously.  If I'm writing an implementation of sed,
>> for example, I'll be working from a specification that leaves no
>> doubt what should happen if there's an overlap.

> If you're writing an implementation of sed, you have to be compatible.
> Not so for the first sed author.

In the case of this thread, the OP is the first author of the challenge, and 
everyone else needs to write compatible implementations.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#45978

From"Chris M. Thomasson" <no@spam.invalid>
Date2014-06-14 13:12 -0700
Message-ID<lniac1$hdf$1@speranza.aioe.org>
In reply to#45607
> "DFS"  wrote in message news:lmrer6$4l3$1@dont-email.me...

FWIW, check out this thread:

https://groups.google.com/forum/#!topic/comp.lang.c/cUeUlQd38-o/overview


Here was my submission:

https://groups.google.com/forum/#!original/comp.lang.c/cUeUlQd38-o/VlU8pTgRk8EJ

http://pastebin.com/f62c80892

This transforms tab characters into spaces.

____________________________________________________
#include <stdio.h>
#include <stdlib.h>
#include <string.h>




#define TAB_MAX 1024U
#define TAB_DEFAULT 1U




typedef char static_assert[
  TAB_MAX > 0 && TAB_DEFAULT > 0 ? 1 : -1
];




struct detab {
  FILE* in_file;
  FILE* out_file;
  unsigned long int tabs[TAB_MAX + 1];
};




int
detab_startup(
struct detab* const self,
int argc,
char** argv
) {
  if (argc > 1) {
    size_t i, size = 0;
    char* stop = NULL;
    char* cur = argc == 2 ? argv[1] : argv[2];
    while (*cur && size < TAB_MAX) {
      self->tabs[size] = strtoul(cur, &stop, 10);
      if (! self->tabs[size]) self->tabs[size] = TAB_DEFAULT;
      ++size;
      if (! *stop) break;
      cur = stop + 1;
    }
    for (i = size; i < TAB_MAX + 1; ++i) {
      self->tabs[i] = TAB_DEFAULT;
    }
    if (argc > 2) {
      self->in_file = fopen(argv[1], "rt");
      if (! self->in_file) {
        fprintf(stderr, "could not open the [in] file!\n");
        return 0;
      }
    } else {
      self->in_file = stdin;
    }
    self->out_file = stdout;
    return 1;
  } else {
    fprintf(stderr, "invalid argument(s)!\n");
  }
  return 0;
}


int
detab_shutdown(
struct detab* const self
) {
  if (self->in_file != stdin && fclose(self->in_file)) {
    fprintf(stderr, "there was an error closing the [in] file!\n");
    return 0;
  }
  return 1;
}


int
detab(
struct detab* const self
) {
  int c;
  unsigned long int i = 1;
  unsigned long int col = 0;
  unsigned long int tab = 0;

  while((c = getc(self->in_file)) != EOF) {
    switch (c) {
    case '\t':
      do {
        if (putc(' ', self->out_file) == EOF) {
          goto bail_out;
        }
        ++i;
      } while (tab < TAB_MAX && ++col % self->tabs[tab]);
      if (tab < TAB_MAX) ++tab;
      break;
    case '\n':
      i = 0;
      col = -1;
      tab = 0;
    default:
      ++i;
      if (tab < TAB_MAX && ! (i % self->tabs[tab])) {
        ++tab;
      }
      col = (col + 1) % self->tabs[tab];
      if (putc(c, self->out_file) == EOF) {
        goto bail_out;
      }
    }
  }
bail_out:;
  if (ferror(self->in_file)) {
    fprintf(stderr, "there was an error reading from the [in] file!\n");
    return 0;
  }
  if (ferror(self->out_file)) {
    fprintf(stderr, "there was an error writing to the [out] file!\n");
    return 0;
  }
  return 1;
}




int main(
int argc,
char** argv
) {
  int status = EXIT_FAILURE;
  struct detab self = { NULL };
  if (detab_startup(&self, argc, argv)) {
    status = EXIT_SUCCESS;
    if (! detab(&self)) {
      status = EXIT_FAILURE;
    }
    if (! detab_shutdown(&self)) {
      status = EXIT_FAILURE;
    }
  }
  return status;
}
____________________________________________________


;^) 

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

Back to top | Article view | comp.lang.c


csiph-web